SlideShare une entreprise Scribd logo
Data	
  Structures:	
  Queues	
  &	
  ADT	
  
CS	
  1112	
  
Mona	
  Diab	
  
Queues	
  
• Defini9on	
  of	
  a	
  Queue	
  	
  
• Examples	
  of	
  Queues	
  
• Design	
  of	
  a	
  Queue	
  Class	
  	
  
• Different	
  Implementa9ons	
  of	
  the	
  
Queue	
  Class	
  
Introduction to Queues
• A queue is a waiting line
• It’s in daily life:
– A line of persons waiting to check out at a
supermarket
– A line of persons waiting to purchase a ticket for a
film
– A line of planes waiting to take off at an airport
– A line of vehicles at a toll booth
Defini*on	
  of	
  a	
  Queue	
  
• A	
  queue	
  is	
  a	
  data	
  structure	
  that	
  models/enforces	
  the	
  
first-­‐come	
  first-­‐serve	
  order,	
  or	
  equivalently	
  the	
  first-­‐
in	
  first-­‐out	
  (FIFO)	
  order.	
  
• That	
  is,	
  the	
  element	
  that	
  is	
  inserted	
  first	
  into	
  the	
  
queue	
  will	
  be	
  the	
  element	
  that	
  will	
  deleted	
  first,	
  and	
  
the	
  element	
  that	
  is	
  inserted	
  last	
  is	
  deleted	
  last.	
  
• A	
  wai9ng	
  line	
  is	
  a	
  good	
  real-­‐life	
  example	
  of	
  a	
  queue.	
  
(In	
  fact,	
  the	
  Bri9sh	
  word	
  for	
  “line”	
  is	
  “queue”.)	
  
	
  
Queues
• Unlike stacks in which elements are popped and
pushed only at the ends of the list
Collection of data elements:
• items are removed from a queue at one end,
called the FRONT of the queue;
• and elements are added at the other end, called
the BACK
Introduction to Queues
• Difference between Stack and Queues:
– Stack exhibits last-in-first-out (LIFO)
– Queue exhibits first-in-first-out (FIFO)
The Queue Operations
❐ A	
  queue	
  is	
  like	
  a	
  line	
  
of	
  people	
  wai9ng	
  for	
  a	
  
bank	
  teller.	
  The	
  queue	
  
has	
  a	
  front	
  and	
  a	
  rear.	
  
	
  
$	
  	
  $	
  	
  
Front	
  
Rear	
  
The Queue Operations
❐ New	
  people	
  must	
  enter	
  the	
  queue	
  at	
  
the	
  rear.	
  This	
  is	
  called	
  an	
  enqueue	
  
opera9on.	
  
	
   $	
  	
  $	
  	
  
Front	
  
Rear	
  
The Queue Operations
❐ When	
  an	
  item	
  is	
  taken	
  from	
  the	
  
queue,	
  it	
  always	
  comes	
  from	
  the	
  front.	
  	
  
This	
  is	
  called	
  a	
  dequeue	
  opera9on.	
  
	
   $	
  	
  $	
  	
  
Front	
  
Rear	
  
Queue Abstract Data Type
• Basic operations
– Construct a queue
– Check if empty
– Enqueue (add element to back)
– Front (retrieve value of element from front)
– Dequeue (remove element from front)
A	
  Graphic	
  Model	
  of	
  a	
  Queue	
  
Tail:	
  
All	
  new	
  items	
  	
  
are	
  added	
  on	
  	
  
this	
  end	
  
	
  	
  	
  	
  	
  	
  Head:	
  
All	
  items	
  are	
  	
  
deleted	
  from	
  	
  
this	
  end	
  
Opera*ons	
  on	
  Queues	
  
• Insert(item):	
  (also	
  called	
  enqueue)	
  
– It	
  adds	
  a	
  new	
  item	
  to	
  the	
  tail	
  of	
  the	
  queue	
  
• Remove(	
  ):	
  	
  (also	
  called	
  delete	
  or	
  dequeue)	
  
– It	
  deletes	
  the	
  head	
  item	
  of	
  the	
  queue,	
  and	
  returns	
  to	
  the	
  caller.	
  If	
  the	
  queue	
  is	
  
already	
  empty,	
  this	
  opera9on	
  returns	
  NULL	
  
• getHead(	
  ):	
  
– Returns	
  the	
  value	
  in	
  the	
  head	
  element	
  of	
  the	
  queue	
  
• getTail(	
  ):	
  
