CS 261. Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

Similar documents
CS 261: Data Structures. Dynamic Array Queue

CS 261: Data Structures. Dynamic Array Queue

CS 261. List Bag List Queue List Deque. by Tim Budd Ron Metoyer Sinisa Todorovic

CS 261. Dynamic Arrays Introduction by Tim Budd Ron Metoyer Sinisa Todorovic

CS 261: Data Structures. Dynamic Arrays. Introduction

CSC 1052 Algorithms & Data Structures II: Queues

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1


Lesson 8: Linked List Deque

.:: UNIT 4 ::. STACK AND QUEUE

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Stack and Queue. Stack:

CS24 Week 4 Lecture 2

Practice test for Midterm 1; solutions

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

CS : Data Structures

csci 210: Data Structures Stacks and Queues

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Dynamic Data Structures

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Chapter 18: Stacks And Queues

Algorithms, Data Structures, and Problem Solving

Data Structure. Recitation VII

Ashish Gupta, Data JUET, Guna

ITI Introduction to Computing II

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Chapter 18: Stacks And Queues

! A data type for which: ! An ADT may be implemented using various. ! Examples:

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Apply to be a Meiklejohn! tinyurl.com/meikapply

Lecture 2: Stacks and Queues

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CS 216 Exam 1 Fall SOLUTION

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Queues. CITS2200 Data Structures and Algorithms. Topic 5

LIFO : Last In First Out

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

CS 211. Project 5 Infix Expression Evaluation

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Data Structures. Lecture 5 : The Queues

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Randomized Queues and Deques

Computer Science. Section 1B

4. Static Data Structures

CSC 1052 Algorithms & Data Structures II: Linked Queues

Linked List. April 2, 2007 Programming and Data Structure 1

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Heaps, stacks, queues

Basic Data Structures

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Cpt S 122 Data Structures. Data Structures

Programming. Lists, Stacks, Queues

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Information Science 2

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

1 P age DS & OOPS / UNIT II

Standard ADTs. Lecture 19 CS2110 Summer 2009

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Your Topic Goes Here Queue

DEEPIKA KAMBOJ UNIT 2. What is Stack?

SYSC 2006 Winter 2012 Linear Collections: Queues

Linear Data Structures

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Stacks and Queues. CSE Data Structures April 12, 2002

CMPT 125: Practice Midterm Answer Key

Queue with Array Implementation

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Elementary Data Structures: Lists

CS2012 Programming Techniques II

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Data structure and algorithm in Python

CMSC 132: Object-Oriented Programming II. Stack and Queue

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Abstract Data Types. Definitions, Implementations, and Uses

Queues. Queue ADT Queue Implementation Priority Queues

CSC 273 Data Structures

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

Container Class and Integrators, Proxy Class EC6301-OOPS AND DATA STRUCTURES

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Functional Data Structures for Typed Racket. Hari Prashanth and Sam Tobin-Hochstadt Northeastern University

Advanced C Programming and Introduction to Data Structures

Transcription:

CS 261 Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

Dynamic Array -- Review Positives: Each element easily accessed Grows as needed The user unaware of memory management

Stack as Dynamic Array -- Review Remove and add elements from top Occasional capacity increase Remove operation has complexity O(1) Add operation has complexity O(1)+ O(1) expected O(n) worst case

Bag as Dynamic Array -- Review Order is unimportant, so adding to end Remove always requires moving elements Add is O(1)+ O(n) worst case Remove is O(n)

Queue

Queue Elements are inserted at one end, and removed from another E.g. queue of people First in, first out (FIFO)

Interface View of Queue addback(newelement) -- inserts an element front() -- returns the first element removefront() -- removes the first element isempty() -- checks if the queue is empty

Queue as Dynamic Array Which end is better for insertion? Which end is better for removal? What would be O(?)?

Removing from Front, Adding to Back Remove: 2 4 7 3 9 1 4 7 3 9 1 Remove requires moving elements => O(n) Insertion to the end is O(1)+

Removing from Back, Adding to Front Insertion: Insertion requires moving elements => O(n) Removal from the end is O(1)

Double-Ended Queue = Deque

Deque Allows: Insertions at both front and back Removals at both front and back Also called, double-ended stack

Interface View of Deque addfront(newelem) -- inserts to the front addback(newelem) -- inserts to the back front() -- returns the first front element back() -- returns the first back element removefront() -- removes from the front removeback() -- removes from the back isempty() -- checks if the queue is empty

Dequeue as Dynamic Array Key idea: Do not tie "front" to index zero Instead, allow both "front" and "back" to float around the array

Example start back_idx

Example DataSize = 6 DataStart = 7 Data = 9 1 back_idx 2 4 7 3 start

Deque Implementation struct deque { TYPE * data; int capacity; int size; int start; };

Keeping size vs Keeping pointer to end We compute the index of end from the index of front and size Why not keep the index of end? OK, but need to compute size frequently

Adding/Removing for Deque Add to front: back off starting point by 1 Add to back: increase size by 1 Remove from front: increase starting point by 1 Remove from back: decrease size by 1

Adding/Removing for Deque What if elements wrap around? DataSize = 6 DataStart = 7 Data = 9 1 2 4 7 3

Wrapping: How to Compute New Index If Index < 0, then add capacity If Index > capacity, then subtract capacity If size == capacity, reallocate new buffer DataSize = 6 DataStart = 7 Data = 9 1 2 4 7 3

Wrapping: How to Compute New Index Use the mod operator: backindex = (da->start + da->size) % da->cap;

Implementation

Deque Structure struct deque { TYPE * data; int capacity; int size; int start; };

initdeque void initdeque (struct deque *d, int initcapacity) { d->size = d->start = 0; assert(initcapacity > 0); d->capacity = initcapacity; d->data = (TYPE *) malloc(initcapacity * sizeof(type)); assert(d->data!= 0); }

Double the Capacity void _doublecapdeque (struct deque *d) { TYPE * olddata = d->data; int oldstart = d->start; int oldsize = d->size; int oldcapacity = d->capacity; int j; initdeque(d, 2 * oldcapacity); for (j = 0 ; j < oldsize; j++) { d->data[j] = olddata[oldstart++]; } if (oldstart >= oldcapacity) oldstart = 0; } free(olddata); d->size = oldsize;

addbackdeque void addbackdeque(struct deque *d, TYPE val) { int back_idx; if (d->size >= d->capacity) _doublecapdeque(d); /* Increment the back index */ back_idx = (d->start + d->size) % d->capacity; d->data[back_idx] = val; } d->size ++; DataSize = 6 DataStart = 7 Data = 9 1 back_idx 2 4 7 3 start

addfrontdeque void addfrontdeque(struct deque *d, TYPE val) { if (d->size >= d->capacity) _doublecapdeque(d); /* Decrement the front index */ d->start--; if (d->start < 0) d->start += d->capacity; d->data[d->start] = val; } d->size ++; DataSize = 6 DataStart = 7 Data = 9 1 start 2 4 7 3

Worksheet 20 Implement Dynamic Array Deque How do you Add to front or back? Return front? Return back? Remove front? Remove back?

Dynamic Array -- Problems Data kept in a single large block of memory Often more memory used than necessary especially when repeatedly growing and shrinking the dynamic array