Implementation of stack Using Array
Implementation of stack Using Array
#include<stdio.h>
void pop();
void push();
void display();
int i,n,ch=0,top,item,stack[50];
int main()
{
printf("Enter the number of element in Stack:\t");
scanf("%d",&n);
top=-1;
printf("Stack Operations");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
do
{
printf("Enter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("!!Wrong choice!!\n");
}
}
while(ch!=4);
return 0;
}
void push(int item)
{
if(top==n-1)
printf("Overflow\n");
else
{
printf("Enter a value to be pushed:\n");
scanf("%d",&item);
stack[++top]=item;
}
}
void pop()
{
int tmp;
if(top==-1)
{
printf("underflow\n");
}
else
{
printf("Pop elements is %d\n",stack[top]);
top--;
}
}
void display()
{
if(top>0)
{
printf("Element in Stack is: \n");
for(i=0;i<n;i++)
{
printf("%d ",stack[i]);
}
printf("\nEnter next choice");
}
else
printf("stack is empty.\n");
}
Comments
Post a Comment