– Returns	
  the	
  value	
  in	
  the	
  tail	
  element	
  of	
  the	
  queue	
  
• isEmpty(	
  )	
  
– Returns	
  true	
  if	
  the	
  queue	
  has	
  no	
  items	
  
• size(	
  )	
  
– Returns	
  the	
  number	
  of	
  items	
  in	
  the	
  queue	
  
Queue	
  as	
  a	
  Class	
  
• Much	
  like	
  stacks	
  and	
  linked	
  lists	
  were	
  
designed	
  and	
  implemented	
  as	
  classes,	
  a	
  
queue	
  can	
  be	
  conveniently	
  packaged	
  as	
  a	
  class	
  
• It	
  seems	
  natural	
  to	
  think	
  of	
  a	
  queue	
  as	
  similar	
  
to	
  a	
  linked	
  list,	
  but	
  with	
  more	
  basic	
  
opera9ons,	
  to	
  enforce	
  the	
  FIFO	
  order	
  of	
  
inser9on	
  and	
  dele9on	
  
Designing and Building a Queue Class
Array-Based
• Consider an array in which to store a
queue
• Note additional variables needed
– myFront, myBack
• Picture a queue
object like this
Queue Operation
• Empty Queue
Enqueue(70)
Queue Operation
• Enqueue(80)
• Enqueue(50)
Queue Operation
• Dequeue()
• Dequeue()
Queue Operation
• Enqueue(90)
• Enqueue(60)
Circular Queue
• Problems
– We quickly "walk off the end" of the array
• Possible solutions
– Shift array elements
– Use a circular queue
– Note that both empty
and full queue
gives myBack == myFront
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).
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
   .	
  .	
  .	
  
An	
  array	
  of	
  integers	
  
to	
  implement	
  a	
  
queue	
  of	
  integers	
  
4	
   8	
   6	
  
We	
  don't	
  care	
  what's	
  in	
  
this	
  part	
  of	
  the	
  array.	
  
Array Implementation
• The easiest 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).
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
   .	
  .	
  .	
  
4	
   8	
   6	
  
size	
  
3	
  
first	
  
0	
  
last	
  
2	
  
A Dequeue Operation
• When an element leaves the queue,
size is decremented, and first changes,
too.
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
   .	
  .	
  .	
  
4	
   8	
   6	
  
size	
  
2	
  
first	
  
1	
  
last	
  
2	
  
An Enqueue Operation
• When an element enters the queue,
size is incremented, and last changes,
too.
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
   .	
  .	
  .	
  
2	
  
8	
   6	
  
size	
  
3	
  
first	
  
1	
  
last	
  
3	
  
At the End of the Array
• There is special behavior 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]:
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
  
2	
   1	
  
6	
  
size	
  
3	
  
first	
  
3	
  
last	
  
5	
  
At the End of the Array
• The new element goes at the front of
the array (if that spot isn’t already
used):
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
  
2	
   1	
  
6	
  
size	
  
4	
  
first	
  
3	
  
last	
  
0	
  
4	
  
Array Implementation
• Easy to implement
• But it has a limited capacity with a fixed array
• Or you must use a dynamic array for an
unbounded capacity
• Special behavior is needed when the rear reaches
the end of the array.
[	
  0	
  ]	
   [1]	
   [	
  2	
  ]	
   [	
  3	
  ]	
   [	
  4	
  ]	
   [	
  5	
  ]	
   .	
  .	
  .	
  
4	
   8	
   6	
  
size	
  
3	
  
first	
  
0	
  
last	
  
2	
  
Linked List Implementation
10	
  
15
7	
  
null	
  
13	
  
• A queue can also be implemented
with a linked list with both a
head and a tail pointer.
head_ptr	
  
tail_ptr	
  
Linked List Implementation
10	
  
15
7	
  
null	
  
13	
  
• Which end do you think is the
front of the queue? Why?
head_ptr	
  
tail_ptr	
  
Linked List Implementation
10	
  
15
7	
  
null	
  
head_ptr	
  
13	
  
• The head_ptr points to the front
of the list.
• Because it is harder to remove
items from the tail of the list.
tail_ptr	
  
Front	
  
Rear	
  
Abstract	
  Data	
  Type	
  
• Abstract	
  Data	
  Type	
  as	
  a	
  design	
  tool	
  	
  
• Concerns	
  only	
  on	
  the	
  important	
  concept	
  or	
  
model	
  
• No	
  concern	
  on	
  implementa9on	
  details.	
  
