Implentation of stack
using arrays
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int arr[SIZE], top = -1, i;
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
do
{
printf("\n[1].PUSH [2].POP [3].Display [4].Exit\n");
printf("\nEnter your choice [1-4] : ");
scanf("%d", &ch);
switch(ch)
{
case 1 :
push();
break;
case 2 :
pop();
break;
case 3 :
display();
break;
case 4 :
break;
default :
printf("\nInvalid option\n");
getch() ;
}
} while(ch != 4);
getch();
}
void push()
{
if(top == SIZE - 1)
{
printf("\nStack is full (overflow)\n");
getch();
return;
}
top;
printf("\nEnter the element to PUSH : ");
scanf("%d", &arr[top]);
}
void pop()
{
if(top == -1)
{
printf("\nStack is empty (underflow)\n");
getch();
return;
}
printf("\nThe POP element is : %d\n", arr[top]);
getch();
top--;
}
void display()
{
if(top == -1)
{
printf("\nStack is empty (underflow)\n");
getch();
return;
}
printf("\nThe elements in stack are :\n\nTOP");
for(i = top; i >= 0; i--)
printf(" -> %d", arr[i]);
printf("\n");
getch();
}
Output:
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 1
Enter the element to PUSH : 10
A.120 Programs in C
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 1
Enter the element to PUSH : 20
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 3
The elements in stack are :
TOP -> 20 -> 10
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 2
The POP element is : 20
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 3
The elements in stack are :
TOP -> 10
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 2
The POP element is : 10
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 2
Stack is empty (underflow)
[1].PUSH [2].POP [3].Display [4].Exit
Enter your choice [1-4] : 4
-
UpdatedDec 31, 2019
-
Views6,711
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order