0% found this document useful (0 votes)
13 views

7 Data Structure II

Vordkefnk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

7 Data Structure II

Vordkefnk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Stack:- A stack is linear structure implemented in LIFO (Last In First Out)

manner where insertions and deletions to occur only at one end –


Stack’s top. LIFO means element last inserted would be the first one to
deleted. Stack is list of data that follows these rules:

1) Data can only be removed from the top (pop), i.e. the element at the
top of the stack. The removal of element from the stack is technically
called POP operation.
2) A new data element can only be added to the top of the stack (push).
The insertion of element in a stack is technically called PUSH
operation.
The stack is a dynamic data structure as it can grow (with increase in
number of elements) or shrink (with decrease in number of element). A
static data structure, on the other hand, is the one that has fixed size.
18
element at the top poped
18 top
32 top 32 32 top

Top=None Push 32 Push 18 Pop 1 element


Empty Stack
19
element at the top poped
19 top
32 32 top

Push 29 Pop 1 element


Other Stack Term:-

Peek:- Refer to inspecting the value at the stack’s top with out removing
it. It is also referred as inspection.

Overflow:- Refer to situation (Error) when one tries to push an item in


stack that is full. This situation occur when the size of stack is fixed and
can not grow further.

Underflow:- Refer to situation (Error) when one tries to pop/delete an


item from empty stack. That is, stack is currently having no item and still
one tries to pop an item.
Example:-
i) Stack empty ii) Push ‘a’ iii) Push ‘b’ iv) Push ‘c’
v) Pop vi) Push ‘d’ vii) Pop viii) Push ‘e’
ix) Push ‘f’ x) Push ‘g’ xi) Pop xii) Pop
xiii) Pop xiv) Pop xv) Pop

Solution
top =
i) Stack is empty (top=None) ii) Push ‘a’ top=0)
0 1 2 3 0 1 2 3
a
iii) Push ‘b’ top=1 iv) Push ‘c’ top=2
0 1 2 3 0 1 2 3
a b a b c

v) Pop top=1 Remove c vi) Push ‘d’ top=2


0 1 2 3 0 1 2 3
a b a b d

vii) Pop top=1 Remove d viii) Push ‘e’ top=2


0 1 2 3 0 1 2 3
a b a b e
ix) Push ‘f’ top=3 x) Push ‘g’ top=3 Overflow
0 1 2 3 0 1 2 3
a b e f a b e f

xi) Pop top=2 Remove f xii) Pop top=1 Remove e


0 1 2 3 0 1 2 3
a b c a b

xiii) Pop top=0 Remove b xiv) Pop top=None Remove a


0 1 2 3 0 1 2 3
a
xv) Pop top=None Underflow
0 1 2 3
Application of Stack
1) Reverse a line:-A simple example of stack application is reversal of a
given line. We can accomplish this task by pushing each character on to
stack as it is read. When the line is finished, characters are then poped
off the stack, and they will come off in the reverse order.

2) Polish String:- Another application of stack in the conversion


arithmetic expressions in high-level programming language into machine
readable form. An arithmetic operation can take place in two operands
only e.g. A+B, C*D, D/A etc. But in our usual form an arithmetic
expression may consist of more than one operator an two operands e.g.
(A+B)*C(D/(E+D)). These complex arithmetic operations can be
converted into polish string using stack.
Expression in infix, prefix and postfix notation

Infix Notation Prefix Notation Postfix Notation


A+B +AB AB+
(A-B)*C *-ABC AB-C*
A+(B*C) +A*BC ABC*+
(A+B)/(C-D) /+AB-CD AB+CD-/
(A+(B*C))/(C-(D*B) /+A*BC-C*DB ABC*+CDB*-/
Conversion of Infix Expression to Postfix (Suffix) Expression
While evaluating an infix expression, there is an evaluation order according to
which

i) Brackets or Parenthesis
ii) Exponentiation
iii) Multiplication or Division
iv) Addition or Subtraction

