Professional Documents
Culture Documents
29/10/2020
Recap
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 2
Today’s Lecture
▪ Stack
▪ Practical applications
► Stacks in validating expressions
► Infix to Postfix conversion
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 3
Stack
Definition:
▪ Conceptually, a stack is a data structure that allows
adding and removing elements in a particular order
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 4
Stack
▪ Example:
► Which is the first coin to pick up from
the stack of gold coins?
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 5
Basics of stack
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 6
Stack Operations
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 7
Stack Operations
▪ Push Concerns
► What if when the stack is full?
▪ Pop Concerns
► What if the stack is empty
Solution:
▪ Before any push, check if the stack is already filled or not
► If it is filled then generate error message e.g., Stack
Overflow
▪ Before pop, check if stack is not already empty
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 8
Stack Operations
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 9
Applications
▪ Real life
► Stack of trays in cafeteria
► Piles of books in library
▪ Computer Science
► Compilers use stacks to evaluate expressions & syntax
parsing
► Program execution stack
► Undo operations
► Expression conversion
► Tower of Hanoi
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 10
Choice of implementation
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 13
Array based stack
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 14
Array based stack
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 15
Array based stack operations
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 16
Linked List based implementation
▪ LIFO
► Push = Insert at head
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 17
Linked List based implementation
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 19
Linked List based implementation
int stack::pop() bool stack::isEmpty()
{ int d; {
if(isEmpty()) { if(getStackSize()==0)
cout<<"\nStack is Empty\n"; return true;
return -9999;
} return false;
else { }
Node *temp = top;
top=top->next;
d=temp->value;
delete temp;
size--; }
return d; }
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 20
Today’s Lecture
▪ Stack
▪ Practical applications
► Stacks in validating expressions
► Infix to Postfix conversion
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 21
Algebraic expression
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 22
Stacks in problem solving
Validation
▪ There is an equal number of right and left parentheses
▪ Every right parenthesis is preceded by a matching left
parenthesis
(A+B) * C Valid
A + B) Invalid
(A+B] Invalid
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 23
Rules
((A+B) (A*B)
122221 11110
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 24
Parsing parenthesis
( ( (
{ { { { {
1 2 3 4 5 6 7
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 28
Operator Priorities
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 29
Tie breaker
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 30
Infix, Prefix & Postfix expressions
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 31
Prefix & Postfix notations, why to use?
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 33
Prefix & Postfix notations, why to use?
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 34
Expression tree
Prefix
Infix
Postfix
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 35
Infix to Postfix
Example:
A+B*C
A+(B*C) Parentheses for emphasis
A+(BC*) Convert the multiplication, Let D=BC*
A+D Convert the addition
AD+
ABC*+ Postfix Form
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 36
Examples of Infix to Post- & Prefix
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 37
More examples
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 38
Infix to Postfix conversion algorithm
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 39
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,
Expression Stack Output
2 Empty 2
* * 2
3 * 23
/ / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
* +* 23*21-/53
3 +* 23*21-/53
Empty 23*21-/53*+
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 40
Evaluating Postfix expression
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 41
Algorithm to evaluate Postfix expression
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 42
Algorithm to evaluate Postfix expression
WHILE more input items exist
{
If symb is an operand
then push (stk,symb) // stk is the stack used
else //symbol is an operator
{
Opnd1=pop(stk);
Opnd2=pop(stk);
Value = result of applying symb to opnd1 & opnd2
Push(stk,value);
} //End of else
} // end while
Result = pop (stk);
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 43
Algorithm to evaluate Postfix expression
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 44
Question
623+-382/+*2^3+
Final answer is
a) 49
b) 51
c) 52
d) 7
e) None of these
M. Shahzad: Data Structures & Algorithms Lecture 6: Stack & Its Applications 45