STACK
ADT ARRAY
AIM:
To write a program for implementing a
stack using arrays to perform various operations such as Push, Pop, Stack
Empty, Stack full and Display.
ALGORITHM:
1.PUSH:
STEP 1:Create a function push() with argument element to be added.
STEP 2:Assign the new element in the array by incrementing the TOP
variable by 1
2.POP:
STEP 1:Create a pop(), store the
deleted value in a variable.
STEP 2: Decrement the TOP variable by 1.
3.DISPLAY:
STEP 1:Create a function display(), print the values by decrementing
index
value by 1 upto it becomes 0.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
class STACK_CLASS
{
private:
struct stack
{
int s[size];
int top;
}
st;
public:
STACK_CLASS();
int stfull();
void push(int item);
int stempty();
int pop();
void display();
};
STACK_CLASS::STACK_CLASS()
{
st.top=-1;
for(int i=0;i<size;i++)
st.s[i]=0;
}
int STACK_CLASS::stfull()
{
if(st.top>=size-1)
return 1;
else
return 0;
}
void STACK_CLASS::push(int item)
{
st.top++;
st.s[st.top]=item;
}
int STACK_CLASS::stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int STACK_CLASS::pop()
{
int item;
item=st.s[st.top];
st.top--;
return(item);
}
void STACK_CLASS::display()
{
int i;
if(stempty())
cout<<"\n Stack is Empty!";
else
{
for(i=st.top;i>=0;i--) cout<<"\n<<st.s[i]";
}
}
void main(void)
{
int item,choice;
char ans;
STACK_CLASS obj;
clrscr();
cout<<"\n\t\t implementation of Stack";
do
{
cout<<"\n Main Menu";
cout<<"\n 1.Push\n 2.Pop\n 3.Display\n 4.exit";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter the item to be pushed";
cin>>item;
if(obj.stfull())
cout<<"\n Stack is full";
else
obj.push(item);
break;
case 2:
if(obj.stempty())
cout<<"\n empty stack !underflow!!";
else
{
item=obj.pop();
cout<<"\n the popped element is"<<item;
}
break; case 3:
obj.display();
break;
case 4: exit(0);
}
cout<<"\n Do u Want To Continue?";
ans=getch();
}
while(ans=='Y'||ans=='y');
getch();}
OUTPUT :
Implementation Of Stack
Main Menu
1. Push
2. Pop
3. Display
4. exit
Enter Your Choice : 1
Enter The item to be pushed 10
You want To Continue?y Enter Your Choice : 1
Enter The item to be pushed 20
Do You want To Continue?y
Enter Your Choice : 1
Enter The item to be pushed 30
Do You want To Continue?y
Enter Your Choice : 2
The popped element is 30
Do You want To Continue?y
Enter Your Choice : 3
20 10
Do You want To Continue?n
0 Comments