7 Data Structure II
7 Data Structure II
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
Peek:- Refer to inspecting the value at the stack’s top with out removing
it. It is also referred as inspection.
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
i) Brackets or Parenthesis
ii) Exponentiation
iii) Multiplication or Division
iv) Addition or Subtraction
• 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
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
Stack 10
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
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:-
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
• 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