Ds 5stacks
Ds 5stacks
Ds 5stacks
2
Introduction to Stacks
A stack is a last-in-first-out (LIFO) data structure
Adding an item
Referred to as pushing it onto the stack
Removing an item
Referred to as
popping it from
the stack
3
Stack
Definition:
An ordered collection of data items
Can be accessed at only one end (the top)
Operations:
construct a stack (usually empty)
check if it is empty
Push: add an element to the top
Top: retrieve the top element
Pop: remove the top element
4
Stack
items[MAX-1]
. .
. .
. .
items[2] C Top=2
items[1] B
items[0] A
Insert an item A
A new item (A) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2]
items[1]
items[0] A Top=0
Insert an item B
A new item (B) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2]
items[1] B Top=1
items[0] A
Insert an item C
A new item (C) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2] C Top=2
items[1] B
items[0] A
Insert an item D
A new item (D) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3] D Top=3
items[2] C
items[1] B
items[0] A
Insert Operation(Array)
PUSH(STACK, N, TOP, ITEM)
1. If TOP=N:
write OVERFLOW, and Return.
2. Set TOP := TOP + 1.
3. Set STACK[TOP]:= ITEM.
4. Return.
Delete D
an item (D) is deleted from the Top of the stack
items[MAX-1]
. .
. .
items[3] D
items[2] C Top=2
items[1] B
items[0] A
Delete C
an item (C) is deleted from the Top of the stack.
items[MAX-1]
. .
. .
items[3] D
items[2] C
items[1] B Top=1
items[0] A
Delete B
an item (B) is deleted from the Top of the stack
items[MAX-1]
. .
. .
items[3] D
items[2] C
items[1] B
items[0] A Top=0
Delete A
an item (A) is deleted from the Top of the stack.
items[MAX-1]
. .
items[4]
items[3] D
items[2] C
items[1] B
items[0] A Top=-1
Delete Operation(Array)
POP(STACK, N, TOP, ITEM)
1. If TOP = NULL then :
write: UNDERFLOW, and Return.
2. Set ITEM := STACK [TOP].
3. Set TOP := TOP - 1.
4. Return.
Insert Operation(LL)
Step 1: Allocate memory for the new node and name it as NEW_NODE
18
Postfix and Prefix Examples
INFIX POSTFIX PREFIX
A+B A B + + A B
A*B+C A B * C + + * A B C
A * (B + C) A B C + * * A + B C
A - (B - (C - D)) A B C D--- -A-B-C D
A-B-C-D A B-C-D- ---A B C D
19
"By hand" (Underlining technique):
1. Scan the expression from left to right to find an operator.
2. Locate ("underline") the last two preceding operands
and combine them using this operator.
3. Repeat until the end of the expression is reached.
Example:
2 3 4 + 5 6 - - *
2 3 4 + 5 6 - - *
2 7 5 6 - - *
2 7 5 6 - - *
2 7 -1 - *
2 7 -1 - * 2 8 * 2 8 * 16
20
Evaluating RPN Expressions
P is an arithmetic expression in Postfix Notation.
2. Scan P from left to right and Repeat Step 3 and 4 for each element of P
until the sentinel “)” is encountered.
22
Evaluate Post Fix expression
A) 2 3 1 * + 9 –
B) 1 2 3 + * 8 -
Ans: -4
Ans: -3
By hand: "Fully parenthesize-move-erase" method:
1. Fully parenthesize the expression.
2. Replace each right parenthesis by the corresponding
operator.
3. Erase all left parentheses.
Examples:
A * B + C ((A * B) + C) A * (B + C) (A * (B + C) )
((A B * C + (A (B C + *
A B * C + A B C + *
24
Stack Algorithm
POLISH (Q, P)
1. PUSH “(” on to 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:
(a) 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
@.
(b) Add @ to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly POP from STACK and add to P each operator (On the
TOP of STACK.) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Don’t add the left parenthesis to P.]
[End of If Structure.]
[End of step 2 Loop.]
7. Exit.
25
By hand: "Fully parenthesize-move-erase" method:
1. Fully parenthesize the expression.
2. Replace each left parenthesis by the corresponding
operator.
3. Erase all right parentheses.
Examples:
A * B + C ((A * B) + C) A * (B + C) (A * (B + C))
+ * A B) C) * A + B C ))
+ * A B C * A + B C
27
Which on of the following is not the
application of stack?
A) evaluation of postfix expression
B) conversion of infix to postfix expression
C) balancing parenthesis
D) print command of multiple documents
Thank You