temlate stack using linked problems in output

Discussion in 'C++' started by xenoglaux, Jan 1, 2009.

Thread Status:
Not open for further replies.
  1. xenoglaux

    xenoglaux New Member

    Joined:
    Jan 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
     
    #include <iostream.h>
    #include<stdlib.h>
    #include<conio.h>
    struct node
    {    
    int data;
        struct node *link;
        };
        struct node *top=NULL,*temp;
        void main()
        {    int choice,data;
            
                    while(1)//infinite loop is used to insert/delete infinite number of nodes    
                    {                cout << "\n1.Push\n2.Pop\n3.Display\n4.Exit\n";        
                    cout << "\nEnter ur choice:";        
                    cin >> choice;        
                    switch(choice)        
    {  case 1:
                temp=(struct node *)malloc(sizeof(struct node));
                cout << "Enter a node data :";
                cin >> data;
                temp->data=data;
                temp->link=top;
                top=temp;   
                                                            
    break;   
                                                            
        case 2:    
                if(top!=NULL)           
             {   
                              
               cout << "The poped element is " << data,top->data;
             
                top=top->link;            
             }          
                else           
              {        cout << "\nStack Underflow";                }  
                          break;   
                                         
        case 3: 
                 temp=top;  
                 if(temp==NULL)      
        {        cout << "\nStack is empty\n";  }   
    
                    while(temp!=NULL)  
        {               
                    cout << "->" <<data << "->",temp->data; 
                       temp=temp->link;            }   
                 break; 
                        
        case 4:       
            
                exit(0);        }           
                       
            }  
        }
    
    
    when i wanna push a number say it 4 6 9 11
    n i choose option2 is pop
    the element of popped is 11
    and again option 2 pop
    the element of popped is 11
    n if i put display option3
    the output is 11->11->11->11->

    do you know what's wrong with this?
    thanks
     
  2. xenoglaux

    xenoglaux New Member

    Joined:
    Jan 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Sorry, i post twice...
    My bad... How can i remove this article?
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,337
    Likes Received:
    377
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice