Wtrite a c Program to implement a stack
Algorithm
PUSH
1. Check
weather the TOP is >=Size-1 then
1. Print
that the “Stack is Full”
2. Else
1. increment
top by one
2. Store
item to the location A[TOP]
3. Stop
POP
1. check
weather the TOP<0 o:p="">0>
1. Print
that the “Stack is Empty”
2. Else
1. Store
the value of A[TOP] to the variable item
2. Decrement
top by one
3. Stop
DISPLAY
1. Check
weathre TOP<0 o:p="">0>
1. Print
that the “Stack is Empty”
2. Else
1.
Check weather the TOP>SIZE-1 then
Print
that the “Stack is Full”
2. else
1. Print that the “Element at the Top of the stack
is ” A[TOP]
2. calculate
freespace=((SIZE-1)-TOP)/SIZE100
3. Print
calculated free space
3. Stop
Program
#include
#include
int arr[100];
int
maxsize;
int top=-1;
int i;
void push(int x)
{
if (top==maxsize-1)
{
printf("stack
is full");
}
else
{
top=top+1;
arr[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("empty");
}
else
{
top=top-1;
}
}
void traverse()
{
if(top==-1)
{
printf("empty");
}
else
{
for(i=0;i<=top;i++)
printf("%d",arr[i]);
}
}
void isempty()
{
if(top==-1)
printf("empty");
else
{
printf("Not empty");
}
}
void isfull()
{
if(top==maxsize-1)
{
printf("Full");
}
else
{
printf("Not Full");
}
}
main()
{
int opt,x;
printf("size of stack");
scanf("%d",&maxsize);
top=-1;
do
{
printf("1:push 2:pop 3:
traverse 4:isempty 5.isfull 6.exit");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("enter the
element");
scanf("%d",&x);
push(x);
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4:
isempty();
break;
case
5:
isfull();
break;
case
6:
break;
}
}
while(opt!=6);
getch();
}
0 comments:
Post a Comment