2. Stack :
Stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack.
Two operations mainly performed on stack:
a) Push is used to insert an element
b) Pop is used to delete an element.
Push
AAA
BBB
CCC
3. Stack operations :
1. Stack() creates a new stack that is empty. It needs no
parameters and returns an empty stack.
2. push(item) adds a new item to the top of the stack. It needs
the item and returns nothing.
3. pop() removes the top item from the stack. It needs no
parameters and returns the item. The stack is modified.
4. peek() returns the top item from the stack but does not
remove it. It needs no parameters. The stack is not
modified.
4. 5. isEmpty() tests to see whether the stack is empty. It needs
no parameters and returns a boolean value.
6. size() returns the number of items on the stack. It needs no
parameters and returns an integer.
Static Representation of Stack :
Algorithms for Push and Pop Operations on Stack :
5. PUSH(S, TOP, X)
This procedure insert an element X to the top of a stack which
is represented by S containing N elements with a pointer TOP
denoting the top element in the stack.
1. [Check stack overflow?]
If TOP >= N then :
Print OVERFLOW, and Return
2. [Increment TOP]
Set TOP := TOP +1
3. [Insert ITEM in new TOP position.]
Set S[TOP] := ITEM
4. Return
6. POP(S, TOP, ITEM)
This procedure deletes the top element of S and assigns it to the
variable ITEM.
1. [Check for stack underflow?]
If TOP = 0 then:
Print UNDERFLOW, and Return
2. [Assign TOP element to stack]
Set ITEM := S[TOP]
3. [Decreases TOP]
Set TOP := TOP -1
4. Return
7. Dynamic Representation of Stack :
Linked Representation :
The linked presentation of stack, i.e. stack is implemented using
a SLL.
The nodes are divided into two parts:
1. INFO hold the element of the stack.
2. LINK hold pointers to the next element in the stack.
AAA BBB CCC
TOP
8. PUSH_LINKSTK(INFO, LINK, TOP, AVAIL, ITEM)
This procedure pushes an ITEM into a linked stack.
1. [Available stack?]
If AVAIL=NULL then
Write OVERFLOW, and Exit
2. [Remove first node from AVAIL list.]
Set New := AVAIL and AVAIL := LINK[AVAIL]
3. [Copies ITEM into new node]
Set INFO[NEW] := ITEM
4. [New node points to the original top node in the stack]
Set LINK[NEW] := TOP
5. [Reset TOP]
Set TOP := NEW
6. Exit
9. POP_LINKSTK(INFO, LINK, TOP, AVAIL, ITEM)
This procedure deletes the top element of a linked list and assigns
it to the variable ITEM.
1. [Stack has an item to be removed?]
If TOP = NULL then Write : UNDERFLOW & Exit
2. [Copies top element into ITEM]
Set ITEM := INFO[TOP]
3. [Remember the old value of TOP pointer in TEMP and reset
TOP to point to the next element in the stack]
Set TEMP := TOP and TOP := LINK[TOP]
4. [Return deleted node to the AVAIL list]
Set LINK[TEMP := AVAIL and AVAIL := TEMP
5. Exit
10. Applications of Stack :
There are two main applications of stack:
1. Infix to Postfix
2. Evaluation of Postfix Expression
3.
11. 1. Infix to Postfix
Infix : The operator written in between the operands. Eg. A+B
Prefix : The notation in which the operator is written before the
operands, it is called Prefix. Eg. +AB
Postfix: The notation in which the operator is written after the
operands, it is called Postfix. Eg. AB+
12. Infix to Postfix: Algorithm
POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression P.
1. Push “(“ onto stack, and add “)” to the end of Q.
2. Scan Q from left to right and repeat steps 3 to 6 for each
element of Q until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered, then:
1. Repeatedly POP from STACK and add to P each
operator (on the top of STACK) which has the same
precedence as or higher precedence than operator.
13. 2. Add operator to stack.
[End of If Structure]
6. If a right parenthesis is encountered, then :
1. Repeatedly POP from stack and add to P each
operator (on the top of stack) until a left
parenthesis is encountered.
2. Remove the left parenthesis. [Do not add to P.]
[End of If Structure]
[End of step 2 loop]
6. Exit
14. 1. A+(B*C-(D/E^F)*G)*G
Symbol scanned STACK POSTFIX
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
15. ^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) ABC*DEF^/G*-H*+
17. 2. Evaluating Postfix Notation
• Use a stack to evaluate an expression in postfix notation.
• The postfix expression to be evaluated is scanned from left
to right.
• Variables or constants are pushed onto the stack.
• When an operator is encountered, the indicated action is
performed using the top elements of the stack, and the result
replaces the operands on the stack.
19. Algorithm for evaluating a postfix expression
WHILE more input items exist
{
If symb is an operand
then push (opndstk,symb)
else //symbol is an operator
{
Opnd1=pop(opndstk);
Opnd2=pop(opndnstk);
Value = result of applying symb to opnd1 & opnd2
Push(opndstk,value);
} //End of else
} // end while
Result = pop (opndstk);