2

我在执行期间收到此错误。

你看我在 cout<< 语句之前有一个 free(temp)。我删除了它们。我认为这是因为错误的取消引用结果证明它更多。

这是我的程序:

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

using namespace std;

struct node{
    int data;
    node* next;
};

node* head=NULL;
node* current=NULL;

void insert_node()
{
    int num=0;
    cout<<"\nEnter the value of the node to insert\n:";

    cin>>num;

    if(head==NULL)
    {
        head=(node*)malloc(sizeof(*head));
        //current=(node*)malloc(sizeof(*current));
        head->data=num;
        head->next=NULL;
        current=head;
        cout<<"Created list\n";

    }
    else
    {
        node* temp=(node*)malloc(sizeof(*temp));
        temp->data=num;
        temp->next=NULL;
        current->next=temp;
        current=temp;
        cout<<"Added element\n";
        free(temp);
        cout<<"dereferenced element\n";

    }
}

void delete_node()
{

    if(head!=NULL && head->next==NULL  )//only one node
    {

        current=head=NULL;
        cout<<"Deleted Head\n";
    }
    else if(head!=NULL && head->next!=NULL)//>= 2 nodes
    {
       node* temp;
       //temp=NULL;
       temp=head;
       while(temp->next!=current)
       {
           temp=temp->next;
       }
       temp->next=NULL;
       current=temp;
       cout<<"Deleted last element\n";
      // free(temp);
       cout<<"Dereferenced temp\n";
    }
    else
    {
        cout<<"delete was not performed";
    }
}

void list_linked_list()
{
    node* temp=(node*)malloc(sizeof(* temp));

    temp=head;

    while(temp!=NULL)
    {

        cout<<temp->data<<"->";
        temp=temp->next;

    }
    cout<<"displayed list\n";
    //free(temp);
    cout<<"dereferenced temp";
}

void search_node()
{
    cout<<"\nenter a number to search";
    int search=0,found=0;
    cin>>search;

    node* temp=(node*)malloc(sizeof(* temp));
    temp=head;
    while(temp!=NULL)
    {
        if(temp->data==search)
            found=1;
    }
    if(found==1)
        cout<<"found\n";
    else
    cout<<"not found\n";
    //free(temp);
    cout<<"dereferenced temp";
}


void main()
{

    int n=0;
    k:
    cout<<"Linked List operations: \n1. insert \n2. delete \n3. search\n 4. view List \n5. Exit";
    cin>>n;

    switch(n)
    {
    case 1: insert_node();break;

    case 2: delete_node();break;

    case 3: search_node();break;

    case 4: list_linked_list();break;
    case 5: exit(0);break;
    default: cout<<" Please enter valid number between 1 and 5";
            break;

    }
    goto k;
}

我不认为我误解了链表的概念。我很清楚。我认为指针有误。

谢谢你。

编辑:新代码:

struct node{
    int data;
    struct node* next;
};

struct node* head=NULL;
struct node* current=NULL;





void insert_node()
{
    int num=0;
    cout<<"\nEnter the value of the node to insert\n:";

    cin>>num;

    if(head==NULL)
    {

        head->data=num;
        head->next=NULL;
        current=head;
        cout<<"Created list\n";

    }
    else
    {
        struct node* temp=(node*)malloc(sizeof(node));
        temp->data=num;
        temp->next=NULL;
        current->next=temp;
        current=temp;
        cout<<"Added element\n";
        cout<<"dereferenced element\n";

    }


}

void delete_node()
{

    if(head!=NULL && head->next==NULL  )//only one node
    {

        current=head=NULL;   //Am I supposed to do anything else here??
        cout<<"Deleted Head\n";
    }
    else
    if(head!=NULL && head->next!=NULL)//>= 2 nodes
    {
       struct node* temp=(node*)malloc(sizeof(node));;
       //temp=NULL;
       temp=head;
       while(temp->next!=current)
       {
           temp=temp->next;
       }
       temp->next=NULL;
       current=temp;
       cout<<"Deleted last element\n";
      free(temp->next);
       cout<<"Dereferenced temp\n";
    }
    else
    {
        cout<<"delete was not performed";
    }


}

void list_linked_list()
{
    node* temp=(node*)malloc(sizeof(node));

    temp=head;

    while(temp!=NULL)
    {

        cout<<temp->data<<"->";
        temp=temp->next;

    }
    cout<<"displayed list\n";
    //free(temp);              //should I free temp?
    cout<<"dereferenced temp";
}

void search_node()
{
    cout<<"\nenter a number to search";
    int search=0,found=0;
    cin>>search;

    node* temp=(node*)malloc(sizeof(node));
    temp=head;
    while(temp!=NULL)
    {
        if(temp->data==search)
            found=1;
        else
            temp=temp->next;
    }
    if(found==1)
        cout<<"found\n";
    else
    cout<<"not found\n";
    free(temp);          //shoudl I free temp?
    cout<<"dereferenced temp";
}
4

2 回答 2

4

您的代码中有多个问题:

  1. 您正在free()插入函数中的一个节点,这不是您想要的。free(temp)因此,从您的插入函数中删除该行。

  2. 当您从链表中删除一个元素时,您确实希望释放该节点。所以取消注释该行:free(temp);. 但这不是current您想要释放()的正确节点。这temp是你的新的current,而你想释放()你的旧currentwhich temp->next。所以你的 free() 语句应该是:free(temp->next);in delete_node()function (Not free(temp);)。

  3. main 的返回值应该是int.

  4. 如果您使用 C++,则有更好的方法来实现链表。您可能想要使用newanddelete而不是mallocand free。使用 C++ 头文件而不是 C 头文件。

  5. 如果您确实使用 C,则不要转换 C 中返回的值malloc

  6. 当您可以简单地使用orgoto时,您将其用作循环的替代品,这是不必要的。for(;;) { }while(1) { }

于 2013-09-06T05:45:54.673 回答
3

在插入函数的 else 部分中,您在将新节点添加到链接列表中之后释放新节点,这会导致运行时出现未定义的行为:

  else
    {
        node* temp=(node*)malloc(sizeof(*temp));
        temp->data=num;
        temp->next=NULL;
        current->next=temp;
        current=temp;
        cout<<"Added element\n";
        free(temp);    <------"Bug"
        cout<<"dereferenced element\n";    
    }

注意:您不能访问已释放内存的节点 ( free()),这样做是非法操作。完成程序后,您应该为节点释放内存(并且不需要再次访问该内存)。

于 2013-09-06T05:27:09.723 回答