Dynamic Array: Implementation of Queue & Deque

Slides:



Advertisements
Similar presentations
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Advertisements

Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
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,
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Dynamic Array Queue and Deque
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
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"
Week 15 – Monday.  What did we talk about last time?  Tries.
Elementary Data Structures
Review Array Array Elements Accessing array elements
Week 4 - Monday CS221.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Linked List Introduction
Queue ADT (Abstract Data Type) N …
Lecture 7 Queues Stacks Trees.
QueueStack CS1020.
Week 4 - Friday CS221.
Queues Rem Collier Room A1.02
Queues Queues Queues.
Week 15 – Monday CS221.
Programming Abstractions
Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Exam 2 Review CS 3358 Data Structures.
Abstract Data Types (ADTs)
THURSDAY, OCTOBER 17 IN LAB
Stacks, Queues, and Deques
CS 2308 Final Exam Review.
Circular queue.
Exam 2 Review CS 3358 Data Structures.
Linked Lists: Implementation of Queue & Deque
Stacks, Queues, and Deques
Lesson Objectives Aims
CSC 143 Queues [Chapter 7].
Exam 2 Review CS 3358 Data Structures.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Lesson 1 Adding Integers.
Data Structures and Algorithms for Information Processing
Stacks and Queues Prof. Michael Tsai 2017/02/21.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Priority Queues & Heaps
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Priority Queues & Heaps
Stacks, Queues, and Deques
Queues and Priority Queue Implementations
Getting queues right … finally (?)
Dynamic Array: Implementation of Stack
Lecture 9: Stack and Queue
COMPUTER 2430 Object Oriented Programming and Data Structures I
slides created by Marty Stepp and Hélène Martin
Data Structures & Programming
Presentation transcript:

Dynamic Array: Implementation of Queue & Deque CS 261 – Data Structures Dynamic Array: Implementation of Queue & Deque

Dynamic Array: Adding & Removing at Front Adding or removing in a dynamic array require “sliding” all elements with a higher index: Adding at front of array  slide elements up Removing from front of array  slide elements down

Deque ADT: Dynamic Array Solution One solution: Rather than always use index zero as our starting point, allow the starting index to “float” Maintain two integer values: Starting or beginning index (beg) Count of elements in the collection (cnt) Still need to reallocate when size equal to capacity

Deque: Dynamic Array Implementation Create a dynamic array version of the ring buffer queue: First filled element no longer always at index 0 Filled elements may wrap around back to the front end of array Called ArrDeque first (beg) first (beg) count (cnt) count (cnt)

Dynamic Array Deque: Structure struct ArrDeque { TYPE *data; /* Pointer to data array. */ int cnt; /* 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 = (TYPE *)malloc(cap * TYPE)); assert(d->data != 0); d->cnt = d->beg = 0; d->cap = cap; }

Inserting & Removing from Back Adding to back is easy, just adjust count of the number of elements Still need to reorganize if adding and size = capacity Add Remove first (beg) first (beg) count (cnt) count (cnt) first (beg) first (beg) count (cnt) count (cnt)

Inserting & Removing from Front Changes to front are easy, just adjust count and starting location Add Remove first (beg) first (beg) count (cnt) count (cnt) first (beg) first (beg) count (cnt) count (cnt)

Wrapping Around Problem: Elements can wrap around from start to end first (beg) count (cnt)

Handling Wrap-Around: Computing Index Calculate offset: add logical (element) index to start (beg) offset = beg + logicalIndex; /* logIndex = 3, offset = 9 */ If larger than capacity, subtract capacity if (offset > cap) absoluteIndex = offset – capacity; If smaller than zero, add cap if (offset < 0) absoluteIndex = offset + capacity; That way sizes are always in range Or..combine into single statement with mod: /* Convert logical index to absolute index. */ absIdx = (logicalIdx + beg) % cap; beg = 6 cap = 8 beg cnt 0 1 2 3 4 5 6 7 beg = 0 cap = 8 beg cnt 0 1 2 3 4 5 6 7

Your Turn: Worksheet Time Write the implementation of the dynamic array deque

Comparison for stack, queue, deque implementations front() back() addFront() addBack() (push, enqueue) remove Front() (dequeue) Back() (pop) get(i) dynArrStack n/a O(1) O(1) ave. O(n) worst listStack O(1) always listQueue listDeque arrayDeque O(1) ave

Midterm Exam – Friday To Study Topics Exam Master all worksheets Read lessons Practice exam Lecture Notes Topics Big O ADTs (stack, queue, deque and applications) Implementations: dynArr, listStack, listQueue, listDeque, arrayDeque Exam Closed book, closed notes, no calculator short answer, reading code, writing code