Stacks and Queues
Stacks and Queues
1/50
STACK
2/50
STACK
• A stack is an ordered list in which insertions and
deletions are made at one end called the top.
• A stack is also known as a Last-In-First-Out (LIFO)
list. The last element inserted is the first one to be
removed
3/50
STACK
4/50
STACK Operations
push(object)
– Adds the object to the top of the stack; the item
pushed is also returned as the value of push
object = pop()
– Removes the object at the top of the stack and
returns it
5/50
STACK Operation - PUSH
6/50
STACK Operation - POP
7/50
Applications of Stacks
• Parsing
• Recursive Function
• Calling Function
• Expression Evaluation
• Expression Conversion
• Infix to Postfix
• Infix to Prefix
• Postfix to Infix
• Prefix to Infix
8/50
STACK implementation
• Arrays or
• Linked lists.
9/50
Implementing Stacks : Array
• Advantages
– best performance
• Disadvantage
– fixed size
• Basic implementation
– initially empty array
– field to record where the next data gets placed into
– if array is full, push() returns false
• otherwise adds it into the correct spot
– if array is empty, pop() returns null
• otherwise removes the next item in the stack
10/50
STACK Operation - PUSH
Algorithm PUSH_A ( ITEM )
Input : The new item ITEM to be pushed onto it
Output : A stack with newly pushed ITEM at the TOP position
Data Structure : An array A with TOP as the pointer
Steps :
1. Top = -1
2. If TOP >= SIZE-1 then
1. Print “Stack is full”
3. Else
1. TOP = TOP+1
2. A[TOP]=ITEM
4. Endif
5. Stop
11/50
STACK Operation - POP
Algorithm POP_A ( )
Input : A stack with elements
Output : Removes an ITEM from the top of the stack if it is not
empty
Data Structure : An array A with TOP as the pointer
Steps :
1. If TOP < 0
1. Print “Stack is empty”
2. Else
1. ITEM = A[ TOP ]
2. TOP = TOP-1
3. Endif
4. Stop
12/50
Implementing Stacks : Linked Lists
head
a1 a2 a3 a4
13/50
STACK - PUSH
Algorithm PUSH_L (ITEM)
Input : The new item ITEM to be pushed onto it
Output : A single linked list with a newly inserted node with data
content ITEM
Data Structure : singly linked list, header : STACK_HEAD
Steps :
1. New = GETNODE( NODE )
2. New.DATA = ITEM
3. New.LINK = STACK_HEAD
4. STACK_HEAD = New
5. Stop
14/50
STACK - POP
Algorithm POP_L ( )
Input : The new item ITEM to be pushed onto it
Output : A single linked list with a newly inserted node with data content ITEM
Data Structure : single linked list, header : STACK_HEAD
Steps :
1. If STACK_HEAD = NULL
1. Print “Stack is empty”
2. Exit
2. Else
1. ITEM = STACK_HEAD.ITEM
2. Ptr = STACK_HEAD
3. STACK_HEAD = STACK_HEAD.LINK
4. DELETE (Ptr)
3. Endif
4. Stop
15/50
Why postfix representation of the expression?
16/50
Algorithm to Convert Infix Expression to Postfix using Stack
Step 3 : Else,
17/50
Algorithm to Convert Infix Expression to Postfix using Stack
Step 3.2 : Else, Pop all the operators from the stack which
are greater than or equal to in precedence than that of the
scanned operator. After doing that Push the scanned
operator to the stack. (If you encounter parenthesis while
popping then stop there and push the scanned operator in
the stack.)
18/50
Algorithm to Convert Infix Expression to Postfix using Stack
Stack : + Stack : + /
Output : 3 Output : 3 4 5 *
Stack : + Stack : + /
Output : 3 4 Output : 3 4 5 * 6
Stack : + * Stack :
Output : 3 4 Output : 3 4 5 * 6 / +
20/50
Convert an expression, I = ((6 + 2) * 5 – 8 / 4)
Character scanned Status of Stack Postfix expression ‘P’
( (
( ((
6 (( 6
+ ((+ 6
2 ((+ 62
) ( 62+
* (* 62+
5 (* 62+5
- (- 62+5*
8 (- 62+5*8
/ (- / 62+5*8
4 (- / 62+5*84
) (- 62+5*84/
( 62+5*84/-
21/50
Evaluate a postfix expression
1. Read the tokens from the postfix string one at a time
from left to right.
2. Initialize an empty stack
3. If the token is an operand, push the operand into the
stack
4. If the token is an operator, then pop the top two
elements from the stack and apply the operator on the
poped out elements. The result of this operation is
pushed back into the stack.
5. After all the tokens are read, only one element is
present in the stack and that is the result.
22/50
Evaluate a postfix expression
Infix is 3*4 + 2*5
Postfix is 3 4 * 2 5 * +
12 2 5 * + 2 5 * is replaced by 10
12 10 + 12 10 + is replaced by 22
22
23/50
STACK
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
24/50
Evaluate a postfix expression
Evaluate the following postfix expression using a stack:
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
25/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 1 onto the stack
1
26/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 2 onto the stack
2
1
27/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 3 onto the stack
3
2
1
28/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop 3 and 2 and push 2 + 3 = 5
5
1
29/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 4 onto the stack
4
5
1
30/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 5 onto the stack
5
4
5
1
31/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 6 onto the stack
6
5
4
5
1
32/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop 6 and 5 and push 5 × 6 = 30
30
4
5
1
33/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop 30 and 4 and push 4 – 30 = –26
–26
5
1
34/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 7 onto the stack
7
–26
5
1
35/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop 7 and –26 and push –26 × 7 = –182
–182
5
1
36/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop –182 and 5 and push –182 + 5 = –177
–177
1
37/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop –177 and 1 and push 1 – (–177) = 178
178
38/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 8 onto the stack
8
178
39/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Push 9 onto the stack
9
8
178
40/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop 9 and 8 and push 8 × 9 = 72
72
178
41/50
Evaluate a postfix expression
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
Pop 72 and 178 and push 178 + 72 = 250
250
42/50
Evaluate a postfix expression
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value : 250
43/50
Queues
Linear list.
45/50
Queue
Alternative terms may be used for the four
operations on a queue, including:
46/50
Circular Queue
In a Circular queue the last element is connected to the
first element of the queue forming a circle.
47/50
Circular Queue
48/50
Circular Queue
49/50
Circular Queue : Insert & Delete
50/50
Circular Queue : Insert & Delete
51/50
Circular Queue : Insert & Delete
52/50
Circular Queue : Insert & Delete
53/50
Circular Queue : Insert & Delete
54/50