Skip to content

Instantly share code, notes, and snippets.

@hrshadhin
Created April 23, 2013 16:17
Show Gist options
  • Save hrshadhin/5445018 to your computer and use it in GitHub Desktop.
Save hrshadhin/5445018 to your computer and use it in GitHub Desktop.
Stack[last in first out]
#include<stdio.h>
void push(int x);
int pop(void);
int y;
int stack[2];
int top=0;
int count=0;
int main()
{
int input,i;
while(1)
{
printf("Press:\n1 for Push to stack\t2 for Pop to stack\n3 for print array \t4 for Exit.\n");
scanf("%d",&input);
if(input==1)
{
printf("Enter value for push to stack.\n");
scanf("%d",&y);
push(y);
}
else if(input==2)
{
pop();
}
else if(input==3)
{
for(i=count-1;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(count==0)
{
printf("Stack is empty\n");
}
}
else
{
return(0);
}
}
return(0);
}
void push(int x)
{
if(top==3)
{
printf("Stack is over flow.\n");
top--;
}
stack[top] = x;
top++;
count++;
}
int pop(void)
{
if(stack[top-1]==0)
{
printf("Stack is underflow.\n");
top++;
count += 1;
}
y = stack[top-1];
top--;
count--;
return y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment