Exp 1 Final STK Adt Wup
Exp 1 Final STK Adt Wup
Date of Performance:
Date of Submission:
Theory:
A Stack is one of the most common Data Structure. We can implement stack using an Array or
Linked list. Stack has only one End referred as TOP. So the element can only be inserted and
removed from TOP only. Hence Stack is also known as LIFO (Last In First Out).
Operation Purpose
push() Pushing (storing) an element on the stack.
pop() Removing (accessing) an element from the stack.
peek() get the top data element of the stack, without removing it.
isFull() check if stack is full.
isEmpty() check if stack is empty.
(top=3) (top=4)
(top=4) (top=3)
Program:
#include<stdio.h>
int size = 5;
int top = -1;
int val;
int choice;
int STK[5];
int i;
int j;
int k;
int isstkfull(){
if(top == (size - 1))
return 1;
else
return 0;
}
int isstkempty(){
if(top == -1)
return 1;
else
return 0;
}
int pop()
{
if(isstkempty())
printf("Stack is Empty cannot pop\n");
else{
val = STK[top];
top--;
return val;
}
}
int stktop(){
if(isstkempty())
printf("Stack is Empty\n");
else{
val = STK[top];
return val;
}
}
void display(){
if(isstkempty())
printf("Stack is empty\n");
else{
for(i=top; i>=0; i--){
val = STK[i];
printf("%d\n",val);
}
}
}
int main()
{
do{
printf("1.PUSH\n2.POP\n3.StackTop\n4.StackisFull\n5.StackisEmpty\n6.Display\n7.Exit\
n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the value to be pushed in the stack\n");
scanf("%d",&val);
push(val);
break;
case 2:
val = pop();
printf("Pop val= %d\n",val);
break;
case 3:
val = stktop();
printf("Top of stack value is = %d\n",val);
break;
case 4:
j = isstkfull();
if(j == 1)
printf("Stack is Full\n");
else
printf("Stack is not Full\n");
break;
case 5:
k = isstkempty();
if( k == 1)
printf("Stack is Empty\n");
else
printf("Stack is not Empty\n");
break;
case 6:
display();
break;
case 7:
printf("Exit the Program\n");
break;
}
}while(choice!=7);
}