SlideShare a Scribd company logo
1 of 12
Download to read offline
Chapter 3: Lists, Stacks, and
Queues - III
Text: Read Weiss, §3.7
1
The Queue ADT
• Like stacks, queues are lists. With a queue,
however, insertion is done at one end, whereas
deletion is performed at the other end.
• The basic operations on a queue (FIFO list) are
enqueue, which inserts an element at the end of
the list (called the rear), and dequeue, which
deletes (and returns) the element at the start of
the list (known as the front).
2
Array Implementation of
Queues - I
• As with stacks, both array and linked list
(trivial) implementations give O(1) running
times. Let’s discuss circular array
implementation.
• An array to store elements, Array, and
the positions Front and Rear to
represent the ends of the queue are kept.
• We also keep track of the number of
elements: Size.
3
4
Array Implementation of
Queues – Problem
5 2 7 1
Front Rear
• To enqueue an element X, increment Size and Rear and
set Array[Rear]=X
• To dequeue an element, set the return value to
Array[Front], decrement Size and then increment
Front.
• Assume after several enqueue operations, the Rear is at the
last index position. Assume also that some elements, in the
meantime, have been dequeued to make up room. The next
enqueue would fail, although there would be free slots.
• The simple solution is whenever Front or Rear gets to the
end of the Array, it is wrapped around to the beginning
(hence the name circular array).
5
Array Implementation of
Queues – Problem Solved
2 4
Front Rear
1 2 4
FrontRear
1 3 2 4
FrontRear
initially
After enqueue(1)
After enqueue(3)
6
Array Implementation of
Queues – Example
1 3 2 4
FrontRear
After dequeue 2
1 3 2 4
Front Rear
After dequeue 4
1 3 2 4
Front Rear
After dequeue 1
1 3 2 4
FrontRear
After dequeue 3
and queue is empty
• If Size is not kept, queue is empty when Rear=Front-1. In that case,
queue is full when there are Capacity-1 items. Why? Only Capacity
different sizes can be differentiated and one of these is 0.
7
Circular Array Implementation of Queues – Sample Code I
typedef int ElementType;
#define MinQueueSize (5)
struct QueueRecord {
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
};
typedef struct QueueRecord *Queue;
int IsEmpty( Queue Q ) {
return Q->Size == 0;
}
int IsFull( Queue Q ) {
return Q->Size == Q->Capacity;
}
/* MakeEmpty is invoked */
/* after CreateQueue */
/* on the next slide */
/* is called */
void MakeEmpty(Queue Q) {
Q->Size = 0;
Q->Front = 1;
Q->Rear = 0;
}
void DisposeQueue(Queue Q) {
if( Q != NULL ) {
free( Q->Array );
free( Q );
}
}
8
Circular Array Implementation of Queues – Sample Code II
Queue CreateQueue(int MaxElements) {
Queue Q;
if(MaxElements<MinQueueSize)
Error("Queue size is too small”);
Q = malloc(sizeof(struct QueueRecord));
if(Q == NULL)
FatalError("Out of space!!!”);
Q->Array = malloc(sizeof(ElementType)*MaxElements);
if( Q->Array == NULL )
FatalError("Out of space!!!”);
Q->Capacity = MaxElements;
MakeEmpty( Q );
return Q;
}
9
Circular Array Implementation of Queues – Sample Code III
static int Succ(int Value, Queue Q) {
if( ++Value == Q->Capacity )
Value = 0;
return Value;
}
void Enqueue( ElementType X, Queue Q ) {
if( IsFull( Q ) )
Error( "Full queue" );
else {
Q->Size++;
Q->Rear = Succ( Q->Rear, Q );
Q->Array[ Q->Rear ] = X;
}
}
ElementType Front( Queue Q ) {
if( !IsEmpty( Q ) )
return Q->Array[ Q->Front ];
Error( "Empty queue" );
return 0; /* Return value used to avoid warning */
}
10
Circular Array Implementation of Queues – Sample Code IV
void Dequeue( Queue Q ) {
if( IsEmpty( Q ) )
Error( "Empty queue" );
else {
Q->Size--;
Q->Front = Succ( Q->Front, Q );
}
}
ElementType FrontAndDequeue( Queue Q ) {
ElementType X = 0;
if( IsEmpty( Q ) )
Error( "Empty queue" );
else {
Q->Size--;
X = Q->Array[ Q->Front ];
Q->Front = Succ( Q->Front, Q );
}
return X;
}
Applications of Queues - I
11
• When jobs are submitted to a printer, they
are arranged in order of arrival.
• Virtually every real-life line is (supposed to
be) a queue.
• Users on other machines are given access
to a file server on a first-come first-served
basis.
• Calls to large companies are generally
placed on a queue when all operators are
busy.
12
Applications of Queues - II
• A whole branch of mathematics, known as
queueing theory, deals with computing,
probabilistically, how long users expect to
wait on a line, how long the line gets, and
other such questions. The answer
depends on how frequently users arrive to
the line and how long it takes to process a
user once the user is served. Both of
these parameters are given as probability
distribution functions.

More Related Content

What's hot (20)

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Priority queues
Priority queuesPriority queues
Priority queues
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Queue
QueueQueue
Queue
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
QUEUES
QUEUESQUEUES
QUEUES
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stack
StackStack
Stack
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stacks
StacksStacks
Stacks
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
DATA STRUCTURE - STACK
DATA STRUCTURE - STACKDATA STRUCTURE - STACK
DATA STRUCTURE - STACK
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 

Similar to 6 chapter3 list_stackqueuepart3

Similar to 6 chapter3 list_stackqueuepart3 (20)

LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Queue
QueueQueue
Queue
 
Queues
QueuesQueues
Queues
 
Queue
QueueQueue
Queue
 
Data structure lecture 9
Data structure lecture 9Data structure lecture 9
Data structure lecture 9
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 

More from SSE_AndyLi

Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsChapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsSSE_AndyLi
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsSSE_AndyLi
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDLSSE_AndyLi
 
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicChapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicSSE_AndyLi
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesSSE_AndyLi
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsChapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsSSE_AndyLi
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mstSSE_AndyLi
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpathSSE_AndyLi
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queuesSSE_AndyLi
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avlSSE_AndyLi
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2SSE_AndyLi
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1SSE_AndyLi
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2SSE_AndyLi
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1SSE_AndyLi
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introductionSSE_AndyLi
 

More from SSE_AndyLi (17)

Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsChapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic Functions
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDL
 
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicChapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational Logic
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsChapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital Systems
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 

Recently uploaded

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

6 chapter3 list_stackqueuepart3

  • 1. Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1
  • 2. The Queue ADT • Like stacks, queues are lists. With a queue, however, insertion is done at one end, whereas deletion is performed at the other end. • The basic operations on a queue (FIFO list) are enqueue, which inserts an element at the end of the list (called the rear), and dequeue, which deletes (and returns) the element at the start of the list (known as the front). 2
  • 3. Array Implementation of Queues - I • As with stacks, both array and linked list (trivial) implementations give O(1) running times. Let’s discuss circular array implementation. • An array to store elements, Array, and the positions Front and Rear to represent the ends of the queue are kept. • We also keep track of the number of elements: Size. 3
  • 4. 4 Array Implementation of Queues – Problem 5 2 7 1 Front Rear • To enqueue an element X, increment Size and Rear and set Array[Rear]=X • To dequeue an element, set the return value to Array[Front], decrement Size and then increment Front. • Assume after several enqueue operations, the Rear is at the last index position. Assume also that some elements, in the meantime, have been dequeued to make up room. The next enqueue would fail, although there would be free slots.
  • 5. • The simple solution is whenever Front or Rear gets to the end of the Array, it is wrapped around to the beginning (hence the name circular array). 5 Array Implementation of Queues – Problem Solved 2 4 Front Rear 1 2 4 FrontRear 1 3 2 4 FrontRear initially After enqueue(1) After enqueue(3)
  • 6. 6 Array Implementation of Queues – Example 1 3 2 4 FrontRear After dequeue 2 1 3 2 4 Front Rear After dequeue 4 1 3 2 4 Front Rear After dequeue 1 1 3 2 4 FrontRear After dequeue 3 and queue is empty • If Size is not kept, queue is empty when Rear=Front-1. In that case, queue is full when there are Capacity-1 items. Why? Only Capacity different sizes can be differentiated and one of these is 0.
  • 7. 7 Circular Array Implementation of Queues – Sample Code I typedef int ElementType; #define MinQueueSize (5) struct QueueRecord { int Capacity; int Front; int Rear; int Size; ElementType *Array; }; typedef struct QueueRecord *Queue; int IsEmpty( Queue Q ) { return Q->Size == 0; } int IsFull( Queue Q ) { return Q->Size == Q->Capacity; } /* MakeEmpty is invoked */ /* after CreateQueue */ /* on the next slide */ /* is called */ void MakeEmpty(Queue Q) { Q->Size = 0; Q->Front = 1; Q->Rear = 0; } void DisposeQueue(Queue Q) { if( Q != NULL ) { free( Q->Array ); free( Q ); } }
  • 8. 8 Circular Array Implementation of Queues – Sample Code II Queue CreateQueue(int MaxElements) { Queue Q; if(MaxElements<MinQueueSize) Error("Queue size is too small”); Q = malloc(sizeof(struct QueueRecord)); if(Q == NULL) FatalError("Out of space!!!”); Q->Array = malloc(sizeof(ElementType)*MaxElements); if( Q->Array == NULL ) FatalError("Out of space!!!”); Q->Capacity = MaxElements; MakeEmpty( Q ); return Q; }
  • 9. 9 Circular Array Implementation of Queues – Sample Code III static int Succ(int Value, Queue Q) { if( ++Value == Q->Capacity ) Value = 0; return Value; } void Enqueue( ElementType X, Queue Q ) { if( IsFull( Q ) ) Error( "Full queue" ); else { Q->Size++; Q->Rear = Succ( Q->Rear, Q ); Q->Array[ Q->Rear ] = X; } } ElementType Front( Queue Q ) { if( !IsEmpty( Q ) ) return Q->Array[ Q->Front ]; Error( "Empty queue" ); return 0; /* Return value used to avoid warning */ }
  • 10. 10 Circular Array Implementation of Queues – Sample Code IV void Dequeue( Queue Q ) { if( IsEmpty( Q ) ) Error( "Empty queue" ); else { Q->Size--; Q->Front = Succ( Q->Front, Q ); } } ElementType FrontAndDequeue( Queue Q ) { ElementType X = 0; if( IsEmpty( Q ) ) Error( "Empty queue" ); else { Q->Size--; X = Q->Array[ Q->Front ]; Q->Front = Succ( Q->Front, Q ); } return X; }
  • 11. Applications of Queues - I 11 • When jobs are submitted to a printer, they are arranged in order of arrival. • Virtually every real-life line is (supposed to be) a queue. • Users on other machines are given access to a file server on a first-come first-served basis. • Calls to large companies are generally placed on a queue when all operators are busy.
  • 12. 12 Applications of Queues - II • A whole branch of mathematics, known as queueing theory, deals with computing, probabilistically, how long users expect to wait on a line, how long the line gets, and other such questions. The answer depends on how frequently users arrive to the line and how long it takes to process a user once the user is served. Both of these parameters are given as probability distribution functions.