//STACK IMPLEMENTATION USING ARRAY
STACK -LIFO
STACK IMPLEMENTATION USING ARRAYS
Fixed stack(10)
A[10], TOP=-1
A[9] 76
A[8] 78
A[7] 55
A[6] 90
A[5] 32
A[4] 89
A[3] 34
A[2] 86
A[1] 56
A[0] 98
TOP is the variable to hold the index of the array
TOP is the variable that gives information about the
position of last filled data(usefull) in the stack
If TOP==-1 Stack is Empty
Data=98, PUSH this data on to the stack
INCREMENT AND PUSH
*Increment the top(TOP=0)
*A[TOP]=data;
A[0]=98;
Data=56, PUSH this data on to the stack
*Increment the top(TOP=1)
*A[TOP]=data;
A[1]=56;
….……
TOP=10-1=9
Stack is full
POP (Removing the last filled data from the stack)
POP and DECREMENT
TOP=8
DataR=A[TOP]
DataR=A[9]
DataR=76;
TOP=8
// isStackfull is a function that returns 1 if stack is full else
it will return 0
int isStackfull()
{
if(TOP==MAX-1)
Return 1;
Else
Return 0;
// isStackEmpty is a function that returns 1 if stack is
empty else it will return 0
int isStackEmpty()
if(TOP==-1)
Return 1;
Else
Return 0;
//PUSH
void push(int data)
{
if(isStackfull==1)
Printf(“STACK IS FULL”);
Else
//INCREMENT AND PUSH
TOP++;
A[TOP]=data
//POP
int pop()
Int D;
If(isStackEmpty==1)
Printf(“STACK iS EMPTY”);
Else
D=A[TOP];
TOP--;
Return D;
//PEEK
int pop()
Int D;
If(isStackEmpty==1)
Printf(“STACK iS EMPTY”);
Else
D=A[TOP];
Return D;
}
}
#include <stdio.h>
#include<stdlib.h>
#define MAX 10
int A[MAX];
int TOP=-1;
int isStackFull();
int isStackEmpty();
void PUSH(int);
int POP();
int PEEK();
void display();
void main()
{
int choice,data,dataR,dataP;
while(1)
{
printf("\nMENU\n");
printf("[Link]\[Link]\[Link]\[Link]\[Link]\n");
printf("\nEnter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter Data to be PUSHED:");
scanf("%d",&data);
PUSH(data);
break;
case 2: dataR=POP();
printf("\nThe Data that was POPPED is %d",dataR);
break;
break;
case 3: dataP=PEEK();
printf("\nThe Data at the TOP of the Stack is %d",dataR);
break;
case 4: printf("\nThe Data Elements in the Stack is ");
display();
break;
case 5: exit(1);//exit from while()
default:printf("Invalid:");
break;
}
}
}
int isStackFull()
{
if(TOP==MAX-1)
return 1;
else
return 0;
}
int isStackEmpty()
{
if(TOP==-1)
return 1;
else
return 0;
}
void PUSH(int D)
{
if(isStackFull())
printf("STACK OVERFLOW!!!!");
else
{
TOP++;
A[TOP]=D;
}
}
int POP()
{
int D;
if(isStackEmpty())
{
printf("STACK UNDERFLOW!!!!");
exit(1);
}
else
{
D=A[TOP];
TOP--;
return D;
}
}
int PEEK()
{
int D;
if(isStackEmpty())
{
printf("STACK UNDERFLOW!!!!");
exit(1);
}
else
{
D=A[TOP];
return D;
}
}
void display()
{
int i;
for(i=0;i<=TOP;i++)
printf("%d ",A[i]);
}