Emrah Akgül
Emrah Akgül

Reputation: 630

Data Structures: I am getting undesired value

I spent a lot of time to understand the problem.I don't know why it occurred.Maybe you guys can find out and understand the problem.I wrote the necessary comments.Here is my code:

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

I am getting x's value that is different than I type on the command line.Where is the point that I miss? Thanks.

Upvotes: 0

Views: 88

Answers (1)

s.bandara
s.bandara

Reputation: 5664

In add, you overwrite the argument Node with a new object that receives the user input. x is never touched within list::add.

Upvotes: 1

Related Questions