CS 261: Data Structures. Dynamic Array Queue

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

CS 261: Data Structures. Dynamic Array Queue

CS 261: Data Structures. Dynamic Arrays. Introduction

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

Lesson 8: Linked List Deque


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

Dynamic Data Structures

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

CSC 1052 Algorithms & Data Structures II: Queues

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

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

Stack and Queue. Stack:

CS24 Week 4 Lecture 2

CS 211. Project 5 Infix Expression Evaluation

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

CS : Data Structures

Algorithms, Data Structures, and Problem Solving

.:: UNIT 4 ::. STACK AND QUEUE

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

Ashish Gupta, Data JUET, Guna

Practice test for Midterm 1; solutions

csci 210: Data Structures Stacks and Queues

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

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

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

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

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

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

Chapter 18: Stacks And Queues

Assist. Prof. Dr. Caner ÖZCAN

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

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

Midterm Review. CS 211 Fall 2018

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

LIFO : Last In First Out

4. Static Data Structures

Chapter 18: Stacks And Queues

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

Data Structure. Recitation VII

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

ITI Introduction to Computing II

CS 241 Data Organization Binary Trees

Cpt S 122 Data Structures. Data Structures

FIFO Queues. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Heaps, stacks, queues

Stack & Queue on Self-Referencing Structures

In Java we have the keyword null, which is the value of an uninitialized reference type

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

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

CSC 1052 Algorithms & Data Structures II: Linked Queues

Linear Data Structures

Apply to be a Meiklejohn! tinyurl.com/meikapply

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CS 322 Operating Systems Practice Midterm Questions

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

CSC 273 Data Structures

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Arrays and Linked Lists

Information Science 2

Elementary Data Structures: Lists

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

Heap Arrays and Linked Lists. Steven R. Bagley

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

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Ashish Gupta, Data JUET, Guna

Advanced C Programming and Introduction to Data Structures

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

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Stacks and Queues. CSE Data Structures April 12, 2002

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

Object Oriented Programming COP3330 / CGS5409

Data Structures and Algorithms (DSA) Course 5 Lists. Iulian Năstac

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

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Stack & Queue on Self-Referencing Structures

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Homework #3 CS2255 Fall 2012

Introduction to C++ Introduction to C++ 1

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Algorithms & Data Structures

Data structure and algorithm in Python

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

Elementary Data Structures: Lists

Abstract Data Types. Definitions, Implementations, and Uses

CS5460: Operating Systems

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

Programming. Lists, Stacks, Queues

Linked Lists and other Dynamic Data Structures

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CS 261 Data Structures. Priority Queue ADT & Heaps

Data Structures. Lecture 5 : The Queues

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Stacks, Queues (cont d)

CS 216 Exam 1 Fall SOLUTION

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

.:: UNIT 3 ::. LIST AND LINKED LIST

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Transcription:

CS 261: Data Structures Dynamic Array Queue

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

Stack as Dynamic Array -- Review Remove and add elements from/to top Occasional capacity increase Remove operation has complexity O(1) Add operation has complexity O(1) 3

Bag as Dynamic Array -- Review Order is not important, so adding to the end Add is O(1), with occasional capacity increase Remove is O(n) 4

Double-Ended Stack = Deque 5

Deque Allows: Insertions at both front and back Removals at both front and back 6

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 7

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

Example Deque In this example, start index is larger than back index DataSize = 6 DataStart = 7 Data = 9 1 backindex 2 4 7 3 startindex 9

Adding/Removing for Deque Add front: decrease the start index by 1 Add back: increase size by 1 DataSize = 6 DataStart = 7 Data = backindex++ 9 1 backindex startindex-- 2 4 7 3 startindex 10

Adding/Removing for Deque Remove front: increase the start index by 1 Remove back: decrease size by 1 DataSize = 6 DataStart = 7 Data = backindex-- 9 1 backindex startindex++ 2 4 7 3 start 11

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

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 13

Implementation 14

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

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

Wrapping: How to Compute Back Index Use the mod operator: backindex = (start + size) % capacity; DataSize = 6 DataStart = 7 Data = 9 1 backindex 2 4 7 3 startindex 17

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

Double the Capacity void _doublecapdeque (struct deque *d) { TYPE * olddata = d->data; /*memorize old data*/ int oldstart = d->start; /*memorize old start*/ int oldsize = d->size; /*memorize old size*/ int oldcapacity = d->capacity; /*memorize old cap.*/ int j; initdeque(d, 2 * oldcapacity); /*new memory alloc.*/ for (j = 0 ; j < oldsize; j++) {/*copy back old data*/ d->data[j] = olddata[oldstart++]; if (oldstart >= oldcapacity) oldstart = 0; } free(olddata); d->size = oldsize; } 19

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 20

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 21

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

Queue 23

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

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 25

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

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) 27

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

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 29