linked stack output problems

xenoglaux's Avatar, Join Date: Jan 2009
Light Poster
here is the code
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);        }           
       
  }  
    }
here's the problem
when i push a number say...
2 4 5 6
the display result is 6->6->6->6->
when i choose pop options the result is
The popped element is 6
and i press 2 again to pop out
The popped element is 6 (again)

is there any mistaken in my code?
Thanks before
0
skp819's Avatar
Contributor
I am not sure but I think display should writer as below:
Code:
case 3: 
          temp=top;  
    if(temp->link!=NULL)      
 {        cout << "\nStack is empty\n";  }   
       while(temp!=NULL)  
 {               
    cout <<temp->data<<"->"; 
                temp=temp->link;           
 }   
          break;

Last edited by skp819; 2Jan2009 at 13:55..
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Is the output really 6->6->6->6->, or is it ->6->->6->->6->->6-> ? Make sure you state EXACTLY the output from the program (for example, you've changed "poped" to "popped", so clearly either you haven't given the output from the program or you've changed the code since posting it).

cout << "->" <<data << "->",temp->data;

Interesting use of the comma operator. Maybe have another look at this line?

Edit: oh yeah, and you've got a memory leak in case 2.
0
skp819's Avatar
Contributor
Here I am posting stack programming singly link list.
From here you can learn it. It will help you to find your mistake in your programe.

Code:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>

class link_stack
    
{

    struct node
    
    {

        int id;

        char name[10];

        node *next;

    };

    node *top,*x,*ptr;

    public:

        link_stack()

        {

            top=x=ptr=NULL;

        }

        void push()

        {

            x=new node;

            cout << "Enter an ID number and name: ";

            cin >> x->id >> x->name;


            x->next=top;

            top=x;

        }

        int pop(char n[])

                {    int result;

                        if(top==NULL)

                                cout<<"\nStack is Empty";

                        else

                        {    result = top->id;
                                
                                strcpy(n, top->name);

                                x=top;

                                top=top->next;

                                delete x;

                                return result;

                        }

                }

        void empty()
{

                 char name[10];
                            
                         while(!obj.empty())
                                        
                         {

                                cout << pop(name) << ": " << name << endl;

                          }

                 }
            
};

void main()

{

    link_stack obj;

    int choice;

    do

    {

        cout << "\n ----------MENU---------- \n";

        cout << "1.Push\n"
            
                        <<  "2.Pop\n"
               
                        <<  "3.Exit";

                 cout << "\nEnter your choice: ";

        cin>>choice;

        switch(choice)

        {

        case 1: obj.push();

            break;

        case 2: obj.pop();

        obj.empty();

            break;

        case 3: cout << endl;

        }

    }

    while (choice!=3);

    getch();

}