The steps to convert an infix expression to postfix expression


i) Determine the actual evaluation order by inserting braces
ii) Convert the expression in the innermost braces into postfix notation by
putting the operator after the operands.
iii) Repeat step (ii) until entire expression is converted in to prefix notation
Example :- Convert (A+B)*C/D into postfix notation

Step 1 :- Determine the actual evaluation order by putting braces


=((A+B)*C)/D
Step 2:- Converting expression into innermost braces
=((AB+)*C)/D
=(AB+C*)/D
=AB+C*D/
Example :- Convert ((A+B)*C/D+E^F)/G into postfix notation

Step 1 :- Determine the actual evaluation order by putting braces


=((((A+B)*C)/D)+(E^F))/G
Step 2:- Converting expression into innermost braces we get
=((((A+B)*C)/D)+(EF^))/G
=((((AB+)*C)/D)+(EF^))/G
=(((AB+C*)/D)+(EF^))/G
=((AB+C*D/)+(EF^))/G
=(AB+C*D/EF^+)/G
=AB+C*D/EF^+G/
Example :- Convert A*(B+(C+D)*(E+F)/G)*H into postfix notation

Step 1 :- Determine the actual evaluation order by putting braces


=(A*(B+((C+D)*(E+F))/G))*H
Step 2:- Converting expression into innermost braces we get
=(A*(B+((CD+)*(EF+))/G))*H
=(A*(B+(CD+EF+*)/G))*H
=(A*(B+CD+EF+*G/))*H
=(A*(BCD+EF+*G/+)*H
=(ABCD+EF+*G/+*)*H
=ABCD+EF+*G/+*H*
Example :- Convert NOT A OR NOT B AND NOT C into postfix notation

Step 1 :- Determine the actual evaluation order by putting braces


= ((NOT A) OR ((NOT B) AND (NOT C)))
Step 2:- Converting expression into innermost braces we get
As priority order is NOT, AND, OR
= ((A NOT) OR ((B NOT) AND (C NOT)))
= ((A NOT) OR (B NOT C NOT AND))
= A NOT B NOT C NOT AND OR
Infix to Postfix Using Stack Algorithm
1. Push “(” onto stack, and “)” to the end of X
2. Scan X from left to right and repeats step 3 to 6 for each element of X until the stack is empty:
3. If an operator is encountered add it to Y.
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 Y each operator (on the top
of stack) which has the same precedence as or highest precedence
than operator.
(b) Add operator to stack
“””End of if structure”””
6. If a right parenthesis is encountered, then
(a) Repeatedly pop from stack and add to Y each operator (on the top
of stack) until a left parenthesis is encountered
(b) Remove the left parenthesis. [do not add the left parenthesis to Y]
“””End of if structure”””
“””End Step 2 loop”””
7. End
Convert X: A+(B*C-(D/E^F)*G)*H into postfix from showing stack after
every step in tabular form.
Symbol Scanned Stack Expression Y
1 A ( A
2 + (+ A
3 ( (+( A
4 B (+( AB
5 * (+(* AB
6 C (+(* ABC
7 - ( + (- ABC*
8 ( (+(-( ABC*
9 D (+(-( ABC*D
. Symbol Scanned Stack Expression Y
10 / ( + (- ( / ABC*D
11 E ( + (- ( / ABC*DE
12 ^ (+(-(/^ ABC*DE
13 F (+(-(/^ ABC*DEF
14 ) (+(- ABC*DEF^/
15 * (+(-* ABC*DEF^/
16 G (+(-* ABC*DEF^/ G
17 ) (+ ABC*DEF^/ G* -
18 * (+* ABC*DEF^/ G* -
19 H (+* A B C * D E F ^ / G * -H
20 ) ABC*DEF^/ G* -H*+
Evaluation of a postfix Expression using Stack
As postfix expression is without parenthesis and can be evaluated a two
operands and an operator at a time, this become easier for the compiler and
the computer to handle. Evaluation rules of a postfix expression states:

• While reading the expression from left to right, push the element in the
stack if it is an operand.
• Pop the two operands from the stack, if the element is a binary operator. In
case of NOT operator, pop one operand from the stack and then evaluate it
(two operands and an operator).
• Push back the result of the evaluation. Repeat it till the end of the
expression.
Evaluate the postfix expression AB+C*D/ if A=2, B=3, C=4 and D=5.
1. First element is operand A push A into stack

A
Stack 1
2. Second element is also operand B push B into stack

B
A
Stack 2
3. Third element ‘+’ is an operator pop 2 operands from stack i.e. A and B and
evaluate the expression i.e. A+B = 2+3=5

Empty
Stack 3
4. Push the result (5) into the stack
5
5. Next element C is operand push C in to Stack
Stack 4

C
5
Stack 5
6. Next element ‘*’ is an operator pop 2 operands from stack i.e. 5 and C and
evaluate the expression i.e. 5 * C = 5 * C =20

Empty
Stack 6
7. Push the result (20) into the stack 20
8. Next element D is operand push D in to Stack Stack 7

D
20
Stack 8
9. Next element ‘/’ is an operator pop 2 operands from stack i.e. D and 20 and
evaluate the expression i.e. 20 / D = 20 /5 =4

Empty
Stack 9
10. Push the result (4) into the stack 20
Stack 10

11. End of expression, pop from stack thus the result is 20


Evaluate the following expression in postfix using a stack and show content of
stack after execution of each operation:
true false true NOT false true OR NOT AND OR AND

‘true’ is operand; pushed into stack

true
‘false’ is operand; pushed into stack
Stack 1
false
true
true Stack 2
false ‘true’ is operand; pushed into stack
true
Stack 2
. ‘NOT’ is a unary operator, thus one operand is poped i.e. true
false Evaluating NOT true = false; Pushing the result false into the stack
false
true
false
Stack 4 false
‘false’ is operand; pushed into stack
false
true
Stack 5
true
false
false
false ‘true’ is operand; pushed into stack
true
Stack 6
. ‘OR’ is a operator, pop two operands i.e. true, false Evaluating
true
false true OR false = true; Pushing the result true into the stack
false
true
false
Stack 7 ‘NOT’ is a unary operator, thus one operand is
false
false poped i.e. true Evaluating NOT true = false;
true Pushing the result false into the stack
Stack 8

false ‘AND’ is a operator, pop two operands i.e. false, false


false Evaluating false AND false = false; Pushing the result
true false into the stack
Stack 9
. ‘OR’ is a operator, pop two operands i.e. false, false Evaluating
false false OR false = false; Pushing the result true into the stack
true

Stack 10

‘AND’ is a operator, pop two operands i.e. false, false Evaluating


false false AND true = false; Pushing the result false into the stack
Stack 11

No more elements Pop from stack therefore the result is ‘false’


Evaluation of Prefix expression using stack

The prefix expression is also with out parenthesis. The prefix expression are
evaluated by the compiler using two stack:

* One stack (Symbol stack) for holding the symbols of the expression (All the
operators and operands/value ) of the expressions are considered symbols) and

* Another stack (Number stack or Operand-Value Stack) for holding the


numbers or value (i.e. the operands)
Evaluate the expression 5,6,2,+,*12,4,/,- in tabular form showing stack status
after every step.

Step Input Symbol/Element Stack Intermediate


Calculations Value
1 5 Push 5
2 6 Push 5,6
3 2 Push 5,6,2
4 + Pop 2 elements & evaluate 5 6+2=8
5 Push result (8) 5,8
6 * Pop 2 elements & evaluate empty 5*8=40
7 Push result (40) 40
.

Step Input Symbol/Element Stack Intermediate


Calculations Value
8 12 Push 40,12
9 4 Push 40,12,4
10 / Pop 2 elements & evaluate 40 12/4=3
11 Push result (3) 40,3
12 - Pop 2 elements & evaluate empty 40-3=37
13 Push result (37) 37
14 No more elements 37 (Result)
Queue:- Queue is a linear data structure implemented in FIFO- First In
First Out manner where insertion take place at the rear end and deletion
take place only front end.

1) Data can only be removed from the front end i.e. the element at the
front-end of the queue. The removal of element from queue is
technically called DEQUEUE operation.
2) A new data element can only be added to the rear of the queue. The
insertion of element in a queue is technically called ENQUEUE.
The queue is a dynamic data structure as it can grow (with increase in
number of elements) or shrink (with decrease in number of element). A
static data structure, on the other hand, is the one that has fixed size.
Other Queue Term:-

Peek:- Refer to inspecting the value at the queue’s front without


removing it. It is also referred as inspection.

Overflow:- Refer to situation (Error) when one tries to ENQUEUE an item


in queue that is full. This situation occur when the size of stack is fixed
and can not grow further.

Underflow:- Refer to situation (Error) when one tries to


DEQUEUE/delete an item from empty queue. That is, queue is currently
having no item and still one tries to DEQUEUE an item.
Example:-
i) Queue empty ii) enqueue ‘a’ iii) enqueue ‘b’ iv) enqueue ‘c’
v) dequeue vi) enqueue ‘d’ vii) enqueue ‘e’ viii) dequeue
ix) dequeue x) dequeue xi) dequeue

Solution
Front = Real =
i) Queue is empty (front=real=None) ii) enqueue ‘a’ (front=real=0)
0 1 2 3 0 1 2 3
a
f r
f r
iii) enquue ‘b’ (front=0 real=1) iv) enquue ‘c’ (front=0 real=2)
0 1 2 3 0 1 2 3
a b a b c
f r f r
v) dequeue (front=1 real=2) vi) enquue ‘d’ (front=1 real=3)
0 1 2 3 0 1 2 3
b c b c d
Remove a f r f r
vii) enquue ‘e’ (front=1 real=3) viii) dequeue (front=2 real=3)
0 1 2 3 0 1 2 3
b c d e d
OVERFLOW f r Remove b f r
ix) dequeue (front=3 real=3) x) dequeue (front= real= None)
0 1 2 3 0 1 2 3
d
Remove c f r f r Remove d

v) dequeue (front= real= None)


0 1 2 3
UNDERFLOW
fr
Type of Queue:-Circular queues are implemented in circular from rather than
straight line. These are used in programming languages that allows the fixed
size linear arrays of C/C++ as queues. In such queues, after some insertions
and deletions unutilized space lies in the beginning of the queue. To remove
such problems, circular queues are used.
Unutilized space 22 20
Front
25 8 7
9
Front Front 17
3 0 3 0 6
Rear
5 15
5 1 Rear 5 1
2 4
2 4 3
7 3 7 12
12 9
9 Rear
Occupied space (c) Circular queue (full to capacity)
(a) (b) Circular queue at any random at any random
Deque:-Deque (Double Ended Queue) are refined queues in which elements
can be added or deleted at either end but not in the middle. There are two
variations of deque - an input restricted deque and an output restricted deque.

• An Input Restricted Deque is a deque which allows insertions at only one


end but allowed deletions at both ends of the list.

• An Output Restricted Deque is a deque which allows deletion only one end
of the list but allowed insertions at both ends of list.
. Front Rear
r a d a r Add to rear-end only

Front Rear
Deletion from front-end r a d a r Deletion from rear-end
also possible possible
(a) Input Restricted Deque

Front Rear
Deletion from front end only r a d a r
Front Rear
Insertion at front-end Insertion at rear-end
possible r a d a r also possible

(b) Output Restricted Deque

You might also like