# include<iostream>
#include<stdlib.h>
using namespace std;
void push();
void pop();
void display();
struct node
{
int info;
struct node *link;
} *top=NULL;
int main()
{
int choice;
do
{
cout<<"\n\n1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(1);
default :cout<<"Wrong choice\n";
}
}while(choice!=4);
return 0;
}
void push()
{
struct node *tmp;
int pushed_item;
tmp = (struct node *)malloc(sizeof(struct node));
cout<<"Value to Push:";
cin>>pushed_item;
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}
void pop()
{
struct node *tmp;
if(top == NULL)
cout<<"Stack is empty\n";
else
{
tmp=top;
cout<<"Popped item is "<<tmp->info;
top=top->link;
free(tmp);
}
}
void display()
{
struct node *ptr;
ptr=top;
if(top==NULL)
cout<<"Stack is empty\n";
else
{
cout<<"Stack elements :\n";
while(ptr!= NULL)
{
cout<<"\n"<<ptr->info;
ptr = ptr->link;
}
}
}
No comments:
Post a Comment