SlideShare uma empresa Scribd logo
1 de 21
Introduction To 
Algorithm
Abstract Data Type 
Stack & Queue is an example of ADT 
An array is not ADT.
Using a Queue 
Several example 
applications of queues are 
given in that Slides. 
This presentation describes 
the queue operations and 
two ways to implement a 
queue. 
Data Structures 
and Other Objects 
Using C++
What is Queue? 
A QUEUE IS A CONTAINER IN WHICH INSERTIONS 
ARE MADE ONLY AT THE BACK DELETIONS, 
RETRIEVALS, AND MODIFICATIONS ARE MADE 
ONLY AT THE FRONT.
The Queue Operations 
A queue is like a line of 
people waiting for a 
bank teller. The queue 
has a front and a rear. 
$ $ 
Front 
Rear
EnQueue Operations 
New people must enter the queue at 
the rear. The C++ queue class calls 
this a push, although it is usually 
called an enqueue operation. 
$ $ 
Front 
Rear
DeQueue Operations 
 When an item is taken from the queue, 
it always comes from the front. The C++ 
queue calls this a pop, although it is 
usually called a dequeue operation. 
$ $ 
Front 
Rear
Array Implementation 
A queue can be implemented with an array, as 
shown here. For example, this queue contains 
the integers 4 (at the front), 8 and 6 (at the rear). 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6 
AAnn aarrrraayy ooff iinntteeggeerrss ttoo 
iimmpplleemmeenntt aa qquueeuuee ooff 
iinntteeggeerrss 
WWee ddoonn''tt ccaarree wwhhaatt''ss iinn 
tthhiiss ppaarrtt ooff tthhee aarrrraayy..
Array Implementation 
The efficient implementation also keeps 
track of the number of items in the 
queue and the index of the first element 
(at the front of the queue), the last 
element (at the rear). 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6 
3 ssiizzee 
0 ffiirrsstt 
2 llaasstt
A Dequeue Operation 
When an element leaves the queue, 
size is decremented, and first 
changes, too. 
2 ssiizzee 
1 ffiirrsstt 
2 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6
An Enqueue Operation 
When an element enters the queue, 
size is incremented, and last changes, 
too. 
3 ssiizzee 
1 ffiirrsstt 
3 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
8 6 2
At the End of the Array 
There is special behaviour at the end 
of the array. For example, suppose we 
want to add a new element to this 
queue, where the last index is [5]: 
2 
3 ssiizzee 
3 ffiirrsstt 
5 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 
5 6 1
At the End of the Array 
The new element goes at the front of 
the array (if that spot isn’t already 
used): 
4 ssiizzee 
3 ffiirrsstt 
0 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 
4 2 6 1
Array Queue Reviews 
 Easy to implement. 
 But it has a limited capacity with a fixed array 
 Special behaviour is needed when the rear 
reaches the end of the array. 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6 
3 ssiizzee 
0 ffiirrsstt 
2 llaasstt
Linked List Implementation 
A queue can also be 
implemented with a linked 
list with both a head and a 
tail pointer. 
null 
10 
15 
7 
13 
Head-ptr 
Tail-ptr
Linked List Implementation 
Which end do you think is the 
front of the queue? Why ? 
10 
15 
7 
13 
Head-ptr 
Tail-ptr 
null
Linked List Implementation 
The head-ptr points to the 
front of the list. 
Because it is harder to 
remove items from the tail 
of the list. 
Rear 
Front 
10 
15 
7 
13 
Head-ptr 
Tail-ptr 
null
The Queue Class 
template <class Item> 
class queue<Item> 
{ 
public: 
queue( ); 
void push(const Item& entry); 
void pop( ); 
bool empty( ) const; 
Item front( ) const; 
} 
 The C++ standard 
template library has a 
queue template class. 
 The template parameter 
is the type of the items 
that can be put in the 
queue.
Summary 
QQuueeuueess hhaavvee mmaannyy aapppplliiccaattiioonnss.. 
 IItteemmss eenntteerr aa qquueeuuee aatt tthhee rreeaarr aanndd 
lleeaavvee aa qquueeuuee aatt tthhee ffrroonntt.. 
QQuueeuueess ccaann bbee iimmpplleemmeenntteedd uussiinngg aann 
aarrrraayy oorr uussiinngg aa lliinnkkeedd lliisstt..
Daily Life Examples 
There's a queue of questions 
that you've asked in exam, all 
waiting for you to accept 
answers for them. 
Waiting in a queue in any 
bank for paying bills, depositing 
cash OR cheques.
Thank You 
THE END

Mais conteúdo relacionado

Mais procurados

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 

Mais procurados (20)

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
Heap sort
Heap sortHeap sort
Heap sort
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Stack
StackStack
Stack
 

Destaque

Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
grahamwell
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
Tareq Hasan
 
Comparative income statement
Comparative income statementComparative income statement
Comparative income statement
Adil Shaikh
 

Destaque (19)

Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
stack
stackstack
stack
 
Priority queue
Priority queuePriority queue
Priority queue
 
Linked List
Linked ListLinked List
Linked List
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Comparative income statement
Comparative income statementComparative income statement
Comparative income statement
 

Semelhante a Queue

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
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
ecomputernotes
 

Semelhante a Queue (20)

