リンクリストから要素を削除した後に表示すると、削除された要素の代わりに0が表示されます。ノードの更新に問題があります。何が起こっているのか説明してもらえますか?なぜ0が表示されるのですか?
#include<iostream>
#include<stdlib.h>
using namespace std;
class node {
public:
int data;
node *link;
};
class linkedlist {
node *head;
public:
linkedlist() {
head=NULL;
}
int add(int data1) {
node *insertnode=new node;
insertnode->data=data1;
insertnode->link=NULL;
node *temp=head;
if(temp!=NULL)
{
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=insertnode;
}
else{head=insertnode;}
}
void disp()
{
node *temp1=head;
cout<<endl;
if(temp1==NULL)
{
cout<<"Empty"<<endl;
}
if(temp1->link==NULL)
{
cout<<temp1->data<<endl;
}
else {
do {
cout<<temp1->data<<endl;
temp1=temp1->link;
} while(temp1!=NULL);
}
}
int remove(int removedata)
{
node *previous;
node *temp2=head;
if(temp2==NULL)
{exit(0);}
if(temp2->link==NULL)
{
delete temp2;
head=NULL;
}
else
{
while(temp2!=NULL)
{
if(temp2->data==removedata)
{
previous=temp2;
delete temp2;
}
temp2=temp2->link;
}
}
}
};
int main()
{
linkedlist list;
list.add(10);
list.add(100);
list.add(200);
list.remove(10);
list.disp();
}
表示される出力は次のとおりです。
0
100
200