実行中にこのエラーが発生します。
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";
}