basics of queues
basics of queuesbasics of queues
basics of queues
 
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
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
2 b queues
2 b queues2 b queues
2 b queues
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdfCEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queue
QueueQueue
Queue
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Queue
QueueQueue
Queue
 
05 queues
05 queues05 queues
05 queues
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
 

Último

No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
Sheetaleventcompany
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
raffaeleoman
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
Kayode Fayemi
 

Último (20)

No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verifiedSector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
 
Causes of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCauses of poverty in France presentation.pptx
Causes of poverty in France presentation.pptx
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animals
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
 
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
 
ICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdfICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdf
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 

Queue

  • 2. Abstract Data Type Stack & Queue is an example of ADT An array is not ADT.
  • 3. Using a Queue Several example applications of queues are given in that Slides. This presentation describes the queue operations and two ways to implement a queue. Data Structures and Other Objects Using C++
  • 4. What is Queue? A QUEUE IS A CONTAINER IN WHICH INSERTIONS ARE MADE ONLY AT THE BACK DELETIONS, RETRIEVALS, AND MODIFICATIONS ARE MADE ONLY AT THE FRONT.
  • 5. The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Front Rear
  • 6. EnQueue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Front Rear
  • 7. DeQueue Operations  When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Front Rear
  • 8. Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 AAnn aarrrraayy ooff iinntteeggeerrss ttoo iimmpplleemmeenntt aa qquueeuuee ooff iinntteeggeerrss WWee ddoonn''tt ccaarree wwhhaatt''ss iinn tthhiiss ppaarrtt ooff tthhee aarrrraayy..
  • 9. Array Implementation The efficient implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 3 ssiizzee 0 ffiirrsstt 2 llaasstt
  • 10. A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. 2 ssiizzee 1 ffiirrsstt 2 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6
  • 11. An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. 3 ssiizzee 1 ffiirrsstt 3 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 8 6 2
  • 12. At the End of the Array There is special behaviour at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 2 3 ssiizzee 3 ffiirrsstt 5 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 5 6 1
  • 13. At the End of the Array The new element goes at the front of the array (if that spot isn’t already used): 4 ssiizzee 3 ffiirrsstt 0 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 4 2 6 1
  • 14. Array Queue Reviews  Easy to implement.  But it has a limited capacity with a fixed array  Special behaviour is needed when the rear reaches the end of the array. [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 3 ssiizzee 0 ffiirrsstt 2 llaasstt
  • 15. Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail pointer. null 10 15 7 13 Head-ptr Tail-ptr
  • 16. Linked List Implementation Which end do you think is the front of the queue? Why ? 10 15 7 13 Head-ptr Tail-ptr null
  • 17. Linked List Implementation The head-ptr points to the front of the list. Because it is harder to remove items from the tail of the list. Rear Front 10 15 7 13 Head-ptr Tail-ptr null
  • 18. The Queue Class template <class Item> class queue<Item> { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; }  The C++ standard template library has a queue template class.  The template parameter is the type of the items that can be put in the queue.
  • 19. Summary QQuueeuueess hhaavvee mmaannyy aapppplliiccaattiioonnss..  IItteemmss eenntteerr aa qquueeuuee aatt tthhee rreeaarr aanndd lleeaavvee aa qquueeuuee aatt tthhee ffrroonntt.. QQuueeuueess ccaann bbee iimmpplleemmeenntteedd uussiinngg aann aarrrraayy oorr uussiinngg aa lliinnkkeedd lliisstt..
  • 20. Daily Life Examples There's a queue of questions that you've asked in exam, all waiting for you to accept answers for them. Waiting in a queue in any bank for paying bills, depositing cash OR cheques.

Notas do Editor

  1. When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive.
  2. This lecture introduces queues. The presentation also shows two common ways of implementing a queue of integers.
  3. When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive.
  4. When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive.
  5. Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear.
  6. When an item is removed from a queue, the removal occurs at the front.
  7. Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array.
  8. The easiest implementation also keeps track of three numbers. The size could be as small as zero or as large as the number of items in the array. The index of the front element is stored in the first member variable. The front item in the queue is at that index of the array. The next item is after the first one and so on until the rear of the queue that occurs at the index stored in a member variable called last.
  9. This shows how the member variables change when an item leaves the queue.
  10. And this shows how the member variables change when a new item enters the queue. For a fixed size array, a new item may enter only if the current size of the queue is less than the size of the array. For a dynamic array, we could increase the size of the array when the queue grows beyond the current array size.
  11. An array implementation of a queue must have special behavior when the rear of the queue reaches the end of the array. In this example, suppose we want to add the number 4 to the queue. We can do so…
  12. …by putting it at location 0 (if that location is not already used).
  13. Here are some of the key aspects of an array implementation of a queue.
  14. A linked list can also be used to implement a queue, but we must maintain both a head and a tail pointer because we need access to both the front and the rear of the queue.
  15. Does it matter which end of a singly-linked list we use for the front of the queue?
  16. Of course, we could put the front of the queue at the end of the linked list, but it would be hard to remove an item. Do you see why?
  17. These are the four most common queue operations. The empty function tells you whether the queue has any items at the moment. The front operation returns the item at the front of the queue (without removing it from the queue).
  18. A quick summary . . .
  19. Feel free to send your ideas to: Michael Main [email_address]