SlideShare a Scribd company logo
1 of 28
1
Stack and Queue
2
Stack
In Out
A
B
C C
B
Data structure with Last-In First-Out (LIFO) behavior
3
Typical Operations
on Stack Push
Pop
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 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
4
Creating and Initializing a Stack
#define MAX_STACK_SIZE 100
typedef struct {
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;
stack Z;
Z.top = -1;
Declaration
Create and Initialize
5
Operations
int isfull (stack *s)
{
if (s->top >=
MAX_STACK_SIZE – 1)
return 1;
return 0;
}
int isempty (stack *s)
{
if (s->top == -1)
return 1;
return 0;
}
6
Operations
void push( stack *s, element e )
{
(s->top)++;
s->list[s->top] = e;
}
void pop( stack *s )
{
(s->top)--;
}
element top( stack *s )
{
return s->list[s->top];
}
7
Application: Parenthesis Matching
 Given a parenthesized expression, test whether the
expression is properly parenthesized
 Examples:
( )( { } [ ( { } { } ( ) ) ] ) is proper
( ){ [ ] is not proper
( { ) } is not proper
)([ ] is not proper
( [ ] ) ) is not proper
8
 Approach:
Whenever a left parenthesis is
encountered, it is pushed in the stack
Whenever a right parenthesis is
encountered, pop from stack and check
if the parentheses match
Works for multiple types of parentheses
( ), { }, [ ]
9
Parenthesis matching
while (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”);
10
fib (5)
fib (3) fib (4)
fib (1)
fib (2)
fib (1) fib (2)
fib (0)
fib (3)
fib (1)
fib (1) fib (2)
fib (0)
fib (0) fib (1)
Fibonacci recurrence:
fib(n) = 1 if n =0 or 1;
= fib(n – 2) + fib(n – 1)
otherwise;
Recursion can be
implemented as a stack
11
Fibonacci Recursion Stack
5 4
3
4
2
1
4
2
4
1
0
4
1
4 3
2
3
1
0
0 0 0 1 1 2 3 3 3
3
1
3 2
1
2 1
0
1
4 5 5 6 6 7 8
12
Tower of Hanoi
A B C
13
Tower of Hanoi
A B C
14
Tower of Hanoi
A B C
15
Tower of Hanoi
A B C
16
Towers of Hanoi Function
void towers (int n, char from, char to, char aux)
{
/* Base Condition */
if (n==1) {
printf (“Disk 1 : %c -> %c n”, from, to) ;
return ;
}
/* Recursive Condition */
towers (n-1, from, aux, to) ;
printf (“Disk %d : %c -> %cn”, n, from, to) ;
towers (n-1, aux, to, from) ;
}
17
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,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
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 2,C,B,A
1,C,A,B
C to B
1,A,B,C
18
Queue
In
Out
A
C B
A
B
Data structure with First-In First-Out (FIFO) behavior
19
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 queue
enqueue: inserts an element at the rear
dequeue: removes the element in front
Enqueue
Dequeue
REAR
FRONT
20
Possible Implementations
Linear Arrays:
(static/dynamicaly allocated)
front rear
Circular Arrays:
(static/dynamically allocated)
Can be implemented by a 1-d
array using modulus operations
front
rear
Linked Lists: Use a linear
linked list with insert_rear
and delete_front operations
21
Circular Queue
[1]
[2]
[3] [4]
[0]
[5]
[6]
[7]
front=0
rear=0
22
Circular Queue
front=0 [0]
[1]
[2]
[3]
[5]
[4]
[6]
[7]
rear = 4
After insertion
of A, B, C, D
A
B
C D
[1]
[2]
[3] [4]
[0]
[5]
[6]
[7]
front=0
rear=0
23
Circular Queue
front=0 [0]
[1]
[2]
[3]
[5]
[4]
[6]
[7]
rear = 4
After insertion
of A, B, C, D
A
B
C D
front=2
[0]
[1]
[2]
[3]
[5]
[4]
[6]
[7]
rear = 4
After deletion of
of A, B
C D
[1]
[2]
[3] [4]
[0]
[5]
[6]
[7]
front=0
rear=0
24
front: index of queue-head (always empty – why?)
rear: index of last element, unless rear = front
Queue Empty Condition: front == rear
Queue Full Condition: front == (rear + 1) % MAX_Q_SIZE
front=0
rear=0
[0]
[1]
[2]
[3]
[5]
[4]
[6]
[7]
Queue Empty
front=4
Queue Full
rear = 3
[4]
[0]
[1]
[2]
[3]
[5]
[6]
[7]
25
Creating and Initializing a Circular
Queue
#define MAX_Q_SIZE 100
typedef struct {
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;
queue Q;
Q.front = 0;
Q.rear = 0;
Declaration
Create and Initialize
26
Operations
int isfull (queue *q)
{
if (q->front == ((q->rear + 1) %
MAX_Q_SIZE))
return 1;
return 0;
}
int isempty (queue *q)
{
if (q->front == q->rear)
return 1;
return 0;
}
27
Operations
void enqueue( queue *q, element e)
{
q->rear = (q->rear + 1)%
MAX_Q_SIZE;
q->list[q->rear] = e;
}
void dequeue( queue *q )
{
q-> front =
(q-> front + 1)%
MAX_Q_SIZE;
}
element front( queue *q )
{
return q->list[(q->front + 1) % MAX_Q_SIZE];
}
28
Exercises
• Implement the Queue as a linked list.
• Implement a Priority Queue which maintains the
items in an order (ascending/ descending) and
has additional functions like remove_max and
remove_min
• Maintain a Doctor’s appointment list

More Related Content

Similar to Lect-28-Stack-Queue.ppt

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptxsinghprpg
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 

Similar to Lect-28-Stack-Queue.ppt (20)

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack
StackStack
Stack
 
04 stacks
04 stacks04 stacks
04 stacks
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 

Lect-28-Stack-Queue.ppt

  • 2. 2 Stack In Out A B C C B Data structure with Last-In First-Out (LIFO) behavior
  • 3. 3 Typical Operations on Stack Push Pop 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 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
  • 4. 4 Creating and Initializing a Stack #define MAX_STACK_SIZE 100 typedef struct { 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; stack Z; Z.top = -1; Declaration Create and Initialize
  • 5. 5 Operations int isfull (stack *s) { if (s->top >= MAX_STACK_SIZE – 1) return 1; return 0; } int isempty (stack *s) { if (s->top == -1) return 1; return 0; }
  • 6. 6 Operations void push( stack *s, element e ) { (s->top)++; s->list[s->top] = e; } void pop( stack *s ) { (s->top)--; } element top( stack *s ) { return s->list[s->top]; }
  • 7. 7 Application: Parenthesis Matching  Given a parenthesized expression, test whether the expression is properly parenthesized  Examples: ( )( { } [ ( { } { } ( ) ) ] ) is proper ( ){ [ ] is not proper ( { ) } is not proper )([ ] is not proper ( [ ] ) ) is not proper
  • 8. 8  Approach: Whenever a left parenthesis is encountered, it is pushed in the stack Whenever a right parenthesis is encountered, pop from stack and check if the parentheses match Works for multiple types of parentheses ( ), { }, [ ]
  • 9. 9 Parenthesis matching while (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”);
  • 10. 10 fib (5) fib (3) fib (4) fib (1) fib (2) fib (1) fib (2) fib (0) fib (3) fib (1) fib (1) fib (2) fib (0) fib (0) fib (1) Fibonacci recurrence: fib(n) = 1 if n =0 or 1; = fib(n – 2) + fib(n – 1) otherwise; Recursion can be implemented as a stack
  • 11. 11 Fibonacci Recursion Stack 5 4 3 4 2 1 4 2 4 1 0 4 1 4 3 2 3 1 0 0 0 0 1 1 2 3 3 3 3 1 3 2 1 2 1 0 1 4 5 5 6 6 7 8
  • 16. 16 Towers of Hanoi Function void towers (int n, char from, char to, char aux) { /* Base Condition */ if (n==1) { printf (“Disk 1 : %c -> %c n”, from, to) ; return ; } /* Recursive Condition */ towers (n-1, from, aux, to) ; printf (“Disk %d : %c -> %cn”, n, from, to) ; towers (n-1, aux, to, from) ; }
  • 17. 17 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,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 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 2,C,B,A 1,C,A,B C to B 1,A,B,C
  • 18. 18 Queue In Out A C B A B Data structure with First-In First-Out (FIFO) behavior
  • 19. 19 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 queue enqueue: inserts an element at the rear dequeue: removes the element in front Enqueue Dequeue REAR FRONT
  • 20. 20 Possible Implementations Linear Arrays: (static/dynamicaly allocated) front rear Circular Arrays: (static/dynamically allocated) Can be implemented by a 1-d array using modulus operations front rear Linked Lists: Use a linear linked list with insert_rear and delete_front operations
  • 22. 22 Circular Queue front=0 [0] [1] [2] [3] [5] [4] [6] [7] rear = 4 After insertion of A, B, C, D A B C D [1] [2] [3] [4] [0] [5] [6] [7] front=0 rear=0
  • 23. 23 Circular Queue front=0 [0] [1] [2] [3] [5] [4] [6] [7] rear = 4 After insertion of A, B, C, D A B C D front=2 [0] [1] [2] [3] [5] [4] [6] [7] rear = 4 After deletion of of A, B C D [1] [2] [3] [4] [0] [5] [6] [7] front=0 rear=0
  • 24. 24 front: index of queue-head (always empty – why?) rear: index of last element, unless rear = front Queue Empty Condition: front == rear Queue Full Condition: front == (rear + 1) % MAX_Q_SIZE front=0 rear=0 [0] [1] [2] [3] [5] [4] [6] [7] Queue Empty front=4 Queue Full rear = 3 [4] [0] [1] [2] [3] [5] [6] [7]
  • 25. 25 Creating and Initializing a Circular Queue #define MAX_Q_SIZE 100 typedef struct { 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; queue Q; Q.front = 0; Q.rear = 0; Declaration Create and Initialize
  • 26. 26 Operations int isfull (queue *q) { if (q->front == ((q->rear + 1) % MAX_Q_SIZE)) return 1; return 0; } int isempty (queue *q) { if (q->front == q->rear) return 1; return 0; }
  • 27. 27 Operations void enqueue( queue *q, element e) { q->rear = (q->rear + 1)% MAX_Q_SIZE; q->list[q->rear] = e; } void dequeue( queue *q ) { q-> front = (q-> front + 1)% MAX_Q_SIZE; } element front( queue *q ) { return q->list[(q->front + 1) % MAX_Q_SIZE]; }
  • 28. 28 Exercises • Implement the Queue as a linked list. • Implement a Priority Queue which maintains the items in an order (ascending/ descending) and has additional functions like remove_max and remove_min • Maintain a Doctor’s appointment list