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

Similar documents
Abstract Data Types - Lists Arrays implementation Linked-lists implementation

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

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

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

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Data Structures Lecture 5

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

CSC212:Data Structure

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

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

COMP 250 Midterm #2 March 11 th 2013

Lecture 12: Doubly Linked Lists

ITI Introduction to Computing II

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

Priority Queue ADT. Revised based on textbook author s notes.

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

DS L9: Queues

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Queues. Queue ADT Queue Implementation Priority Queues

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Linked Lists. Singly Linked Lists (3.2) CSE 2011 Winter /01/2011

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

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

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

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

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Queues. Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid. Java: Queues / 1

Week 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Computer Science 136 Exam 2

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

Your Topic Goes Here Queue

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Lecture 3 Linear Data Structures

Computer Science 210: Data Structures. Linked lists

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Data Abstraction and Specification of ADTs

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

COMP 103. Data Structures and Algorithms

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

CS350: Data Structures. Doubly Linked Lists

COS 226 Algorithms and Data Structures Fall Midterm

Lecture 3 Linear Data Structures

ITI Introduction to Computing II

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

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

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

Solutions to a few of the review problems:

Lists. ordered lists unordered lists indexed lists 9-3

CSC 1052 Algorithms & Data Structures II: Queues

CISC 323 Intro to Software Engineering

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Chapter 13: Collections Lab Exercises

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

CSC 1052 Algorithms & Data Structures II: Linked Queues

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

CSCD 326 Data Structures I Queues

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

1. Introduction. Lecture Content

CS171 Midterm Exam. October 29, Name:

Notes on access restrictions

Queues Fall 2018 Margaret Reid-Miller

COMP 250. Lecture 8. stack. Sept. 25, 2017

More Data Structures (Part 1) Stacks

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

csci 210: Data Structures Stacks and Queues

Te Whare Wananga o te Upoko o te Ika a Maui EXAMINATIONS 2003 END-YEAR COMP 103. Data Structures and Algorithms

Stacks and queues (chapters 6.6, 15.1, 15.5)

Lecture 4. The Java Collections Framework

The Java Collections Framework. Chapters 7.5

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University


Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Appendix A: Interfaces and Classes in the AP Java Subset (AB)

COMP9024: Data Structures and Algorithms

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

== isn t always equal?

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

Stack. The Stack stores arbitrary objects Insertions and deletions follow the last-in firstout

Stacks Goodrich, Tamassia, Goldwasser Stacks

Laboratorio di Algoritmi e Strutture Dati

Lecture 3: Stacks & Queues

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

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Unit 4 Basic Collections

CS/CE 2336 Computer Science II

csci 210: Data Structures Linked lists Summary Arrays vs. Lists Arrays vs. Linked Lists a[0] a[1] a[2]... null Today READING: Arrays

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

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

Foundations of Data Structures

CSEN 301 Data Structures and Algorithms

Class 26: Linked Lists

CS 307 Final Spring 2008

Transcription:

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

COMP250: Queues, deques, and doubly-linked lists Lecture 20 Jérôme Waldispühl School of Computer Science McGill University Based on slides by Mathieu Blanchette

Queues Rear of queue Front of queue Queue: First-in First-out data structure (FIFO) Applications: Any first-come first-serve service

Queues operations void enqueue (Object o) Add o to the rear of the queue Object dequeue() Removes object at the front of the queue. Exception thrown if queue is empty (N.B. other implementations also return the object) Object front() Returns object at the front of the queue but doesn't remove it from the queue. Exception if queue empty. int size() Returns the number of objects in the queue boolean isempty() returns True is queue is empty

Queue q = new Queue() q.enqueue( one ) q.enqueue( two ) q.enqueue( three ) print q.size() print q.front() q.dequeue() print q.front() q.dequeue() print q.front() print q.isempty() Example 3 one two three False

Queues with linked-lists One two three Front = Head Queue operation enqueue(object o) dequeue() front() empty() size() Rear = Tail Linked-list operation Running time addlast(o) removefirst() getfirst() empty() size() O( ) O( ) What would happen if we used instead the convention: "Front of queue = tail, Rear of queue = head"? 1 n

removelast() on single linked list would need access to the predecessor of the last node. "Go" "Habs" "Go!" null head tail "Go" "Habs" "Go!" null head tail Go" Habs" null head tail

Double-ended queues A double-ended queue (a.k.a. "deque") allows insertions and removal from the front and back. head tail

Implementation Dequeoperations with linked-lists Object getfirst() Object getlast() addfirst(object o) addlast(object o) O( 1 ) boolean isempty() Object removefirst() Object removelast() int size() O( n ) Why?

Deques and doubly-linked-lists Problem: removelast runs in O(n) with linked lists To do it faster, each node has to have a reference to the previous node in the list prev value next prev value next prev value next Let head it be tail class node { node prev, next; Object value; node(object val, node p, node n); node getprev(); void SetPrev(node n); node getnext(); void SetNext(node n); Object getvalue(); void setvalue(object o); }

Operations on doubly-linked-lists Object removelast() throws Exception { if (tail==null) throw new Exception("Empty deque"); Object ret = tail.getvalue(); tail = tail.getprev(); Now in O(1)! if (tail==null) { head=null; } else { tail.setnext(null); } return ret; // If we return the object removed } void addfirst(object o) { node n = new node(o, null, head); if (head!= null) { head.setprev( n ); } else { tail = n; } head = n; } Exercise: Write all other deque methods using a doubly-linked-list

Implementing deques with arrays Suppose we know in advance the deque will never contain more than N elements. We can use an array to store elements in the deque Keep track of indices for head and tail L 0 1 2... N-1 addlast(o) { tail = tail + 1; L[tail] = o; } addfirst(o) { head = head - 1; L[head] = o } removelast { tail = tail 1; } removefirst { head = head + 1; }

Implementing deques with arrays O N A D I E head tail addlast(n) O N A D I E N N A D I E N removefirst() A N A D I E N A N A D I E N addfirst(a) addlast(s) Full!

Rotating arrays Idea: To avoid outofbounds exceptions, have indices wrap around : (N-1) + 1 = 0 0-1 = N-1 N-2 Equivalent to arithmetic modulo N a mod N = rest of integer division a/n 3 mod 7 = 3 7 mod 7 = 10 mod 7 = 0 3 N-1 With a rotating array, the deque will never go out of bounds, but may overwrite itself if we try to put more than N elements into it. How can we check if the deque is full (has N elements?) x 0... 1 2

Implementing deques with arrays O N A D I E head tail addlast(n) O N A D I E N N A D I E removefirst() A N A D I E N S A N A D I E N addfirst(a) addlast(s)

Operations on deques with Array Enqueue(o) throw Exception { if ( isfull() ) { throw new Exception( Full ) } tail = ( ( tail + 1 ) % N ) ; L[tail] = o; } Dequeue() { If ( isempty() ) { throw new Exception( Empty ) } Object o = L[head]; head = ( ( head + 1 ) % N ); return o; // If return object } Exercise: Write all other deque methods using a rotating array. What are the index of an empty list?

Operations on deques with Array Head and Tail index are initialized at -1 Enqueue and Dequeue must handle specific cases: - There is only one object in deque when we remove an element. - We insert the first element in the file (head & tail must be updated!) - Clean implementation of isempty() and isfull().