0% found this document useful (0 votes)
49 views54 pages

Stacks and Queues

The document discusses stacks and queues, including definitions of stacks as LIFO lists and common stack operations like push and pop. It provides examples of stack implementations using arrays and linked lists and applications of stacks like expression evaluation. Key algorithms covered include converting infix to postfix notation and evaluating postfix expressions using a stack.

Uploaded by

aryakabir22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views54 pages

Stacks and Queues

The document discusses stacks and queues, including definitions of stacks as LIFO lists and common stack operations like push and pop. It provides examples of stack implementations using arrays and linked lists and applications of stacks like expression evaluation. Key algorithms covered include converting infix to postfix notation and evaluating postfix expressions using a stack.

Uploaded by

aryakabir22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

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

• If the elements A, B, C, D, E are added to the stack, in


that order, then E is the first element to be deleted from
the 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

• Stacks structures are usually implemented


using

• 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

lst Top of Stack = Front of Linked-List


LinkedListItr

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?

• Infix expressions are readable and solvable by


humans because of easily distinguishable order of
operators, but compiler doesn't have integrated order
of operators.
• Hence to solve the Infix Expression compiler will scan
the expression multiple times to solve the sub-
expressions in expressions orderly which is very
inefficient.
• To avoid this traversing, Infix expressions are
converted to Postfix expression before evaluation.

16/50
Algorithm to Convert Infix Expression to Postfix using Stack

Step 1 : Scan the Infix Expression from left to right.

Step 2 : If the scanned character is an operand, append it


with final Infix to Postfix string.

Step 3 : Else,

Step 3.1 : If the precedence order of the scanned(incoming)


operator is greater than the precedence order of the
operator in the stack (or the stack is empty or the stack
contains a ‘(‘ or ‘[‘ or ‘{‘), push it on stack.

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.)

Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push


it to the stack.

18/50
Algorithm to Convert Infix Expression to Postfix using Stack

Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop


the stack and and output it until a ‘(‘ or ‘[‘ or ‘{‘
respectively is encountered, and discard both the
parenthesis.

Step 6 : Repeat steps 2-6 until infix expression is scanned.

Step 7 : Print the output

Step 8 : Pop and output from the stack until it is not


empty.
19/50
Example : Convert Infix Expression to Postfix using Stack
Infix Expression : 3+4*5/6
Stack : Stack : + *
Output : 3 Output : 3 4 5

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 * +

34*25*+ * is the first operator 3 4 * is replaced by 12

12 2 5 * + 2 5 * is replaced by 10

12 10 + 12 10 + is replaced by 22

22

23/50
STACK

Evaluate the following postfix


expression using a 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.

One end is called front.

Other end is called rear.

Additions are done at the rear only.

Removals are made from the front only.


44/50
Queue
First-In–First-Out (FIFO) or
First Come First Serve (FCFS) data structure

45/50
Queue
Alternative terms may be used for the four
operations on a queue, including:

Enqueue(Push), Dequeue(Pop), Head(Front),


Tail (Back)

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

You might also like