Created
April 23, 2013 16:17
-
-
Save hrshadhin/5445018 to your computer and use it in GitHub Desktop.
Stack[last in first out]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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