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

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

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

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

8. Fundamental Data Structures

Lecture 12: Doubly Linked Lists

Stack and Queue. Stack:

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Information Science 2

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

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

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

Queue. COMP 250 Fall queues Sept. 23, 2016

ITI Introduction to Computing II

CS/CE 2336 Computer Science II

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

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

Queues Fall 2018 Margaret Reid-Miller

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

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

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


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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

1 P age DS & OOPS / UNIT II

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Stacks and queues (chapters 6.6, 15.1, 15.5)

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

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

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Implementing Lists, Stacks, Queues, and Priority Queues

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CSC 321: Data Structures. Fall 2012

Computer Science 62. Midterm Examination

CS350 - Exam 1 (100 Points)

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

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

PA3 Design Specification

csci 210: Data Structures Stacks and Queues

Computer Science 136 Exam 2

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

COMP 250 Midterm #2 March 11 th 2013

THE UNIVERSITY OF WESTERN AUSTRALIA

CSC 321: Data Structures. Fall 2013

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

182 review 1. Course Goals

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks and Queues. David Greenstein Monta Vista

Class 26: Linked Lists

Lecture 4. The Java Collections Framework

The Java Collections Framework. Chapters 7.5

CSC212:Data Structure

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

Motivation for Queues

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

Procedural Programming & Fundamentals of Programming

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

Summer Final Exam Review Session August 5, 2009

Tutorial 2: Linked Lists

Introduction to Computing II (ITI 1121) Final Examination

Lecture Data Structure Stack

ITI Introduction to Computing II

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

COS 226 Algorithms and Data Structures Fall Midterm

Lesson 3: Understanding General Software Development

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Stacks and Queues. CSE 373 Data Structures Lecture 6

CE204 Data Structures and Algorithms Part 2

CSE 230 Intermediate Programming in C and C++

Linear Data Structures

Sequence Abstract Data Type

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Programming. Lists, Stacks, Queues

Programming II (CS300)

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

CSC 1052 Algorithms & Data Structures II: Linked Queues

Queue rear tail front head FIFO

Lecture 3: Stacks & Queues

Linked Lists in C and C++

DS L9: Queues

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CSEN 301 Data Structures and Algorithms

ADTs, Arrays, and Linked-Lists

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Midterm Exam (REGULAR SECTION)

CH7. LIST AND ITERATOR ADTS

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

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

CS 206 Introduction to Computer Science II

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

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

Ashish Gupta, Data JUET, Guna

An Introduction to Data Structures

+ Abstract Data Types

.:: UNIT 4 ::. STACK AND QUEUE

CH7. LIST AND ITERATOR ADTS

Transcription:

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

Agenda Mid Semester Analysis Doubly Linked List Queue 2

Administrative None 3

Doubly Linked List Operations completed: addfirst add(index) removelast addlast removefirst get(index) 4

Recall - Singly Linked List Complexity AddFirst/ RemoveFirst? Add/Remove (at index)? Get (using index)? AddLast/RemoveLast? 5

Recall - Singly Linked List Complexity AddFirst/ RemoveFirst : O(1) Add/Remove (at index) : O(n) Get (using index) : O(n) AddLast/RemoveLast : O(n) 6

Doubly Linked List Complexity AddFirst/ RemoveFirst? Add/Remove (at index)? Get (using index)? AddLast/RemoveLast? 7

Doubly Linked List Complexity AddFirst/ RemoveFirst : O(1) Add/Remove (at index) : O(n) Get (using index) : O(n) AddLast/RemoveLast : O(1) 8

Queue 9

Queue A queue is a FIFO sequence. Addition takes place only at the tail, and removal takes place only at the head. Like a stack, queue can contain any object (Generics). The basic operations for queues are identical to those for stacks, except the standard name for adding elements into a queue is enqueue (instead of push) and the name for removing elements from a queue is dequeue. 10

Applications Line of cars at a light Line of people at cafe Printer buffer 11

Queue Operations The basic operations are : enqueue(x): add an item at the tail dequeue(): remove the item at the head peek(): return the item at the head (without removing it) size(): return the number of items in the queue isempty(): return whether the queue has no items 12

Queue Operations Queue.add(e): It throws an exception if the operation fails. Same for Queue.remove() and Queue.element(): Queue.offer(e): It returns a special value(e.g. null or false) if the operation fails. Same for Queue.poll() and Queue.peek(): OPERATION THROWS EXCEPTION SPECIAL VALUE Insert add(e) offer(e) Remove remove() poll() Examine element() peek() Source: https://docs.oracle.com/javase/8/docs/api/java/util/queue.html 13

Implementation We can use many ways to implement queues: Array: enqueue is easy, dequeue requires shifting the rest in the array ArrayList: enqueue is easy, ArrayList takes care of the shifting cased by dequeue, resize memory is necessary SinglyLinkedList: dequeue is easy, but enqueue need to traverse to the end of the list DoublyLinkedList: enqueue (addlast) and dequeue (removefirst/remove(0)) are easy. 14

Array-Based Queue We can use an array of fixed capacity to store items as a queue. enqueue: append new items to the end of the queue dequeue: remove items from the front of the queue, and shift the rest of the items. Enqueue is okay, but dequeue is not efficient due to the shifting. How to make the array-based queue more efficient? Have two indexes (like the head and tail pointers): rear index and front index. Enqueue new items to the rear index Dequeue items from the front index 15

Array-Based Queue Issue: Rear index may reach the last array index (reach the capacity) while the front index may have available space (due to the dequeue) Solution: Allocate more space (double the size) Wrap around (circular array). 16

Queue Operations 17

Queue Operations 18

Queue Operations 19

Circular Array In the circular array, we use modular arithmetic to calculate the from and rear indexes. Assume the length of the array is m. front++ gets replaced with front = (front + 1) % m; first element in the queue rear++ gets replaced with rear = (rear + 1) % m; next available space 20

Circular Array But it is still possible for the buffer to fill if using circular array. How can we tell? If the rear wrap around and catches up with the front (if (rear + 1) % m == front), it is full. Or we can keep track of the count and the capacity. If the array is full, we can also reallocate the array and copy the queue data into the new array starting at position 0 again. 21

Array-based Queue Implementation Let s write some code! 22

Queue Complexity Enqueue()? Dequeue()? Peek()? 23