stack and queuecse.iitkgp.ac.in/~ppchak/course/pds/lect-28-stack-queue.pdf · 2013-10-02 · queue...
TRANSCRIPT
![Page 1: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/1.jpg)
Stack and QueueStack and QueueStack and Queue
1
Stack and Queue
![Page 2: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/2.jpg)
Stack
In
ABC
Data structure with Last-In First
Out
CB
In First-Out (LIFO) behavior
2
![Page 3: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/3.jpg)
Typical Operations
on Stack
isempty: determines if the stack has no elements
isfull: determines if the stack is full in case
of a bounded sized stack
top: returns the top element in the stacktop: returns the top element in the stack
push: inserts an element into the stack
pop: removes the top element from the stack
push is like inserting at the front of the list
pop is like deleting from the front of the list
Typical Operations Push
Pop
determines if the stack has no elements
determines if the stack is full in case
of a bounded sized stack
returns the top element in the stack
3
returns the top element in the stack
inserts an element into the stack
removes the top element from the stack
is like inserting at the front of the list
is like deleting from the front of the list
![Page 4: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/4.jpg)
Creating and Initializing a Stack
#define MAX_STACK_SIZE 100
typedef struct {
int key; /* just an example, can have
Declaration
int key; /* just an example, can have
any type of fields depending
on what is to be stored */
} element;
typedef struct {
element list[MAX_STACK_SIZE];
int top; /* index of the topmost element */
} stack;
Creating and Initializing a Stack
int key; /* just an example, can have
Create and Initialize
4
int key; /* just an example, can have
any type of fields depending
on what is to be stored */
element list[MAX_STACK_SIZE];
int top; /* index of the topmost element */
stack Z;
Z.top = -1;
![Page 5: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/5.jpg)
Operations
int isfull (stack *s)
{
if (s->top >=
MAX_STACK_SIZE –
return 1;
return 0;
}
int isempty (stack *s)
{
5
– 1) {
if (s->top == -1)
return 1;
return 0;
}
![Page 6: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/6.jpg)
Operations
element top( stack *s )
{
return s->list[s->top];
}
void push( stack *s, element e )
{
(s->top)++;
s->list[s->top] = e;
}
void pop( stack *s )
{
6
{
(s->top)--;
}
![Page 7: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/7.jpg)
Application: Parenthesis Matching
� Given a parenthesized expression, test whether the
expression is properly parenthesized
� Examples:
( )( { } [ ( { } { } ( ) ) ] ) ( )( { } [ ( { } { } ( ) ) ] )
( ){ [ ] is not proper
( { ) } is not proper
)([ ] is not proper
( [ ] ) ) is not proper
Application: Parenthesis Matching
Given a parenthesized expression, test whether the
expression is properly parenthesized
is proper
7
is proper
is not proper
is not proper
is not proper
is not proper
![Page 8: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/8.jpg)
� Approach:
�Whenever a left parenthesis is encountered, it is pushed in the stack
�Whenever a right parenthesis is �Whenever a right parenthesis is encountered, pop from stack and check if the parentheses match
�Works for multiple types of parentheses ( ), { }, [ ]
Whenever a left parenthesis is encountered, it is pushed in the stack
Whenever a right parenthesis is
8
Whenever a right parenthesis is encountered, pop from stack and check if the parentheses match
Works for multiple types of parentheses
![Page 9: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/9.jpg)
Parenthesis matchingwhile (not end of string) do
{
a = get_next_token();
if (a is ‘(‘ or ‘{‘ or ‘[‘) push (a);
if (a is ‘)’ or ‘}’ or ‘]’)
{{
if (is_stack_empty( ))
{ print (“Not well formed”); exit(); }
x = top();
pop();
if (a and x do not match)
{ print (“Not well formed”); exit(); }
}
}
if (not is_stack_empty( )) print (“Not well formed”);
Parenthesis matching
if (a is ‘(‘ or ‘{‘ or ‘[‘) push (a);
9
{ print (“Not well formed”); exit(); }
if (a and x do not match)
{ print (“Not well formed”); exit(); }
if (not is_stack_empty( )) print (“Not well formed”);
![Page 10: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/10.jpg)
fib (5)
fib (3)
Recursion can be
implemented as a stack
fib (3)
fib (1)
fib (2)fib (1) fib (2)
fib (0) fib (0)
fib (4)
Fibonacci recurrence:
fib(n) = 1 if n =0 or 1;
= fib(n – 2) + fib(n – 1)
otherwise;
implemented as a stack
10
fib (4)
fib (2) fib (3)
fib (1)
fib (1) fib (2)
fib (0)
fib (1)
![Page 11: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/11.jpg)
Fibonacci Recursion Stack
5 4
3
4
2
1
4
2
4
1
0
5 4 4 4 4
0 0 0 1 1
3
1
3 2
1
2
4 5 5 6
Fibonacci Recursion Stack
4
1
0
4
1
4 3
2
3
1
0
11
4 4 4 3 3
1 2 3 3 3
1
0
1
6 7 8
![Page 12: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/12.jpg)
Tower of Hanoi
A B
12
C
![Page 13: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/13.jpg)
Tower of Hanoi
A B
13
C
![Page 14: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/14.jpg)
Tower of Hanoi
A B
14
C
![Page 15: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/15.jpg)
Tower of Hanoi
A B
15
C
![Page 16: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/16.jpg)
Towers of Hanoi Function
void towers (int n, char from, char to, char aux) {
/* Base Condition */ if (n==1) {
printf (“Disk 1 : %c printf (“Disk 1 : %c return ;
}/* Recursive Condition */ towers (n-1, from, aux, to) ;printf (“Disk %d : %c towers (n-1, aux, to, from) ;
}
Towers of Hanoi Function
void towers (int n, char from, char to, char aux)
printf (“Disk 1 : %c -> %c \n”, from, to) ;
16
printf (“Disk 1 : %c -> %c \n”, from, to) ;
/* Recursive Condition */ 1, from, aux, to) ;
printf (“Disk %d : %c -> %c\n”, n, from, to) ;1, aux, to, from) ;
![Page 17: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/17.jpg)
TOH Recursion Stack
3,A,B,C
2,A,C,B
A to B
2,C,B,A
1,A,B,C
A to C
1,B,C,A
A to B
2,C,B,A3,A,B,C 2,C,B,A 2,C,B,A
1,B,C,A
A to B
2,C,B,A
B to C
A to B
2,C,B,A
A to B
2,C,B,A
TOH Recursion Stack
1,A,B,C
A to C
1,B,C,A
A to B
2,C,B,A
A to B
A to C
1,B,C,A
A to B
2,C,B,A
A to C
1,B,C,A
A to B
2,C,B,A
17
2,C,B,A 2,C,B,A 2,C,B,A
A to B
2,C,B,A 2,C,B,A
1,C,A,B
C to B
1,A,B,C
![Page 18: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/18.jpg)
Queue
In
Data structure with First-In First
In
AC B
Out
In First-Out (FIFO) behavior
18
AB
![Page 19: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/19.jpg)
Typical Operations
on Queue
isempty: determines if the queue is empty
isfull: determines if the queue is full
in case of a bounded size queue
front: returns the element at front of the queuefront: returns the element at front of the queue
enqueue: inserts an element at the rear
dequeue: removes the element in front
Typical Operations
determines if the queue is empty
determines if the queue is full
in case of a bounded size queue
returns the element at front of the queue
Enqueue
REAR
19
returns the element at front of the queue
inserts an element at the rear
removes the element in front
Dequeue
FRONT
![Page 20: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/20.jpg)
Possible Implementations
Linear Arrays:
(static/dynamicaly allocated)
front
Circular Arrays:
(static/dynamically allocated)
front rear
Can be implemented by a 1
array using modulus operations
Linked Lists: Use a linear
linked list with insert_rear
and delete_front operations
Possible Implementations
Circular Arrays:
(static/dynamically allocated)
20
Can be implemented by a 1-d
array using modulus operations
frontrear
![Page 21: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/21.jpg)
Circular Queue
[1]
[2]
[3] [4]
[0]
[5]
[6]
[7]
front=0rear=0
21
![Page 22: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/22.jpg)
Circular Queue
front=0
[1]
[2]
[3] [4]
[0]
[5]
[6]
[7]
front=0rear=0
front=0 [0]
[1]
[2]
[3]
[5]
[4]
[6]
[7]
rear = 4
After insertionof A, B, C, D
A
B
C D
22
![Page 23: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/23.jpg)
Circular Queue
front=0
[1]
[2]
[3] [4]
[0]
[5]
[6]
[7]
front=2
[1]
[2]
front=0rear=0
front=0 [0]
[1]
[2]
[3]
[5]
[4]
[6]
[7]
rear = 4
After insertionof A, B, C, D
A
B
C D
23
[0]
[3]
[5]
[4]
[6]
[7]
rear = 4
After deletion ofof A, B
C D
![Page 24: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/24.jpg)
front: index of queuerear: index of last element, unless rear = front
[2]
[3]
[5]
[4]
Queue Empty Condition: front == rear
Queue Full Condition: front == (rear + 1) % MAX_Q_SIZE
front=0rear=0
[0]
[1] [6]
[7]
Queue Empty
index of queue-head (always empty – why?)index of last element, unless rear = front
front=4rear = 3[4]
[2]
[3]
[5]
24
front == rear
front == (rear + 1) % MAX_Q_SIZE
Queue Full
[0]
[1] [6]
[7]
![Page 25: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/25.jpg)
Creating and Initializing a Circular
Queue
#define MAX_Q_SIZE 100
typedef struct {
int key; /* just an example, can have
Declaration
int key; /* just an example, can have
any type of fields depending
on what is to be stored */
} element;
typedef struct {
element list[MAX_Q_SIZE];
int front, rear;
} queue;
Creating and Initializing a Circular
int key; /* just an example, can have
Create and Initialize
25
int key; /* just an example, can have
any type of fields depending
on what is to be stored */
queue Q;
Q.front = 0;
Q.rear = 0;
![Page 26: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/26.jpg)
Operations
int isfull (queue *q)
{
if (q->front == ((q->rear + 1) %
MAX_Q_SIZE))MAX_Q_SIZE))
return 1;
return 0;
}
>rear + 1) %
MAX_Q_SIZE))
26
MAX_Q_SIZE))
int isempty (queue *q)
{
if (q->front == q->rear)
return 1;
return 0;
}
![Page 27: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/27.jpg)
Operations
void enqueue( queue *q, element e)
element front( queue *q )
{
return q->list[(q->front + 1) % MAX_Q_SIZE];
}
void enqueue( queue *q, element e)
{
q->rear = (q->rear + 1)%
MAX_Q_SIZE;
q->list[q->rear] = e;
}
void enqueue( queue *q, element e)
>front + 1) % MAX_Q_SIZE];
27
void enqueue( queue *q, element e)
void dequeue( queue *q ){
q-> front = (q-> front + 1)%
MAX_Q_SIZE;}
![Page 28: Stack and Queuecse.iitkgp.ac.in/~ppchak/course/pds/Lect-28-Stack-Queue.pdf · 2013-10-02 · Queue In Data structure with First-In First C B A Out-Out (FIFO) behavior 18 B A. Typical](https://reader030.vdocument.in/reader030/viewer/2022040915/5e8ce69dec9b376e740bcda3/html5/thumbnails/28.jpg)
Exercises
• Implement the Queue as a linked list.
• Implement a Priority Queueitems in an order (ascending/ descending) and has additional functions like has additional functions like remove_min
• Maintain a Doctor’s appointment list
Implement the Queue as a linked list.
Priority Queue which maintains the items in an order (ascending/ descending) and has additional functions like remove_max and
28
has additional functions like remove_max and
Maintain a Doctor’s appointment list