Lab Assignment
Lab Assignment
Lab Assignment
Index
1.Write a menu driven C program to
implement the following stack operations:
i. PUSH
ii. POP
iii. PEEK
iv. DISPLAY
Q1.Write a menu driven C program to implement
the following stack operations:
i. PUSH
ii. POP
iii. PEEK
iv. DISPLAY
Algorithm(Pseudo Code):
PUSH
if top>=size then
print “Stack is full”
else
top=top+1
A[top]=item
end if
stop
POP
if top<1 then
print “Stack is empty”
else
item=A[top]
top=top-1
end if
stop
CODE:
#include<stdio.h>
#include<stdlib.h>
int top=0;
void PUSH(int A[10],int size){
int x;
if(top>=size)
printf("Stack overfllow\n");
else{
printf("Enter element to be pushed ");
scanf("%d",&x);
top=top+1;
A[top]=x;
}
}
void POP(int A[10],int size){
if(top<1)
printf("Stack underflow");
else{
printf("%d is popped\n",A[top]);
top=top-1;
}
}
void peek(int A[10],int size){
if(top<1)
printf("Stack underflow\n");
else
printf("Top element is %d\n",A[top]);
}
void display(int A[10],int size){
if (top<1)
printf("Stack underflow\n");
else{
printf("Elements in stack are\n");
for(int i=top;i>0;i--){
printf("%d ",A[i]);
}
}
}
int main(){
int ch,A[10];
printf("Enter the size of stack ");
int size;
scanf("%d",&size);
while(1){
printf("\nMENU\n");
printf("1.PUSH\n2.POP\n3.peek\n4.display\n5.exit\n");
printf("Enter your choice ");
scanf("%d",&ch);
switch(ch){
case 1:PUSH(A,size);break;
case 2:POP(A,size);break;
case 3:peek(A,size);break;
case 4:display(A,size);break;
case 5:exit(0);
}
}
}
Sample Output: