我花了很多时间来理解这个问题。我不知道为什么会出现。也许你们可以找出并理解这个问题。我写了必要的评论。这是我的代码:
#include <iostream>
struct node{
////I am creating node structure.
int number;
node *next;
node *previous;
};
struct list{
////I am creating list structure.It has head and tail pointers
node *head;
node *tail;
void add(node *); // Add function
void create(); // Create function
void deleteList(); // Delete function.
}emrah;
using namespace std;
int main(){
emrah.create(); // a list called 'emrah' has been created.
cout<<"Type 1."<<endl; // So that we lead user to add a node.
int selection;
cin>>selection;
if (selection==1){ // Suppose user typed 1.
node x;// new node is x.
emrah.add(&x); // x has been sent.
cout<<x.number; // Problem is here.On the command line,it shows like -9231
}
}
void list::create(){ // Create function.It has no problem.
head=NULL;
tail=NULL;
}
void list::add(node *Node){ // I think problem is around this function.
Node=new node;
cout<<"Sayi gir"<<endl;
cin>>Node->number;
cout<<Node->number;
head=tail=Node;
}
我得到的 x 值与我在命令行上键入的值不同。我错过的点在哪里?谢谢。