Dynamic Array Queue and Deque

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
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
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 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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
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.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Stacks And Queues Chapter 18.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Linked List Implementation of the Deque
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Elementary Data Structures
Chapter 18: Stacks and Queues.
ADT description Implementations
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues.
Queues 11/9/2018 6:32 PM Queues.
Chapter 19: Stacks and Queues.
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.
Linked Lists: Implementation of Queue & Deque
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Dynamic Array: Implementation of Queue & Deque
Getting queues right … finally (?)
Dynamic Array: Implementation of Stack
Stacks and Linked Lists
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Dynamic Array Queue and Deque CS261 Data Structures Dynamic Array Queue and Deque

Queues int isEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front. remove from the front add to the back

Queue Applications Printer Queue What nodes are reachable from A? DES: Use a queue to represent the simulated stations. Say, a factory with conveyer belts and parts that are moved from one belt to the next. These are typically simulated as DES where time is not represented explicitly as clock time, but as events that are created, arrive and leave at designated times. Simulation, therefore is about queuing up events, processing them one at a time. Operating Systems make heavy use of queues as buffers to hold jobs waiting to be serviced. A perfect example is the print queue. Generally: Good for situations where need to store things in order they arrived! What nodes are reachable from A?

Queue with Dynamic Array Removal from front is expensive! front back int isEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front. Stack grew down and was O(1) Implementing queue requires remove from front – O(n)

Deque (Double Ended Queue) ADT void addFront(TYPE val); void removeFront(); TYPE front(); void addBack(TYPE val); void removeBack () TYPE back(); removeBack addFront front back We’re not going to implement a queue with the dynamic array, but will introduce and implement a new ADT, the DEQUE Now let’s introduce a new ADT – the Deque. Both the stack and queue interfaces combined. 5 22 8 removeFront addBack

Deque Application Finite Length Undo Palindrome Checker ( e.g. radar) Could use a STACK, but don’t’ really want to keep them all around indefinitely. So, which do you get rid of and when? Can’t really keep ALL operations Want to throw out old…or allow user to determine their undo buffer Add to end Throw out of the front (add/remove back…remove front) Also: A-Steal Job scheduling algorithm

Let the partially filled block “float” & “wrap” Allow the starting index, beg, to “float” around the underlying array! It’s no longer confined to index 0 Logicalindex beg=3 beg=5 size size 3 4 0 1 2 3 0 1 2 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Just some additional bookkeeping to track beg and additional logic as you’ll see shortly. See Java ArrayDeque for an example. Absolute index How?? Keep track of the data, arranged circularly and convert all indices into proper indices for the actual array! Circular Buffer

ArrayDeque Structure struct ArrDeque { TYPE *data; /* Pointer to data array. */ int size; /* Number of elements in collection. */ int beg; /* Index of first element. */ int cap; /* Capacity of array. */ }; void initArrDeque(struct ArrDeque *d, int cap) { d->data = malloc(cap * sizeof(TYPE))); assert(d->data != 0); d->size = d->beg = 0; d->cap = cap; }

Adding/Removing from Back Remove beg beg size size beg beg size size

Adding/Removing from Front Remove beg beg size size decrement beg Increase size increment beg decrease size beg must be wrapped around though! beg beg size size

Wrapping Around – Circular Buffer Elements can wrap around from beg to end beg size

Wrapping Around absIdx = (logicalIdx + beg) % cap; Calculate offset: add logical (element) index to start (beg) offset = beg + logicalIndex; /* logIndex = 3, offset = 9 */ If larger than or eq to capacity, subtract capacity if (offset >= cap) absoluteIndex = offset – cap; Alternatively, use mod: /* Convert logical index to absolute index. */ absIdx = (logicalIdx + beg) % cap; beg = 6 cap = 8 beg size 0 1 2 3 4 5 6 7 beg = 0 cap = 8 beg size Say beg = 15, logical = 0, abso = (0+15) % 8 = 7 With offset: offset = 15 + 0; its > cap so abs = 15 – 8 = 7 Say beg = 23; offset = 23+0; it’s > cap, so abs = 23 – 8 = 15 !!!!!! Does NOT WORK!!! [could modify to abs = offset – cap*(beg/cap) because beg/cap tells you how many tiems to subtract off capacity (in this case == 2) Alterantively, you can write your code to make sure beg always falls in the bounds: e.g. wrap beg as well ! In my solution, beg can go well beyond the bounds and float in either direction Alternatively (this is my current version!): Wrap beg around so that it’s always within capacity! 0 1 2 3 4 5 6 7

Resizing? Can we simply copy the elements to a larger array? Have to be careful because the wrapping is dependent on the ‘capacity’

Key Changes from Dynamic Array Keep track of floating data with beg & size Wrap beg as necessary Whenever accessing a logical index, convert to absolute first On resize, copy to start of the new array and reset beg

Let’s look at some code… void addFrontDynArr(DynArr *v, TYPE val){ if (v->size >= v->capacity) _dynArrSetCapacity(v, 2*v->capacity); v->beg = v->beg - 1; if(v->beg < 0) v->beg = v->capacity-1; v->data[_absoluteIndex(v, 0)] = val; v->size++; } Always make sure you have space Update beg Wrap beg as necessary Don’t forget to increment size convert to proper absolute index front is at logical index = 0

Let’s look at some code… Full implementation is in dynamicArrayDeque.c void addFrontDynArr(DynArr *v, TYPE val){ if (v->size >= v->capacity) _dynArrSetCapacity(v, 2*v->capacity); v->beg = v->beg - 1; if(v->beg < 0) v->beg = v->capacity-1; v->data[_absoluteIndex(v, 0)] = val; v->size++; } Always make sure you have space Update beg Wrap beg as necessary Don’t forget to increment size convert to proper absolute index front is at logical index = 0

Operations Analysis Operation Best Worst Ave AddBack 1 n 1+ RemoveBack AddFront RemoveFront Worksheet Time – worksheet #20 Finish the implementation… 1+ is amortized constant time As you’re seeing in your homework, keepin