Circular Queues: Implemented using Arrays

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Stack & Queues COP 3502.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures - Queues
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Python: Stacks and Queues (as a Linked List) Damian Gordon.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
CSE 1342 Programming Concepts
Review Array Array Elements Accessing array elements
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
Linked Lists Damian Gordon.
Queue data structure.
Queue.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queue, Deque, and Priority Queue Implementations
Queues.
Lesson Objectives Aims
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
COMPUTER 2430 Object Oriented Programming and Data Structures I
Revised based on textbook author’s notes.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues: Implemented using Arrays
ADT list.
Stacks and Queues CSE 373 Data Structures.
Stacks: Implemented using Linked Lists
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
ADT Queue (Array Implementation)
Python: Stacks and Queues (as an Array)
Queues Definition of a Queue Examples of Queues
CSE 373 Data Structures Lecture 6
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Getting queues right … finally (?)
Presentation transcript:

Circular Queues: Implemented using Arrays Damian Gordon

Circular Queues We can also have a circular queue:

Circular Queues We can also have a circular queue: A queue where the start and end of the queue are joined together.

Circular Queues Head Tail We can also have a circular queue: 7 43 6 1 43 6 1 12 35 5 2 22 99 4 Tail 3

Circular Queues Head Tail We can also have a circular queue: 7 6 1 12 6 1 12 35 5 2 22 99 4 Tail 3

Circular Queues Head Tail We can also have a circular queue: 7 6 1 12 6 1 12 68 35 5 2 Tail 22 99 4 3

Circular Queues Head Tail We can also have a circular queue: 7 6 1 12 6 1 Tail 12 54 68 35 5 2 22 99 4 3

Circular Queues Tail Head We can also have a circular queue: 7 6 1 54 6 1 Tail 54 Head 68 35 5 2 22 99 4 3

Circular Queues Tail Head We can also have a circular queue: 7 17 6 1 17 6 1 54 Head 68 35 5 2 22 99 4 3

Circular Queues Tail Head We can also have a circular queue: 7 17 81 6 17 81 6 1 54 Head 68 35 5 2 22 99 4 3

Circular Queues Tail Head We can also have a circular queue: 7 17 81 6 17 81 6 1 54 43 Head 68 35 5 2 22 99 4 3

Simple Simulation

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize

Queues Queue Head Tail MaxSize We will implement a queue as an array called Queue. Tail Head 1 2 3 4 5 6 Queue MaxSize

End of Simulation

Circular Queues Queue Head Tail MaxSize How does that work, let’s DeleteFromQ() Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize

Circular Queues Queue Head Tail MaxSize Now AddtoQ(55) 41 1 2 59 26 3 41 1 2 59 26 3 53 4 5 6 Queue MaxSize

Circular Queues Queue Head Tail MaxSize Now let’s DeleteFromQ() 1 41 2 1 41 2 59 3 26 4 53 5 55 6 Queue MaxSize

Circular Queues Queue Head Tail MaxSize Now AddtoQ(34) 1 2 59 26 3 53 1 2 59 26 3 53 4 5 55 6 Queue MaxSize

Circular Queues Queue Head Tail MaxSize Now AddtoQ(12) 1 2 59 3 26 4 1 2 59 3 26 4 53 5 55 6 34 Queue MaxSize

Circular Queues Tail Head 12 1 2 59 3 26 4 53 5 55 6 34 Queue MaxSize

Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But…

Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… To be circular it goes as follows: 4->5->6->0->1->2->3->4->5->6->0->1->2->3->4->5->6->0->1->etc.

Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7

Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7 So Tail % 7 works as follows: 4 % 7 = 4 5 % 7 = 5 6 % 7 = 6 7 % 7 = 0 8 % 7 = 1

Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7 So Tail % MaxSize works as follows: 4 % MaxSize = 4 5 % MaxSize = 5 6 % MaxSize = 6 7 % MaxSize = 0 8 % MaxSize = 1