• Stack	
  &	
  Queue	
  is	
  an	
  example	
  of	
  ADT	
  
• An	
  array	
  is	
  not	
  ADT.	
  	
  
What	
  is	
  the	
  difference?	
  
• Stack	
  &	
  Queue	
  vs.	
  Array	
  
– Arrays	
  are	
  data	
  storage	
  structures	
  while	
  stacks	
  and	
  queues	
  
are	
  specialized	
  DS	
  and	
  used	
  as	
  programmer’s	
  tools.	
  
• Stack	
  –	
  a	
  container	
  that	
  allows	
  push	
  and	
  pop	
  
• Queue	
  -­‐	
  a	
  container	
  that	
  allows	
  enqueue	
  and	
  
dequeue	
  
• No	
  concern	
  on	
  implementa9on	
  details.	
  
• In	
  an	
  array	
  any	
  item	
  can	
  be	
  accessed,	
  while	
  in	
  these	
  
data	
  structures	
  access	
  is	
  restricted.	
  
• They	
  are	
  more	
  abstract	
  than	
  arrays.	
  
Ques9ons?	
  
• Array	
  is	
  not	
  ADT	
  
• Is	
  Linked	
  list	
  ADT?	
  
Queue ADT for data structure for computer

Contenu connexe

Similaire à Queue ADT for data structure for computer

Queue
QueueQueue
stack.pptx
stack.pptxstack.pptx
stack.pptx
mayankKatiyar17
 
Queues
QueuesQueues
Queues
QueuesQueues
linked list in c++
linked list in c++linked list in c++
linked list in c++
YaminiLakshmi Meduri
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
SnehilKeshari
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
Data Structures
Data StructuresData Structures
Data Structures
Dr.Umadevi V
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
Sunipa Bera
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
LECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
SKUP1
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
Muhammad Hammad Waseem
 
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
vtunali
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
Dr.Shweta
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
SajalFayyaz
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
Mehedi Hasan
 

Similaire à Queue ADT for data structure for computer (20)

Queue
QueueQueue
Queue
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
Queues
QueuesQueues
Queues
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Queues
QueuesQueues
Queues
 
