#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct list
{
int data;
struct list *prev,*next;
}*top;
int push();
int pop();
int peep();
int change();
void main()
{
int choice;
while(1)
{
clrscr();
cout<<"\nSTACK OPERATIONS :\n";
cout<<"1. Push\n2. Pop\n3. Peep\n4. Change";
cout<<"\n5. Exit\nEnter choice : ";
cin>>choice;
switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: peep(); break;
case 4: change(); break;
default: exit(0);
}
}
}
int push()
{
if(top==NULL)
{
top=(struct list *)malloc(sizeof(struct list));
top->prev=NULL;
}
else
{
top->next=(struct list *)malloc(sizeof(struct list));
top->next->prev=top;
top=top->next;
}
cout<<"\n\nPUSH :\nEnter data : ";
cin>>top->data;
return 0;
}
int pop()
{
if(top==NULL)
{
cout<<"\nStack empty!";
getch();
return 0;
}
top=top->prev;
free(top->next);
cout<<"\n\nPOP :\nData popped.";
getch();
return 0;
}
int peep()
{
if(top==NULL)
{
cout<<"\nStack empty!";
getch();
return 0;
}
cout<<"\n\nPEEP :\n"<<top->data;
getch();
return 0;
}
int change()
{
if(top==NULL)
{
cout<<"\nStack empty!";
getch();
return 0;
}
cout<<"\n\nCHANGE :\n";
cout<<"Current data : "<<top->data;
cout<<"\nEnter new data : ";
cin>>top->data;
cout<<"\nData changed.";
getch();
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.