ADT Queue (Array Implementation)

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

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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 Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Stacks, Queues & Deques CSC212.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Data Structures - Queues
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
The Queue ADT.
18 Chapter Stacks and Queues
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Priority Queue.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
Revised based on textbook author’s notes.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Lecture 16 Stacks and Queues CSE /26/2018.
CE 221 Data Structures and Algorithms
Circular Queues: Implemented using Arrays
Queues Definition of a Queue Examples of Queues
Queues: Implemented using Linked Lists
Chapter 9 The Priority Queue ADT
Lecture 16 Stacks and Queues CSE /26/2018.
Queues and Priority Queue Implementations
Getting queues right … finally (?)
Data Structures & Programming
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

ADT Queue (Array Implementation)

ADT Queue (Array Implementation) Array implementation of the queue…a fixed size array is used to store the data elements. As data elements are enqueued & served the queue crawls through the array from low to high index values. As the queue crawls forward, it also expands and contracts.

ADT Queue (Array Implementation) Head Tail After one En-queue and one Serve Head Tail

ADT Queue (Array Implementation) Head Tail Where to En-queue this?

ADT Queue (Array Implementation) MaxSize-1 Tail Head Wrap Round

ADT Queue (Array Implementation) Tail Head MaxSize - 1

Adapted from Pearson Education, Inc. An empty Queue [0] [1] [2] [3] [4] Front Back 4 Enqueue at back  incr back then store item The queue is EMPTY ( Back + 1 ) % size = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. enQueue A [0] A [1] [2] [3] [4] Front Back The queue is not EMPTY ( Back + 1 ) % size = ( 0 + 1 ) % 5 = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. enQueue X [0] A [1] X [2] [3] [4] Front Back 1 The queue is not EMPTY ( Back + 1 ) % size = ( 1 + 1 ) % 5 = 2 =/= Front = 0 Adapted from Pearson Education, Inc.

enQueue G, B, and Z – it’s full now Same As When Queue Was empty [0] A [1] X [2] G [3] B [4] Z Front Back 4 Dequeue from Front Serve front then incr front The queue is FULL BUT ( Back + 1 ) % size = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. deQueue [0] [1] X [2] G [3] B [4] Z Front 1 Back 4 The queue is not Empty And not full ( Back + 1 ) % size = ( 4 + 1 ) % 5 = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. deQueue [0] [1] [2] G [3] B [4] Z Front 2 Back 4 Let’s enqueue 2 more: W and K The queue is not Empty ( Back + 1 ) % size = ( 4 + 1 ) % 5 = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. enQueue W [0] W [1] [2] G [3] B [4] Z Front 2 Back The queue is not Empty ( Back + 1 ) % size = ( 0 + 1 ) % 5 = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

enQueue K – it’s full again [0] W [1] K [2] G [3] B [4] Z Front 2 Back 1 How can we avoid the condition for empty and full being the same? The queue is FULL BUT ( Back + 1 ) % size = ( 1 + 1 ) % 5 = 2 == Front = 2 Adapted from Pearson Education, Inc.

Start with an empty Queue again [0] [1] [2] [3] [4] Front Back 4 One Location remains unused The queue is EMPTY ( Back + 1 ) % size = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

enQueue A [0] A [1] [2] [3] [4] Front Back One Location remains unused Back One Location remains unused The queue is not EMPTY ( Back + 1 ) % size = ( 0 + 1 ) % 5 = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

enQueue X [0] A [1] X [2] [3] [4] 1 Front Back Back 1 One Location remains unused The queue is not EMPTY ( Back + 1 ) % size = ( 1 + 1 ) % 5 = 2 =/= Front = 1 Adapted from Pearson Education, Inc.

enQueue G, and B – it’s full now Different From When Queue Was empty [0] A [1] X [2] G [3] B [4] Front Back 3 One Location remains unused The queue is FULL ( Back + 2 ) % size = ( 3 + 2 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

deQueue [0] [1] X [2] G [3] B [4] 1 3 Front Back One Location remains unused The queue is not Empty And not full ( Back + 2 ) % size = ( 3 + 2 ) % 5 = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

deQueue [0] [1] [2] G [3] B [4] 2 3 Let’s enqueue 2 more: W and K Front 2 Back 3 One Location remains unused Let’s enqueue 2 more: W and K The queue is not Empty ( Back + 1 ) % size = ( 3 + 1 ) % 5 = 4 =/= Front = 2 Adapted from Pearson Education, Inc.

enQueue W [0] [1] [2] G [3] B [4] W 2 4 Front Back One Location remains unused The queue is not Empty ( Back + 1 ) % size = ( 4 + 1 ) % 5 = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

enQueue K [0] K [1] [2] G [3] B [4] W 2 Let’s enqueue 1 more: Z Front Back One Location remains unused Let’s enqueue 1 more: Z The queue is not Empty ( Back + 1 ) % size = ( 0 + 1 ) % 5 = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

enQueue Z – can’t do it… already full [0] K [1] [2] G [3] B [4] W Front 2 Back One Location remains unused The queue is FULL ( Back + 2 ) % size = ( 0 + 2 ) % 5 = 2 == Front = 2 Adapted from Pearson Education, Inc.

ADT Queue (Array Implementaion) public class ArrayQueue <T> { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */ public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }

ADT Queue (Array Implementation) public boolean full () { return size == maxsize ? true : false; } public int length () { return size; public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1) % maxsize; size++;

ADT Queue -Array Implementation public T serve () { T e = nodes[head]; head = (head + 1) % maxsize; size--; return e; }