Program:
#include <stdio.h>
#include <stdlib.h>
int top = -1;
int stk[5];
void push()
{
if (top == 4)
printf("Overflow: Stack is Full. You cannot add more items");
else
{
top = top + 1;
printf("\nenter value");
scanf("%d", &stk[top]);
}
}
void pop()
{
if (top == -1)
printf("underflow!!!. Stack is empty. No item to delete");
else
{
printf("Deleted item is : %d", stk[top]);
top = top - 1;
}
}
void peek()
{
if (top == -1)
printf("Stack is empty. No item to display");
else
{
printf("Item at the Top is :%d",stk[top]);
}
}
void display()
{
if (top == -1)
printf("Stack is empty. No item to display");
else
{
printf("\nInformation of stack is : \n");
for (int i = top; i >= 0; i--)
printf("\n%d", stk[i]);
}
}
void main()
{
int ch;
printf("****************************************\n");
printf("****************************************\n");
printf("** Static / Linear stack Using Array **\n");
printf("** Program Created By Sheetal Garg **\n");
printf("** Assistant professor **\n");
printf("** 9467863365 **\n");
printf("****************************************\n");
printf("****************************************\n");
while (1)
{
printf("\nOptions Available are\n");
printf("1. Push \n");
printf("2. Pop \n");
printf("3. Peek \n");
printf("4. Display \n");
printf("5. Exit \n");
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Wrong choice entered");
}
}
}
Output:
****************************************
****************************************
** Static / Linear stack Using Array **
** Program Created By Sheetal Garg **
** Assistant professor **
** 9467863365 **
****************************************
****************************************
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter value10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter value20
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter value30
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter value40
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter value50
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
Overflow: Stack is Full. You cannot add more items
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
50
40
30
20
10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 3
Item at the Top is :50
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is : 50
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
40
30
20
10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is : 40
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
30
20
10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is : 30
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
20
10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is : 20
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is : 10
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Stack is empty. No item to display
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
underflow!!!. Stack is empty. No item to delete
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 5