Professional Documents
Culture Documents
in/trb-study-materials/
Stack
What is Stack?
1. Stack is LIFO Structure [ Last in First Out ]
4. In Stack all Operations such as Insertion and Deletion are permitted at only one end
called Top
Visual Representation of Stack:
Field Value
1. Create
2. Push
3. Pop
4. Empty
Creating Stack:
1. Stack can be created by declaring the structure with two members.
2. One Member can store the actual data in the form of array.
V.MANIKANDAN. M.Sc.,B.Ed.,M.Phil.,CCNA.
Paavai Engineering College, Namakkal-18. E_Mail ID:- vmaniapt@Gmail.com
http://www.tnbedcsvips.in/trb-study-materials/
Push on Stack:
We have declared data array in the above declaration. Whenever we add any element in the
Suppose “top” is a pointer to the top element in a stack. After every push operation, the value of
“top” is incremented by one.
Pop on Stack :
Whenever we try to remove element from the stack then the operation is called as POP Operation
on Stack.
V.MANIKANDAN. M.Sc.,B.Ed.,M.Phil.,CCNA.
Paavai Engineering College, Namakkal-18. E_Mail ID:- vmaniapt@Gmail.com
http://www.tnbedcsvips.in/trb-study-materials/
http://www.tnbedcsvips.in/trb-study-materials/
Concept Definition
-1 Stack is Empty
http://www.tnbedcsvips.in/trb-study-materials/
http://www.tnbedcsvips.in/trb-study-materials/
1. Return Type : Integer. [Empty Stack Return 1 , Non Empty Stack Return 0 ]
} stack;
Empty Function :
1. Return Type : Integer. [If full Stack Return 1 , not full Stack Return 0 ]
void main()
{
stack s; // Declare Stack Variable
-------
-------
i = full(&s); // Pass By Reference
------
------
}
2. As full Function returns integer , we have facility to store returned value into some
V.MANIKANDAN. M.Sc.,B.Ed.,M.Phil.,CCNA.
Paavai Engineering College, Namakkal-18. E_Mail ID:- vmaniapt@Gmail.com
http://www.tnbedcsvips.in/trb-study-materials/
http://www.tnbedcsvips.in/trb-study-materials/
Complete Code
Push Operation:
o First Increment Variable “top“ so that it now refers to next memory location.
3. Main Function Should ensure that stack is not full before making call to push() in order to
2. Decrement Top by 1
x = s->data[s->top];
s->top = s->top - 1;
return(x);
}
V.MANIKANDAN. M.Sc.,B.Ed.,M.Phil.,CCNA.
Paavai Engineering College, Namakkal-18. E_Mail ID:- vmaniapt@Gmail.com
http://www.tnbedcsvips.in/trb-study-materials/
http://www.tnbedcsvips.in/trb-study-materials/
Application of Stack :
1. Parsing
2. Recursive Function
3. Calling Function
4. Expression Evaluation
5. Expression Conversion
I. Infix to Postfix
6. Towers of hanoi
Expression Representation Techniques :
1. Infix Expression
2. Prefix Expression
3. Postfix Expression
Evaluation of Postfix Expression : [ Click Here ]
Expression Example Note
Generally postfix expressions are free from Operator Precedence thats why they are preferred in
Computer system.Computer System Uses Postfix form to represent expression. Following is the
example that shows evaluation of the Postfix expression using stack as data structure.
Algorithm for Evaluation of Postfix Expression
Initialize(Stack S)
x = ReadToken(); // Read Token
while(x)
{
if ( x is Operand )
Push ( x ) Onto Stack S.
if ( x is Operator )
V.MANIKANDAN. M.Sc.,B.Ed.,M.Phil.,CCNA.
Paavai Engineering College, Namakkal-18. E_Mail ID:- vmaniapt@Gmail.com
http://www.tnbedcsvips.in/trb-study-materials/
http://www.tnbedcsvips.in/trb-study-materials/
{
Operand2 = Pop(Stack S);
Operand2 = Pop(Stack S);
Evaluate (Operand1,Operand2,Operator x);
}
V.MANIKANDAN. M.Sc.,B.Ed.,M.Phil.,CCNA.
Paavai Engineering College, Namakkal-18. E_Mail ID:- vmaniapt@Gmail.com