This is the Implementation of Stack using Array in C. The concept of Stack is to work on LIFO(Last In First Out). The Element can be Pushed into the Stack of Size 5. The Below code shows How the stack works. The Last Element inserted by the user is shown at first position and element that insert at first is shown at last.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void push();
void pop();
void disp();
int arr[5];
int top=-1;
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:Push");
printf("\n2:Pop");
printf("\n3:Display");
printf("\n4:Exit");
printf("\n\tPlease Enter Your Choice:-");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
exit(1);
default:
printf("\n\tYou Have Entered Wrong Choice");
break;
}
}
getch();
}
void push()
{
top++;
if(top==5)
{
printf("\n\tStack is Overflow Stop Pushing Element");
top--;
}
else
{
printf("\n\tEnter the Element:=");
scanf("%d",&arr[top]);
}
}
void pop()
{
if(top==-1)
{
printf("\n\tStack is Underflow please Insert Element");
}
else
{
printf("\n\t %d is Poped",arr[top]);
top--;
}
}
void disp()
{
int ele=top;
if(top==-1)
{
printf("\n\tStack is Empty No Element");
}
else
{
while(ele>=0)
{
printf("\n\t%d",arr[ele]);
ele--;
}
}
}
0 Comment(s)