linked list in c++
linked list in c++linked list in c++
linked list in c++
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Data Structures
Data StructuresData Structures
Data Structures
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [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
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 

Dernier

Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
eddie19851
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Enterprise Wired
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 

Dernier (20)

Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 

Queue ADT for data structure for computer

  • 1. Data  Structures:  Queues  &  ADT   CS  1112   Mona  Diab  
  • 2. Queues   • Defini9on  of  a  Queue     • Examples  of  Queues   • Design  of  a  Queue  Class     • Different  Implementa9ons  of  the   Queue  Class  
  • 3. Introduction to Queues • A queue is a waiting line • It’s in daily life: – A line of persons waiting to check out at a supermarket – A line of persons waiting to purchase a ticket for a film – A line of planes waiting to take off at an airport – A line of vehicles at a toll booth
  • 4. Defini*on  of  a  Queue   • A  queue  is  a  data  structure  that  models/enforces  the   first-­‐come  first-­‐serve  order,  or  equivalently  the  first-­‐ in  first-­‐out  (FIFO)  order.   • That  is,  the  element  that  is  inserted  first  into  the   queue  will  be  the  element  that  will  deleted  first,  and   the  element  that  is  inserted  last  is  deleted  last.   • A  wai9ng  line  is  a  good  real-­‐life  example  of  a  queue.   (In  fact,  the  Bri9sh  word  for  “line”  is  “queue”.)    
  • 5. Queues • Unlike stacks in which elements are popped and pushed only at the ends of the list Collection of data elements: • items are removed from a queue at one end, called the FRONT of the queue; • and elements are added at the other end, called the BACK
  • 6. Introduction to Queues • Difference between Stack and Queues: – Stack exhibits last-in-first-out (LIFO) – Queue exhibits first-in-first-out (FIFO)
  • 7. The Queue Operations ❐ A  queue  is  like  a  line   of  people  wai9ng  for  a   bank  teller.  The  queue   has  a  front  and  a  rear.     $    $     Front   Rear  
  • 8. The Queue Operations ❐ New  people  must  enter  the  queue  at   the  rear.  This  is  called  an  enqueue   opera9on.     $    $     Front   Rear  
  • 9. The Queue Operations ❐ When  an  item  is  taken  from  the   queue,  it  always  comes  from  the  front.     This  is  called  a  dequeue  opera9on.     $    $     Front   Rear  
  • 10. Queue Abstract Data Type • Basic operations – Construct a queue – Check if empty – Enqueue (add element to back) – Front (retrieve value of element from front) – Dequeue (remove element from front)
  • 11. A  Graphic  Model  of  a  Queue   Tail:   All  new  items     are  added  on     this  end              Head:   All  items  are     deleted  from     this  end  
  • 12. Opera*ons  on  Queues   • Insert(item):  (also  called  enqueue)   – It  adds  a  new  item  to  the  tail  of  the  queue   • Remove(  ):    (also  called  delete  or  dequeue)   – It  deletes  the  head  item  of  the  queue,  and  returns  to  the  caller.  If  the  queue  is   already  empty,  this  opera9on  returns  NULL   • getHead(  ):   – Returns  the  value  in  the  head  element  of  the  queue   • getTail(  ):   – Returns  the  value  in  the  tail  element  of  the  queue   • isEmpty(  )   – Returns  true  if  the  queue  has  no  items   • size(  )   – Returns  the  number  of  items  in  the  queue  
  • 13. Queue  as  a  Class   • Much  like  stacks  and  linked  lists  were   designed  and  implemented  as  classes,  a   queue  can  be  conveniently  packaged  as  a  class   • It  seems  natural  to  think  of  a  queue  as  similar   to  a  linked  list,  but  with  more  basic   opera9ons,  to  enforce  the  FIFO  order  of   inser9on  and  dele9on  
  • 14. Designing and Building a Queue Class Array-Based • Consider an array in which to store a queue • Note additional variables needed – myFront, myBack • Picture a queue object like this
  • 15. Queue Operation • Empty Queue Enqueue(70)
  • 19. Circular Queue • Problems – We quickly "walk off the end" of the array • Possible solutions – Shift array elements – Use a circular queue – Note that both empty and full queue gives myBack == myFront
  • 20. 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). [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   .  .  .   An  array  of  integers   to  implement  a   queue  of  integers   4   8   6   We  don't  care  what's  in   this  part  of  the  array.  
  • 21. Array Implementation • The easiest 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). [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   .  .  .   4   8   6   size   3   first   0   last   2  
  • 22. A Dequeue Operation • When an element leaves the queue, size is decremented, and first changes, too. [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   .  .  .   4   8   6   size   2   first   1   last   2  
  • 23. An Enqueue Operation • When an element enters the queue, size is incremented, and last changes, too. [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   .  .  .   2   8   6   size   3   first   1   last   3  
  • 24. At the End of the Array • There is special behavior 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]: [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   2   1   6   size   3   first   3   last   5  
  • 25. At the End of the Array • The new element goes at the front of the array (if that spot isn’t already used): [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   2   1   6   size   4   first   3   last   0   4  
  • 26. Array Implementation • Easy to implement • But it has a limited capacity with a fixed array • Or you must use a dynamic array for an unbounded capacity • Special behavior is needed when the rear reaches the end of the array. [  0  ]   [1]   [  2  ]   [  3  ]   [  4  ]   [  5  ]   .  .  .   4   8   6   size   3   first   0   last   2  
  • 27. Linked List Implementation 10   15 7   null   13   • A queue can also be implemented with a linked list with both a head and a tail pointer. head_ptr   tail_ptr  
  • 28. Linked List Implementation 10   15 7   null   13   • Which end do you think is the front of the queue? Why? head_ptr   tail_ptr  
  • 29. Linked List Implementation 10   15 7   null   head_ptr   13   • The head_ptr points to the front of the list. • Because it is harder to remove items from the tail of the list. tail_ptr   Front   Rear  
  • 30. Abstract  Data  Type   • Abstract  Data  Type  as  a  design  tool     • Concerns  only  on  the  important  concept  or   model   • No  concern  on  implementa9on  details.   • Stack  &  Queue  is  an  example  of  ADT   • An  array  is  not  ADT.    
  • 31. What  is  the  difference?   • Stack  &  Queue  vs.  Array   – Arrays  are  data  storage  structures  while  stacks  and  queues   are  specialized  DS  and  used  as  programmer’s  tools.   • Stack  –  a  container  that  allows  push  and  pop   • Queue  -­‐  a  container  that  allows  enqueue  and   dequeue   • No  concern  on  implementa9on  details.   • In  an  array  any  item  can  be  accessed,  while  in  these   data  structures  access  is  restricted.   • They  are  more  abstract  than  arrays.  
  • 32. Ques9ons?   • Array  is  not  ADT   • Is  Linked  list  ADT?