SlideShare a Scribd company logo
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
Jsaddam Hussain
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
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.
CIIT Atd.
 
Queue
QueueQueue
Queue
Ayaz Akhtar
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
V.V.Vanniaperumal College for Women
 
QUEUES
QUEUESQUEUES
QUEUES
Dr. Sindhia Lingaswamy
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
Dipayan Sarkar
 
Stack
StackStack
Stack
Seema Sharma
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
Sanjay Saha
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Shakila Mahjabin
 
Stacks
StacksStacks
Stacks
sardorbek mamazhanov
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
Bhavesh Sanghvi
 
DATA STRUCTURE - STACK
DATA STRUCTURE - STACKDATA STRUCTURE - STACK
DATA STRUCTURE - STACK
Devyani Chaudhari
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
Rajendran
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0
Zidny Nafan
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 

Similar to 6 chapter3 list_stackqueuepart3 (20)

10 -queues using array_07485555555510.ppt
10 -queues using array_07485555555510.ppt10 -queues using array_07485555555510.ppt
10 -queues using array_07485555555510.ppt
nailapp2023
 
Queues_0748555555555555555555555526.pptx
Queues_0748555555555555555555555526.pptxQueues_0748555555555555555555555526.pptx
Queues_0748555555555555555555555526.pptx
nailapp2023
 
queue (1).ppt queue notes and ppt in Data Structures
queue (1).ppt queue notes and ppt in Data Structuresqueue (1).ppt queue notes and ppt in Data Structures
queue (1).ppt queue notes and ppt in Data Structures
nisharaheja1986
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
Tribhuvan University
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
Teksify
 
The presention is about the queue data structure
The presention is about the queue data structureThe presention is about the queue data structure
The presention is about the queue data structure
gaurav77712
 
Lec-12, 13 Quees Array Implementation IN
Lec-12, 13 Quees Array Implementation INLec-12, 13 Quees Array Implementation IN
Lec-12, 13 Quees Array Implementation IN
Anil Yadav
 
Lec-12, 13 Quees -How to determine empty and full Queues?
Lec-12, 13 Quees -How to determine empty and full Queues?Lec-12, 13 Quees -How to determine empty and full Queues?
Lec-12, 13 Quees -How to determine empty and full Queues?
Anil Yadav
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
Victor Palmar
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
singhprpg
 
RPT_02_B_Queue presentation for FE students
RPT_02_B_Queue presentation for FE studentsRPT_02_B_Queue presentation for FE students
RPT_02_B_Queue presentation for FE students
AshishFamt
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
btechsmartclass
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
NuraMohamed9
 
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
abinathsabi
 
basics of queues
basics of queuesbasics of queues
basics of queues
sirmanohar
 
10 -queues using array_07485555555510.ppt
10 -queues using array_07485555555510.ppt10 -queues using array_07485555555510.ppt
10 -queues using array_07485555555510.ppt
nailapp2023
 
Queues_0748555555555555555555555526.pptx
Queues_0748555555555555555555555526.pptxQueues_0748555555555555555555555526.pptx
Queues_0748555555555555555555555526.pptx
nailapp2023
 
queue (1).ppt queue notes and ppt in Data Structures
queue (1).ppt queue notes and ppt in Data Structuresqueue (1).ppt queue notes and ppt in Data Structures
queue (1).ppt queue notes and ppt in Data Structures
nisharaheja1986
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
Teksify
 
The presention is about the queue data structure
The presention is about the queue data structureThe presention is about the queue data structure
The presention is about the queue data structure
gaurav77712
 
Lec-12, 13 Quees Array Implementation IN
Lec-12, 13 Quees Array Implementation INLec-12, 13 Quees Array Implementation IN
Lec-12, 13 Quees Array Implementation IN
Anil Yadav
 
Lec-12, 13 Quees -How to determine empty and full Queues?
Lec-12, 13 Quees -How to determine empty and full Queues?Lec-12, 13 Quees -How to determine empty and full Queues?
Lec-12, 13 Quees -How to determine empty and full Queues?
Anil Yadav
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
singhprpg
 
RPT_02_B_Queue presentation for FE students
RPT_02_B_Queue presentation for FE studentsRPT_02_B_Queue presentation for FE students
RPT_02_B_Queue presentation for FE students
AshishFamt
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
NuraMohamed9
 
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
abinathsabi
 
basics of queues
basics of queuesbasics of queues
basics of queues
sirmanohar
 

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
SSE_AndyLi
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic Functions
SSE_AndyLi
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDL
SSE_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 Logic
SSE_AndyLi
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
SSE_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 Systems
SSE_AndyLi
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
SSE_AndyLi
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
SSE_AndyLi
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
SSE_AndyLi
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
SSE_AndyLi
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
SSE_AndyLi
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
SSE_AndyLi
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
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 Circuits
SSE_AndyLi
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic Functions
SSE_AndyLi
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDL
SSE_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 Logic
SSE_AndyLi
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
SSE_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 Systems
SSE_AndyLi
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
SSE_AndyLi
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
SSE_AndyLi
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
SSE_AndyLi
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
SSE_AndyLi
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
SSE_AndyLi
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
SSE_AndyLi
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
SSE_AndyLi
 

Recently uploaded (20)

What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Download MathType Crack Version 2025???
Download MathType Crack  Version 2025???Download MathType Crack  Version 2025???
Download MathType Crack Version 2025???
Google
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
NYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdfNYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdf
AUGNYC
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Download MathType Crack Version 2025???
Download MathType Crack  Version 2025???Download MathType Crack  Version 2025???
Download MathType Crack Version 2025???
Google
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
NYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdfNYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdf
AUGNYC
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 

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.
  翻译: