Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created September 17, 2016 16:48
Show Gist options
  • Save priyadarshitathagat/42376c646502490417501f2a6d7a0ed4 to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/42376c646502490417501f2a6d7a0ed4 to your computer and use it in GitHub Desktop.
Stack Implementation in c
#include<stdio.h>
main()
{ int a[20],front=-1,x,n;
printf("enter size of stack\n");
scanf("%d",&n); n=n-1;
while(1)
{ printf("Enter 1 to push,2 to pop and 3 to display:\n");
int c; scanf("%d",&c);
switch(c)
{ case 1:{front=push(&a,front,n); break;}
case 2:{front=pop(&a,front); break;}
case 3:{disp(&a,front); break;}
default:printf("wrong option chosen\n");
}
printf("to continue press 0, else press 1\n");
scanf("%d",&x);
if(x==1)
break;
}
//disp(&a,front);
}
int push(int *p,int y,int n)
{ if(y>=n)
{printf("overflow\n"); return(y);}
printf("enter a value\n");
int t; scanf("%d",&t);
*(p+(++y))=t;
return(y);
}
int pop(int *p,int y)
{ if(y<=-1)
{printf("underflow\n"); return(y);}
printf("The popped out value is: %d\n",*(p+(y--)));
return(y);
}
disp(int *p,int y)
{ int i=0;
if(y==-1)
{printf("The Stack is empty.\n");return;}
printf("The Stack contains:\n");
while(y!=-1)
{
printf("%d\n",*(p+(y--)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment