链表访问冲突

Linked List access violation

本文关键字:访问冲突 链表      更新时间:2023-10-16

我在执行过程中收到此错误。

你看我之前有一个空闲(临时) 库特<<语句。我删除了它们。我以为这是因为糟糕的取消引用,结果证明它更多的东西。

这是我的程序:

#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 insertn:";
    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 listn";
    }
    else
    {
        node* temp=(node*)malloc(sizeof(*temp));
        temp->data=num;
        temp->next=NULL;
        current->next=temp;
        current=temp;
        cout<<"Added elementn";
        free(temp);
        cout<<"dereferenced elementn";
    }
}
void delete_node()
{
    if(head!=NULL && head->next==NULL  )//only one node
    {
        current=head=NULL;
        cout<<"Deleted Headn";
    }
    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 elementn";
      // free(temp);
       cout<<"Dereferenced tempn";
    }
    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 listn";
    //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<<"foundn";
    else
    cout<<"not foundn";
    //free(temp);
    cout<<"dereferenced temp";
}

void main()
{
    int n=0;
    k:
    cout<<"Linked List operations: n1. insert n2. delete n3. searchn 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 insertn:";
    cin>>num;
    if(head==NULL)
    {
        head->data=num;
        head->next=NULL;
        current=head;
        cout<<"Created listn";
    }
    else
    {
        struct node* temp=(node*)malloc(sizeof(node));
        temp->data=num;
        temp->next=NULL;
        current->next=temp;
        current=temp;
        cout<<"Added elementn";
        cout<<"dereferenced elementn";
    }

}
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 Headn";
    }
    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 elementn";
      free(temp->next);
       cout<<"Dereferenced tempn";
    }
    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 listn";
    //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<<"foundn";
    else
    cout<<"not foundn";
    free(temp);          //shoudl I free temp?
    cout<<"dereferenced temp";
}

代码中存在多个问题:

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

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

  3. 主的返回值应为int

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

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

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

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

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

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