Skip to content

Instantly share code, notes, and snippets.

@krishnaprajapat
Created December 6, 2018 04:21
Show Gist options
  • Save krishnaprajapat/c2ded4c44f225d919edf0ded9c8587c7 to your computer and use it in GitHub Desktop.
Save krishnaprajapat/c2ded4c44f225d919edf0ded9c8587c7 to your computer and use it in GitHub Desktop.
Array representation of stack
#include<stdio.h>
#define MAX 100;
int top=-1,stack[100];
void push(int item,int n)
{
if(top==n-1)
printf("\n stack overflow");
else
{
top=top+1;
stack[top]=item;
}
}
void pop()
{
int temp;
if(top==-1)
printf("\n stack underflow");
else
{
temp=stack[top];
top=top-1;
}
}
void display(int n)
{
for(int i=0;i<=top;i++)
{
printf("\n%d",stack[i]);
}
}
int main()
{
int n,item,ch;
printf("\n enter size");
scanf("%d",&n);
printf("\n1push\n2pop\n3display");
while(1){
printf("\n enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("enter number");
scanf("%d",&item);
push(item,n);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display(n);
break;
}
default:
printf("\n invalid choice");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment