SlideShare a Scribd company logo
Data Structures and Algorithms EEE2020-01
Prof. Jongyoo Kim
6.
Queue
Queue
q Queue is Data Structure that save data in
§ LILO (Last-In, Last Out)
§ FIFO (First-In, First-Out)
q Type of Queue
§ Array Queue
• Implementation based on array
§ Linked Queue
• Implementation using memory allocation
Multimedia AI Lab. 2
head Data NULL
Data Data
Queue’s ADT
q void EnQueue(Datatype);
§ Insert first argument to Queue
q bool DeQueue(Datatype &);
§ Delete Data from Queue and return Data by Reference
§ If Deletion is successful, return true. If not, return false
q void PrintQueue();
§ Print Data in Queue
Multimedia AI Lab. 3
Main function
q Create Queue & Insert Data
q Delete Data
q Print Queue
Multimedia AI Lab. 4
Queue quque; Create queue
quque.enqueue(4); Insert a value(4) to the queue
quque.dequeue(tempdata); Get the value from the queue and delete it
quque.PrintList(); Print out all data in the queue
If the queue is empty, it returns false
If not, it returns true and tempdata stores the deleted value
Main function
Multimedia AI Lab. 5
Empty
Data : 1 2 3 4 5
Dequeue, Return data : 1
Dequeue, Return data : 2
Data : 3 4 5
Array Queue
q Array : Pointer to Array
q Front : index of first input data
q Rear : index of last input data
Multimedia AI Lab. 6
Array Queue(Enqueue, Dequeue)
q Insert Data to Queue
q Delete Data from Queue
Multimedia AI Lab. 7
Problem in Array Queue
q Array is not full, but Rear can’t be increased
q To solve this problem, Rear moves to 0 for inserting data(Circular Queue).
Multimedia AI Lab. 8
Array
Front
Rear ………
Data
Data
0
1
…
MAX-2
MAX-1
4
Max-1
New Data Out of bound
Circular Queue
q When index is Last, Front and Rear move to 0
Rear moves to next, insert data to rear
Delete data from front, front moves to next
Multimedia AI Lab. 9
Problem in Circular Queue
q Relation of Front and Rear is same whether queue is full or empty
Both queue is full and empty
(Rear + 1) % Max == Front
Multimedia AI Lab. 10
Problem in Circular Queue
q Initialize Front, Rear to 0
q Solve Problem by wasting one data store space
Multimedia AI Lab. 11
If queue is full
(Rear + 1) % Max == Front
If queue is empty,
Front == Rear
Circular Queue(Contructor)
q Initialize Front, Rear, Create Array for list
Array
Front
Rear
0
0
Index 0
Multimedia AI Lab. 12
Circular Queue(Destructor)
q Free memory of Array
Array
Front
Rear
0
2
Index 0
D
a
t
a
D
a
t
a
D
a
t
a
Multimedia AI Lab. 13
Circular Queue(EnQueue)
q Insert Data to Queue
D
a
t
a
Rear
Front
Index 0
D
a
t
a
D
a
t
a
Rear
Front
Index 0
Enqueue
Multimedia AI Lab. 14
Circular Queue(DeQueue)
q Delete Data to From
D
a
t
a
Rear
Front
Index 0
Dequeue
D
a
t
a
D
a
t
a
Rear
Front
Index 0
Multimedia AI Lab. 15
Circular Queue (PrintQueue)
q If Rear is bigger than Front, Print Data from Front+1 to Rear
q If Front is bigger than Rear, Print Data from Front+1 to MAX-1, from 0 to rear
Multimedia AI Lab. 16
Circular Queue(PrintQueue)
Multimedia AI Lab. 17
Multimedia AI Lab. 18
CircularQueue.h
Multimedia AI Lab. 19
CircularQueue.cpp (1/2)
Multimedia AI Lab. 20
CircularQueue.cpp (2/2)
Linked Queue
§ Linked Queue’s structure is the same as a Linked List.
§ Linked Queue’s function’s rule is the same as a Linked List’s function.
§ We implement Linked Queue as a Linked List.
Multimedia AI Lab. 21
head Data NULL
Data Data
Second Move
First Move
Linked Queue(EnQueue)
q When the list is empty,
§ the head changes to the new node.
q When the list isn’t empty:
§ Find the last node.
• First, the last node points to the first node.
• Second, if there exists a next node for the last node, move to the next node.
• Repeat until the last node’s next node is NULL.
Multimedia AI Lab. 22
head Data
Last Node
NULL
Last Node Last Node
head NULL head
New Node
Data NULL
Data Data
Last Node
New Node
Linked Queue(EnQueue)
q After finding the last node, the last node’s next node changes to the new node.
q The rule for enqueue is the same as pushback in a linked list.
Data NULL
Data NULL
Multimedia AI Lab. 23
Linked Queue(DeQueue)
q The head changes to the first node’s next node.
q The rule for dequeue is the same as deleting the front in a linked list.
Delete Node
Data
head Data
Multimedia AI Lab. 24
Linked Queue(PrintQueue)
q Print the node’s data from front to back.
q The rule for PrintQueue is the same as PrintList in a linked list.
Multimedia AI Lab. 25
Multimedia AI Lab. 26
LinkedQueue.h LinkedQueue.cpp
Multimedia AI Lab. 27
LinkedList.h
Multimedia AI Lab. 28
LinkedList.cpp (1/2)
Multimedia AI Lab. 29
LinkedList.cpp (2/2)
Multimedia AI Lab. 30
main.cpp
Empty
Data : 1 2 3 4 5
Dequeue, Return data : 1
Dequeue, Return data : 2
Data : 3 4 5

More Related Content

PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
PDF
05 queues
PPT
Queue AS an ADT (Abstract Data Type)
PPTX
@Chapter 4 DSA Part II.pptx
PPT
Algo>Queues
PPTX
lecture7 Stack.pptxsjkcnlksdnlnsnvlsndlvkn
PPT
Data Structures by Maneesh Boddu
Lec-07 Queues.ppt queues introduction to queue
Data Structures and Agorithm: DS 09 Queue.pptx
05 queues
Queue AS an ADT (Abstract Data Type)
@Chapter 4 DSA Part II.pptx
Algo>Queues
lecture7 Stack.pptxsjkcnlksdnlnsnvlsndlvkn
Data Structures by Maneesh Boddu

Similar to lecture6 Queue.pdfaonkjlasnknknlkasnncknckn (20)

PPTX
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
PPTX
Queues in C++
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPTX
Mca ii dfs u-3 linklist,stack,queue
PPTX
Data structures
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPTX
Queue Data Structure (w/ php egs)
DOCX
CDS artificial intelligence and Machine.docx
PPTX
Chapter 4 data structure and algorithm - Stacks and Queues
PPTX
Queue-Data Structure
PPTX
DS UNIT2QUEUES.pptx
PPTX
Queue data structures and operation on data structures
PPTX
PPTX
Queue ppt
PPTX
The presention is about the queue data structure
PPTX
Queue and its operations
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
Queues in C++
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
Polynomialmotilalanehrunationalinstitute.pdf
Bsc cs ii dfs u-2 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Data structures
Bca ii dfs u-2 linklist,stack,queue
Queue Data Structure (w/ php egs)
CDS artificial intelligence and Machine.docx
Chapter 4 data structure and algorithm - Stacks and Queues
Queue-Data Structure
DS UNIT2QUEUES.pptx
Queue data structures and operation on data structures
Queue ppt
The presention is about the queue data structure
Queue and its operations
Ad

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
A comparative analysis of optical character recognition models for extracting...
The AUB Centre for AI in Media Proposal.docx
Machine Learning_overview_presentation.pptx
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Building Integrated photovoltaic BIPV_UPV.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Ad

lecture6 Queue.pdfaonkjlasnknknlkasnncknckn

  • 1. Data Structures and Algorithms EEE2020-01 Prof. Jongyoo Kim 6. Queue
  • 2. Queue q Queue is Data Structure that save data in § LILO (Last-In, Last Out) § FIFO (First-In, First-Out) q Type of Queue § Array Queue • Implementation based on array § Linked Queue • Implementation using memory allocation Multimedia AI Lab. 2 head Data NULL Data Data
  • 3. Queue’s ADT q void EnQueue(Datatype); § Insert first argument to Queue q bool DeQueue(Datatype &); § Delete Data from Queue and return Data by Reference § If Deletion is successful, return true. If not, return false q void PrintQueue(); § Print Data in Queue Multimedia AI Lab. 3
  • 4. Main function q Create Queue & Insert Data q Delete Data q Print Queue Multimedia AI Lab. 4 Queue quque; Create queue quque.enqueue(4); Insert a value(4) to the queue quque.dequeue(tempdata); Get the value from the queue and delete it quque.PrintList(); Print out all data in the queue If the queue is empty, it returns false If not, it returns true and tempdata stores the deleted value
  • 5. Main function Multimedia AI Lab. 5 Empty Data : 1 2 3 4 5 Dequeue, Return data : 1 Dequeue, Return data : 2 Data : 3 4 5
  • 6. Array Queue q Array : Pointer to Array q Front : index of first input data q Rear : index of last input data Multimedia AI Lab. 6
  • 7. Array Queue(Enqueue, Dequeue) q Insert Data to Queue q Delete Data from Queue Multimedia AI Lab. 7
  • 8. Problem in Array Queue q Array is not full, but Rear can’t be increased q To solve this problem, Rear moves to 0 for inserting data(Circular Queue). Multimedia AI Lab. 8 Array Front Rear ……… Data Data 0 1 … MAX-2 MAX-1 4 Max-1 New Data Out of bound
  • 9. Circular Queue q When index is Last, Front and Rear move to 0 Rear moves to next, insert data to rear Delete data from front, front moves to next Multimedia AI Lab. 9
  • 10. Problem in Circular Queue q Relation of Front and Rear is same whether queue is full or empty Both queue is full and empty (Rear + 1) % Max == Front Multimedia AI Lab. 10
  • 11. Problem in Circular Queue q Initialize Front, Rear to 0 q Solve Problem by wasting one data store space Multimedia AI Lab. 11 If queue is full (Rear + 1) % Max == Front If queue is empty, Front == Rear
  • 12. Circular Queue(Contructor) q Initialize Front, Rear, Create Array for list Array Front Rear 0 0 Index 0 Multimedia AI Lab. 12
  • 13. Circular Queue(Destructor) q Free memory of Array Array Front Rear 0 2 Index 0 D a t a D a t a D a t a Multimedia AI Lab. 13
  • 14. Circular Queue(EnQueue) q Insert Data to Queue D a t a Rear Front Index 0 D a t a D a t a Rear Front Index 0 Enqueue Multimedia AI Lab. 14
  • 15. Circular Queue(DeQueue) q Delete Data to From D a t a Rear Front Index 0 Dequeue D a t a D a t a Rear Front Index 0 Multimedia AI Lab. 15
  • 16. Circular Queue (PrintQueue) q If Rear is bigger than Front, Print Data from Front+1 to Rear q If Front is bigger than Rear, Print Data from Front+1 to MAX-1, from 0 to rear Multimedia AI Lab. 16
  • 18. Multimedia AI Lab. 18 CircularQueue.h
  • 19. Multimedia AI Lab. 19 CircularQueue.cpp (1/2)
  • 20. Multimedia AI Lab. 20 CircularQueue.cpp (2/2)
  • 21. Linked Queue § Linked Queue’s structure is the same as a Linked List. § Linked Queue’s function’s rule is the same as a Linked List’s function. § We implement Linked Queue as a Linked List. Multimedia AI Lab. 21 head Data NULL Data Data
  • 22. Second Move First Move Linked Queue(EnQueue) q When the list is empty, § the head changes to the new node. q When the list isn’t empty: § Find the last node. • First, the last node points to the first node. • Second, if there exists a next node for the last node, move to the next node. • Repeat until the last node’s next node is NULL. Multimedia AI Lab. 22 head Data Last Node NULL Last Node Last Node head NULL head New Node Data NULL Data Data
  • 23. Last Node New Node Linked Queue(EnQueue) q After finding the last node, the last node’s next node changes to the new node. q The rule for enqueue is the same as pushback in a linked list. Data NULL Data NULL Multimedia AI Lab. 23
  • 24. Linked Queue(DeQueue) q The head changes to the first node’s next node. q The rule for dequeue is the same as deleting the front in a linked list. Delete Node Data head Data Multimedia AI Lab. 24
  • 25. Linked Queue(PrintQueue) q Print the node’s data from front to back. q The rule for PrintQueue is the same as PrintList in a linked list. Multimedia AI Lab. 25
  • 26. Multimedia AI Lab. 26 LinkedQueue.h LinkedQueue.cpp
  • 27. Multimedia AI Lab. 27 LinkedList.h
  • 28. Multimedia AI Lab. 28 LinkedList.cpp (1/2)
  • 29. Multimedia AI Lab. 29 LinkedList.cpp (2/2)
  • 30. Multimedia AI Lab. 30 main.cpp Empty Data : 1 2 3 4 5 Dequeue, Return data : 1 Dequeue, Return data : 2 Data : 3 4 5