Sunteți pe pagina 1din 2

#include<iostream.

h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
struct stack {
int item[7];
int top;
};
void push(struct stack *);
void pop(struct stack *);
void print(struct stack *);
void position(struct stack *);
void main()
{
int choice;
clrscr();
struct stack *obj;
obj->top=0;
do{
delay(900);
clrscr();
cout<<"\n\t\tMy Stack Menu"<<endl;
cout<<"\tEnter your choice:"<<endl;
cout<<"\t1.PUSH/Enter data"<<endl;
cout<<"\t2.POP/Delete data"<<endl;
cout<<"\t3.PRINT/Show stored data"<<endl;
cout<<"\t4.TOP/Position"<<endl;
cout<<"\t5.EXIT"<<endl;
cin>>choice;
switch(choice){
case 1: push(obj);
break;
case 2: pop(obj);
break;
case 3: print(obj);
break;
case 4: position(obj);
break;
case 5: exit(1);
break;
default: cout<<"Error!"<<endl;
}
}while(choice!=5);
getch();
}
void push(struct stack *obj1)
{
if(obj1->top>4)
cout<<"Overflow!!!"<<endl;
else
{
int val;
cout<<"Enter Value:"<<endl;
cin>>val;
obj1->item[obj1->top]=val;
obj1->top++;
cout<<val<<" Entered Successfully!"<<endl;
}
}
void pop(struct stack *obj1)
{
if(obj1->top==0)
cout<<"Underflow!!!!"<<endl;
else{
obj1->top--;
cout<<"POP function performed successfully!"<<endl;
}
}
void print(struct stack *obj1)
{
if(obj1->top==0)
cout<<"Stack is empty!"<<endl;
else
{
int i;
for(i=0;i<obj1->top;i++)
cout<<obj1->item[i];
}
}
void position(struct stack *obj1)
{
cout<<"Top is at "<<obj1->top<<" position."<<endl;
}

S-ar putea să vă placă și