删除链接列表中的节点导致程序崩溃

时间:2016-06-09 13:57:20

标签: c++ linked-list delete-operator

我试图删除链接列表中的节点,如下所示:

typedef struct
{
    char key[10];
    char name[20];
    int age;
}Data;

typedef struct Node
{
    Data nodeData;
    struct Node *nextNode;
}cltype;

删除功能定义如下:

int clDeleteNode(cltype *head, char *key)
{
    cltype *node, *htemp = new (cltype);
    htemp = head;
    node = head;
    while (htemp)
    {
        if (strcmp(htemp->nodeData.key, key))
        {
            node->nextNode = htemp->nextNode;
            delete htemp;
            return 1;
        }
        else
        {
            node = htemp;
            htemp = htemp->nextNode;
        }
    }
    //delete mp
    return 0;
}

问题是当我尝试以这种形式调用删除功能时,程序将崩溃:

    cout << "program demo for deleting node\n" << "input key:" << endl;
    fflush(stdin);
    cin >> key;
    //scanf("%s", key);
    //int i = 
    if (clDeleteNode(head, key))
    {
        cout << "Delete Successful" << endl;
    }
    else
    {
        cout << "Operation Failed" << endl;
    }
    clAllNode(head);

屏幕将显示&#34; Delete Successful&#34;,但clAllNode(...)函数将不会执行。而clAllNode(...)函数在其他地方工作得很好,所以我猜那里删除操作有问题,有人能告诉我哪里做错了吗?

谢谢。

0 个答案:

没有答案