What is stack?
Stack is a linear data structure which can store object in particular order
The order may be
- Last in First out
- First in last out
Basic Functions use in stack
- Push()
- Pop()
- Isempty()
- Isfull()
- Peek()
Push()
Push function is used to insert element at the top of stack
Steps:
- Check stack is it full or not
- If stack full it produces error
- If stack is not full increment top to point next empty space
POP()
Pop function is used to remove a elements from top of stack
Step
Create temp variables
Assign value to temp
Decrement on top
Create temp variables
Assign value to temp
Decrement on top
ISEMPTY()
If top is equal to -1 then stack is empty
ISFULL()
If stack is equal to its size then stack is full
EXAMPLE PROGRAM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<iostream> using namespace std; struct stack { int top; int data[5]; }; void initialize (stack & s) { s.top=-1; for(int i=0 ; i<=5 ; i++) { s.data[i]=0; } } void push (stack &p , int n) { p.top++; p.data[p.top]=n; } void pop (stack &s) { int temp; temp=s.data[s.top]; s.top--; cout<<"\n"<<temp<<endl; } void display (stack &t) { cout<<"Values in stack :"; for(int i=4 ; i>=0 ;i-- ) { cout<<t.data[i]<<"\t"; } } int main() { stack s1; initialize(s1); push(s1 , 5); push(s1 , 4); push(s1 , 15); push(s1 , 44); push(s1 , 34); pop(s1); display(s1); } |
Order of operation
Parantheses
() {} []
Exponents
Left to Right
Multiplication and Division
Left and right
Addition and subtraction
Left to right
5+3*2+3/4
INPUT | ACTION | STACK | POST FIX STRING |
---|---|---|---|
5 | Append | $ | 5 |
+ | Push | $+ | 5 |
3 | Append | $+ | 53 |
* | Push | $+* | 53 |
2 | Append | $+* | 532 |
+ | Pop | $+ | 532*+ |
3 | Append | $+ | 532*+3 |
/ | pop | $+/ | 532*+3 |
4 | Append | $+/ | 532+*34 |
532+*34/+ |
0 Comments