链接列表访问冲突

时间:2013-09-06 05:20:51

标签: c++ linked-list access-violation

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

你看到我之前有一个免费的(临时)     COUT<< 语句。我删除了它们。我认为这是因为糟糕的解除引用更多的东西。

这是我的计划:

#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";
}

2 个答案:

答案 0 :(得分:4)

您的代码中存在多个问题:

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

  2. 当您从链接列表中删除元素时, 想要释放节点。因此,取消注释该行:free(temp);。但这不是您要释放的正确current节点()。此处temp是您的新current,而您希望释放()current temp->next。所以你的free()语句应该是free(temp->next);函数中的delete_node()(不是free(temp);)。

  3. 主要的返回值应为int

  4. 如果您使用的是C ++,则有更好的方法来实现链接列表。您可能希望使用newdelete代替mallocfree。使用C ++标头而不是C标头。

  5. 如果您使用C,则don't cast the value returned by malloc in C.

  6. 您使用goto代替循环,只需使用for(;;) { }while(1) { }即可。

答案 1 :(得分:3)

在insert function的else部分中,在将新节点添加到Linked List中之后释放新节点,导致在运行时导致未定义的行为:

  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()),这样做是非法操作。完成程序后,您应该为节点释放内存(并且不需要再次访问该内存)。