An exception is occuring while popping elements from stack.


Hello , I am doing a stack implementation using linked lists.
The program is running fine for pushing but while popping elements it is giving it is quitting.

Please run the program and check..

#include<iostream>
using namespace std;

struct list_element
{
int item;
struct list_element *next;
};

typedef struct list_element node;
int menu()
{
int choice;
do{
cout<<"Main Menu"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"3.end"<<endl;
cout<<"enter choice:"<<endl;
cin>>choice;
if(choice<1 || choice>3)
cout<<"In valid choice"<<endl;

}while(choice < 1 || choice > 3);

return choice;
}
void display(node *record)
{
if(record != NULL)
{
cout<<"->"<<record->item;
display(record->next);

}
cout<<endl;
return;
}
node *push(node *first)
{
node *newrecord;
int newitem;
cout<<"enter an item "<<endl;
cin>>newitem;
newrecord = (node *)malloc(sizeof(node));

newrecord->item = newitem;

newrecord->next = first;


first = newrecord;

return(first);

}
node *pop(node *first)
{
node *temp;
int target;
if(first == NULL)
cout<<"stack is empty"<<endl;
else
{
cout<<"Element deleted:"<<first->item<<endl;
temp = first->next;

free(first);

first = temp;

cout<<"stack after popping:"<<endl;
display(first);
}
return first;
}

int main()
{
node *start = NULL;
int key;
int choice;
do{

choice = menu();
switch(choice)
{
case 1: start = push(start);
cout<<"stack after pushing:";
display(start);
continue;

case 2: start = pop(start);
continue;

default: cout<<"terminating"<<endl;
}
}while(choice != 3);


system("pause");
return 0;
}
Topic archived. No new replies allowed.