Prac cal No.
1
Aim :- Implement Stack ADT using Array Sarang R. Shinde
Program : ID no.: VU1F2223060
#include<stdio.h> Class : SE/A Batch : C
#include<stdlib.h>
#define max 3
int stack[max];
int ele, choice,i,top=-1;
int push();
void pop ();
void peek();
int display();
void main ()
{
prin ("\n1.PUSH\t2.POP\t3.PEEK\t4.DISPLAY\t5.EXIT\n");
while (1)
{
prin ("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
case 5: exit(0);
break;
default : prin ("wrong choice entered");
break;
}
}
}
int push()
{
if (top==max-1)
prin ("stack overflow\n");
else
{
prin ("Enter the number to be added to the stack : ");
scanf("%d",&ele);
top=top+1;
stack[top]=ele;
}
}
void pop()
{
if (top==-1)
prin ("stack underflow\n");
else
{
ele= stack[top];
prin ("the deleted element is %d\n",ele);
top--;
}
}
void peek ()
{
if (top==-1)
prin ("stack underflow\n");
else
{
ele= stack[top];
prin ("the topmost element is %d\n",ele);
}
}
int display()
{
for(i=top;i>=0;i--)
prin ("%d\t",stack[i]);
prin ("\n");
}
OUTPUT :
PS C:\Users\snehal\OneDrive\Desktop> gcc 1st.c
PS C:\Users\snehal\OneDrive\Desktop> ./a.exe
1.PUSH 2.POP 3.PEEK 4.DISPLAY 5.EXIT
Enter your choice : 1
Enter the number to be added to the stack : 10
Enter your choice : 1
Enter the number to be added to the stack : 20
Enter your choice : 1
Enter the number to be added to the stack : 30
Enter your choice : 1
stack overflow
Enter your choice : 3
the topmost element is 30
Enter your choice : 4
30 20 10
Enter your choice : 2
the deleted element is 30
Enter your choice : 2
the deleted element is 20
Enter your choice : 2
the deleted element is 10
Enter your choice : 2
stack underflow
Enter your choice : 5