Circular Queues We will look at implementing the following modules: IsFull() Check if the queue is full IsEmpty() AddToQ(N) Add a new item (N) to the back of the queue DeleteFromQ() Remove the front value from the queue ClearQ() Empty the queue

Circular Queues Tail Head IsFull() 1 41 2 59 26 3 4 7 if Head = (Tail % MaxSize) + 1 If they have caught up to each other, then it’s full. Tail 7 17 81 6 1 54 43 1 41 2 59 26 3 4 Head 68 35 5 2 22 99 4 3

Circular Queues Tail MaxSize Head IsFull() 81 1 35 2 99 22 3 4 68 54 5 if Head = (Tail % MaxSize) + 1 If they have caught up to each other, then it’s full. Tail Head 81 1 35 2 99 22 3 4 68 54 5 6 17 MaxSize

Circular Queues MODULE IsFull(): Boolean Full; IF Head = (Tail + 1) % MaxSize THEN Full <- True; ELSE Full <- False; ENDIF; RETURN Full; END.

Circular Queues Or MODULE IsFull(): RETURN Head = (Tail + 1) % MaxSize; END.

Circular Queues Tail Head IsEmpty() 1 41 59 2 26 3 4 if Tail = Head 7 6 1 1 41 59 2 26 3 4 5 2 4 3

Circular Queues Queue MaxSize Head Tail IsEmpty() 1 2 3 4 5 6 if Tail = Head Head Tail 1 2 3 4 5 6 Queue MaxSize

Circular Queues MODULE IsEmpty(): Boolean Empty; IF Head = Tail THEN Empty <- True; ELSE Empty <- False; ENDIF; RETURN Empty; END.

Circular Queues Or MODULE IsEmpty(): RETURN Head = Tail; END.

Circular Queues AddToQ(17) 7 6 1 Tail 54 Head 68 35 5 2 22 99 4 3

Circular Queues AddToQ(81) Tail 7 17 6 1 54 Head 68 35 5 2 22 99 4 3

Circular Queues Tail Head AddToQ(43) 7 17 81 6 1 54 68 35 5 2 22 99 4 17 81 6 1 54 Head 68 35 5 2 22 99 4 3

Circular Queues Tail Head Queue is Full! 7 17 81 6 1 43 54 68 35 5 2 17 81 6 1 54 43 Head 68 35 5 2 22 99 4 3

Circular Queues Queue Head Tail MaxSize AddToQ(N) 31 1 41 2 59 3 26 53 Increment the Tail pointer % MaxSize, and add to the Tail. Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize

Circular Queues MODULE AddToQ(N): IF IsFull() = True THEN Print “Queue is Full”; ELSE Tail <- (Tail + 1) % MaxSize; Queue[Tail] <- N; ENDIF; END.

Circular Queues Head Tail DeleteFromQ() 7 43 6 1 12 54 68 35 5 2 22 99 43 6 1 Tail 12 54 68 35 5 2 22 99 4 3

Circular Queues Head Tail DeleteFromQ() 7 6 1 12 54 68 35 5 2 22 99 4 6 1 Tail 12 54 68 35 5 2 22 99 4 3

Circular Queues DeleteFromQ() 7 6 1 Tail 54 Head 68 35 5 2 22 99 4 3

Circular Queues Queue Head Tail MaxSize DeleteFromQ() 31 1 41 2 59 3 Write Head value into N, and add one to Head Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize

Circular Queues MODULE DeleteFromQ(): N <- 0; IF IsEmpty() = True THEN Print “Queue is Empty”; ELSE N <- Queue[Head]; Head <- (Head + 1) % MaxSize; ENDIF; RETURN N; END.

Circular Queues Tail Head ClearQ() 1 41 59 2 26 3 4 Set Tail = Head = 0 Tail Head 7 6 1 1 41 59 2 26 3 4 5 2 4 3

Circular Queues Queue MaxSize Head Tail ClearQ() 1 2 3 4 5 6 Set Tail = Head = -1 Head Tail 1 2 3 4 5 6 Queue MaxSize

Circular Queues MODULE ClearQ(): Tail <- -1; Head <- Tail; END.

etc.