chapter five stack
TRANSCRIPT
-
8/8/2019 Chapter Five Stack
1/76
CHAPTER FIVE:
STACKS AND QUEUES
December 20, 2010
-
8/8/2019 Chapter Five Stack
2/76
STACKS
December 20, 2010
-
8/8/2019 Chapter Five Stack
3/76
What is Stack?
Stack is a simple data structure or a list with the restriction
in which insertion and deletion occur at the same end.
It is a LIFO (Last In First Out) structure.
A Stack is an ordered collection of data items, into which new items may be inserted and from which items may be
deleted at only one end; and that end is called Top of the Stack.
The other end is called bottom
-
8/8/2019 Chapter Five Stack
4/76
Operations of Stack
The operations of insertion and deletion are called
Push -push (put) item onto stack
Pop -pop (get) item from stack
top
top
top
top AA
B
A
empty stack push an element push another pop
empty stack
top
push an element
Atop
push anotherB
toppop
-
8/8/2019 Chapter Five Stack
5/76
Implementation of Stacks
Any list implementation could be used to implement a stack
Arrays (contiguous list)(static: the size of stack is given initially)
Linked lists (dynamic: never become full) Next Chapter
Lets see how to use an array to implement a stack first Unlike an array,
stack provides the insertion and deletion of items.
So a stack is a dynamic, constantly changing object.
Although an array cannot be a stack it can be the home of the
stack The stack will grow and shrink within the space reserved for it.
One end of the array will be fixed as bottom of the stack,
while the top of the stack will constantly shift as items are popped andpushed.
-
8/8/2019 Chapter Five Stack
6/76
Array Implementation of Stacks:
The basic operations:
The PUSH Operation
Step-1: Increment the Stack TOP by 1. Check whether it is always
less than the Upper Limit of the stack. If it is less than the UpperLimit go to step-2 else report -"Stack Overflow"
Step-2: Put the new element at the position pointed by the TOP
The POP Operation
Step-1: If the Stack is empty then give the alert "Stack underflow"
and quit; or else go to step-2
Step-2: a) Hold the value for the element pointed by the TOP
b) Put a NULL value instead
c) Decrement the TOP by 1
-
8/8/2019 Chapter Five Stack
7/76
Array Implementation
Push()
{
if there is no room
give an error message
else
put an item on the top of the stack
}
Pop()
{
if stack emptygive an error message
else {
return the value of the top item
remove the top item from the stack
}
}
-
8/8/2019 Chapter Five Stack
8/76
Example of Push & Pop operations
A stack is some time called as LIFO (Last In First Out) list
Top= -1
0
4
1
3
Again Pop 2Empty stackPush 40Push 50
Push 20Push 30Push 10
10
20
30
50
40
Pop
top
top
top
top
-
8/8/2019 Chapter Five Stack
9/76
A Stack in C++ declared as a class.
class stack
{
private: int st[MAXSIZE];int top;
public: void init();
void push();
void pop();
void list();};
class definition
stack s ; Object declaration
-
8/8/2019 Chapter Five Stack
10/76
A Stack in C++
Constructor to initialize the stack:void stack::init()
{
top=0;
}
Member function definition for PUSH operationvoid stack::push()
{
if(top==MAXSIZE)
{
cout
-
8/8/2019 Chapter Five Stack
11/76
A Stack in C++
Member Function definition to pop an element from a stack.
void stack::pop()
{if(top==0)
{
cout
-
8/8/2019 Chapter Five Stack
12/76
A Stack in C++
Member Function definition to display the elements of stack.
void stack::list()
{
if(top==0)
{
cout
-
8/8/2019 Chapter Five Stack
13/76
Applications of stacks
Although the stack data structure is one of the simplest,
it is essential in certain important applications:
To reverse a sequence
Matching braces and parentheses
Stacks are used to hold the return addresses of the function
calls
In recursive programs Expression evaluation
In converting the in-fix expression into postfix form.
In graph traversals. Etc,.
-
8/8/2019 Chapter Five Stack
14/76
Matching braces and parentheses
To check that every right brace, bracket, andparentheses must correspond to its left counterpart e.g. [( )] is legal, but [( ] ) is illegal
Algorithm(1) Make an empty stack.(2) Read characters until end of file
i. If the character is an opening symbol, push it onto thestack
ii. If it is a closing symbol, then if the stack is empty, reportan error
iii. Otherwise, pop the stack. If the symbol popped is not the
corresponding opening symbol, then report an error
(3) At end of file, if the stack is not empty, report an
error
-
8/8/2019 Chapter Five Stack
15/76
Matching braces &
0. Create an empty stack S.
1. While( there are characters left ){
2. Read a character ch.
3. If is ch an opening paren (of any kind), push it onto S4. Else
5. If ch is a closing paren (of any kind), look at the top of S.
6. If S is empty as this point, report failure.
7. If the top of S is the opening paren that corresponds to c,then pop S and continue to 1, this paren matches OK.
8. Else report failure.
9. If at the end of input the stack S is not empty, return failure.
Else return success.
-
8/8/2019 Chapter Five Stack
16/76
Matching braces &
-
8/8/2019 Chapter Five Stack
17/76
Matching braces &
Familiar Stacks: Web Browser
Web Browsers use stacks. Where?
To validate HTML tags to make sure that they match.
`bold text plain text italics
bold italics with messed up tags
Stack of pages visitedUsed by Back and Forward
-
8/8/2019 Chapter Five Stack
18/76
Return addresses of the function calls
When a function is called, arguments (including the return
address) have to be passed to the called function.
10 call function abc(); /* retadrs = 11 */
11 continue;...
90 function abc;
91 code;
92 if (expression)93 call function abc(); /* retadrs = 94 */
94 code
95 return /* to retadrs */
-
8/8/2019 Chapter Five Stack
19/76
Evaluation of Algebraic Expressions
e.g. 4 + 5 * 5 (type as it is in both calculator)
simple calculator ?
scientific calculator ?
Can we develop a method of evaluating arithmetic expressionswithout having to look ahead or lookback?
ie consider the quadratic formula:
x = (-b+(b^2-4*a*c)^0.5)/(2*a)
Re-expressing the Expression
-
8/8/2019 Chapter Five Stack
20/76
Expression
Infixform
The normal (or human) way of expressing mathematical expressions
e.g.4+5*5
Prefixform the operators are written before their operands
e.g.+4*55
This method is called Polish Notation (because this method was
discovered by the Polish mathematician Jan Lukasiewicz).
Postfix form
the operators come after their operands
e.g. 4 5 5 * +
This method called suffix form orreverse polish notation
-
8/8/2019 Chapter Five Stack
21/76
Expression
Infix, Postfix and Prefix notations are used in many calculators.
The valuable aspect of RPN (Reverse Polish Notation or postfix )
Parentheses are unnecessary
Easy for a computer (compiler) to evaluate an arithmetic expression
Postfix (RPN) The easiest way to implement the Postfix expression is to use
stack.
Infix and prefixnotations can be converted to postfix notation
using stack.
-
8/8/2019 Chapter Five Stack
22/76
postfix calculations
Scan the postfix expression left to right
If the character x is a digit
Push it into the stack
if the character is an operator (+,-,*,/)
pop two values from the stack
Perform the operation
Push the result of the operation into the stack
When all characters in postfix expression are
processed
pop the value from the stack and output it.
-
8/8/2019 Chapter Five Stack
23/76
Postfix Evaluation
initialise stack to empty;
while (not end of postfix expression) {
get next postfix item;
If(item is value)
push it onto the stack;
else if(item is binary operator) {
pop the stack to x;
pop the stack to y;
perform y operator x;
push the results onto the stack; }
else if (item is unary operator) {
pop the stack to x;
perform operator(x);
push the results onto the stack }
}
The single value on the stack is the desired result.
Binary operators: +,-,*,/,etc.,
Unary operators: unary minus,square root,sin,cos,exp,etc.,
-
8/8/2019 Chapter Five Stack
24/76
Postfix Expression
65 2 3 + 8 *+ 3 +*
Stackpostfix calculation
=
-
8/8/2019 Chapter Five Stack
25/76
6
Postfix Expression
Stack
5 2 3 + 8 *+ 3 +*
postfix calculation
=
-
8/8/2019 Chapter Five Stack
26/76
6
Postfix Expression
Stack
2 3 + 8 *+ 3 +*
5
postfix calculation
-
8/8/2019 Chapter Five Stack
27/76
6
5
Postfix Expression
Stack
3 + 8 *+ 3 +*
2
postfix calculation
=
-
8/8/2019 Chapter Five Stack
28/76
6
5
Postfix Expression
Stackpostfix calculation
+ 8 *+ 3 +*
2
3
=
-
8/8/2019 Chapter Five Stack
29/76
Postfix Expression
Stackpostfix calculation
8 *+ 3 +*
+=
6
5
2
3
-
8/8/2019 Chapter Five Stack
30/76
Postfix Expression
Stackpostfix calculation
(
-
6
5
2 3
8 *+ 3 +*
+ 3 =
-
8/8/2019 Chapter Five Stack
31/76
postfix calculation
Postfix Expression
Stack
(
-
6
5
8 *+ 3 +*
2 + 3 =
-
8/8/2019 Chapter Five Stack
32/76
d ( e+f )
a b + c -
*
Stackpostfix calculation
Postfix Expression
Stack
(
-
6
5
8 *+ 3 +*
2 + 3 = 5
-
8/8/2019 Chapter Five Stack
33/76
postfix calculation
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
8 *+ 3 +*
5 =
-
8/8/2019 Chapter Five Stack
34/76
postfix calculation
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
*+ 3 +*
5
8
=
-
8/8/2019 Chapter Five Stack
35/76
e+f )
a b + c d *
-
(
Stackpostfix calculation
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
+ 3 +*
5
8
* =
-
8/8/2019 Chapter Five Stack
36/76
+f )
a b + c d * e
-
(
Stackpostfix calculation
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
+ 3 +*
5 * 8 =
-
8/8/2019 Chapter Five Stack
37/76
f )
a b + c d * e
-
(
+
Stackpostfix calculation
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
+ 3 +*
5 * 8 =
-
8/8/2019 Chapter Five Stack
38/76
)
a b + c d * ef
-
(
+
Stackpostfix calculation
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
+ 3 +*
5 * 8 = 40
-
8/8/2019 Chapter Five Stack
39/76
-
8/8/2019 Chapter Five Stack
40/76
a b + c d * ef+ -
Stackpostfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
3 +*
40 +=
-
8/8/2019 Chapter Five Stack
41/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
5
3 +*
+ 40 =
-
8/8/2019 Chapter Five Stack
42/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
3 +*
5 + 40 =
-
8/8/2019 Chapter Five Stack
43/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
3 +*
5 + 40 = 45
-
8/8/2019 Chapter Five Stack
44/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
45
3 +*
=
-
8/8/2019 Chapter Five Stack
45/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
45
+*
3 =
-
8/8/2019 Chapter Five Stack
46/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
45
*
3+=
-
8/8/2019 Chapter Five Stack
47/76
-
8/8/2019 Chapter Five Stack
48/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
*
45 + 3 =
-
8/8/2019 Chapter Five Stack
49/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
*
45 + 3 = 48
-
8/8/2019 Chapter Five Stack
50/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
*
=
48
-
8/8/2019 Chapter Five Stack
51/76
-
8/8/2019 Chapter Five Stack
52/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
* 48 =
-
8/8/2019 Chapter Five Stack
53/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6 * 48 =
-
8/8/2019 Chapter Five Stack
54/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6 * 48 = 288
-
8/8/2019 Chapter Five Stack
55/76
postfix calculation
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
6
18
3 +*
a b + c d * ef+
-
Stack
( e+f )
a b + c d *
-
Stack
( e+f )
a b + c - d
*
Stack
Postfix Expression
Stack
(
-
288
=
-
8/8/2019 Chapter Five Stack
56/76
Infix to postfix conversion
Scan the Infix expression left to right If the character x is an operand
Output the character into the Postfix Expression
If the character x is a left or right parenthesis
If the character is (
Push it into the stack if the characteris )
Repeatedly pop and output all the operators/characters until ( is popped fromthe stack.
If the character x is a is a regular operator
Step 1: Check the character y currently at the top of the stack.
Step 2: If Stack is empty or y=( or y is an operator of lower precedence than x, then
push x into stack. Step 3: If y is an operator of higher or equal precedence than x, then pop and output y
and push x into the stack.
When all characters in infix expression are processed repeatedly pop the character(s) from the stack and output them until the stack
is empty.
-
8/8/2019 Chapter Five Stack
57/76
Infix to Postfix (RPN) Conversion
initialise stack and postfix output to empty;
while(not end of infix expression) {
get next infix item
if(item is value) append item to pfix o/p
else if(item == () push item onto stack
else if(item == )) {pop stack to x
while(x != ()
app.x to pfix o/p & pop stack to x
} else {
while(precedence(stack top) >= precedence(item))pop stack to x & app.x to pfix o/p
push item onto stack
}
}
while(stack not empty)
pop stack to x and append x to pfix o/p
-
8/8/2019 Chapter Five Stack
58/76
Infix to Postfix (RPN) Conversion
Operator Precedence (for this algorithm):
4 : ( - only popped if a matching ) is found
3 : All unary operators
2 : / *
1 : + -
-
8/8/2019 Chapter Five Stack
59/76
Infix Expression
Postfix Expression
( a + b - c ) * d ( e+f )
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
60/76
-
8/8/2019 Chapter Five Stack
61/76
+ b - c ) * d ( e+f )
(
a
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
62/76
b - c ) * d ( e+f )
(
a
+
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
63/76
- c ) * d ( e+f )
(
a b
+
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
64/76
c ) * d ( e+f )
(
a b +
-
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
65/76
) * d ( e+f )
(
a b + c
-
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
66/76
-
8/8/2019 Chapter Five Stack
67/76
d ( e+f )
a b + c -
*
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
68/76
( e+f )
a b + c - d
*
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
69/76
( e+f )
a b + c d *
-
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
70/76
e+f )
a b + c d *
-
(
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
71/76
+f )
a b + c d * e
-
(
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
72/76
f )
a b + c d * e
-
(
+
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
73/76
)
a b + c d * ef
-
(
+
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
74/76
a b + c d * ef+
-
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
75/76
a b + c d * ef+ -
Infix Expression
Postfix Expression
StackInfix to postfix conversion
-
8/8/2019 Chapter Five Stack
76/76
Thank you!!!
u F m m s