Introduction
A Stack is a linear data structure in which a data item is inserted and deleted at one record. A stack is called a Last In First Out (LIFO) structure. Because the data item inserted last is the data item deleted first from the stack.
Stacks are used most used is system software such as compilers, operating systems, etc.In fact, many compilers store the local variables inside a function on the stack.
Writing a value to the stack is called as a 'Push' operation, where as reading a value from it is called as a 'Pop' operation. A pop operation in stack is destructive. That is, once an item is popped from stack, then it is no longer available.
#include <stdio.h> #define Stacksize 5 void push(int); int pop(); //int getSize(); int s[Stacksize]; int top=-1; void main() { int k; push (33); push (44); push (77); k=pop(); printf("\n popped element : %d",k); k=pop(); printf("\n popped element : %d",k); } void push(int ele) { if(top==(Stacksize-1)) { printf("\n Stack overflow"); return; } else { s[++top]=ele; } } int pop() { if(top==-1) { printf("\n Stack underflow"); return -1; } else { return s[top--]; } }
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment
Marie Bustamante 7/17/2012 (IST) / Reply
The code above is correct but I hope you anyone can make more simple code for the slow learners programmer like me.
mansoor nisar 12/5/2012 (IST) / Reply
l
lakshmi 4/9/2013 (IST) / Reply
if output is given it will me more better
ttt 7/19/2013 (IST) / Reply
thanx
Seema negi 1/17/2014 (IST) / Reply
Hello sir/mam i want to know about stacks , queue,recursion in data structrure using c plz explain it easily...