SYSC 2006 Winter 2012 Linear Collections: Queues

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

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

SYSC 2006 C Winter 2012

ITI Introduction to Computing II

.:: UNIT 4 ::. STACK AND QUEUE

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

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

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

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

Data Structures and Algorithms for Engineers

Chapter 9 STACK, QUEUE

Chapter 18: Stacks And Queues

Queue with Array Implementation

Chapter 18: Stacks And Queues

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

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

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Motivation for Templates

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

Lecture Notes on Queues

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

CS302 - Data Structures using C++

Lecture Notes on Priority Queues

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

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

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Programming. Lists, Stacks, Queues

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Lecture 10 Notes Linked Lists

SYSC 2006 CD. Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

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

Procedural Programming & Fundamentals of Programming

Lecture 10 Notes Linked Lists

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

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

Data structure and algorithm in Python

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

Information Science 2

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Ashish Gupta, Data JUET, Guna

csci 210: Data Structures Stacks and Queues

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

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

Lecture Notes on Queues

ECE 2400 Computer Systems Programming Fall 2018 Topic 9: Abstract Data Types

Queue Linked List Implementation

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

Stack and Queue. Stack:

1 P age DS & OOPS / UNIT II

Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

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

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

ECE 2400 Computer Systems Programming Fall 2017 Topic 9: Abstract Data Types

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

Data Structures and Algorithms

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

Queues. Queue ADT Queue Implementation Priority Queues

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

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

An Introduction to Queues With Examples in C++

Lecture 17 Notes Priority Queues

Lecture 17 Priority Queues

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

3137 Data Structures and Algorithms in C++

Queues. CITS2200 Data Structures and Algorithms. Topic 5

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

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY


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

Lecture Data Structure Stack

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

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

ADTs: Stacks and Queues

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

SYSC Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Your Topic Goes Here Queue

BBM 201 DATA STRUCTURES

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Linked Structures. See Section 3.2 of the text.

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

CS 222: Linked Lists, Queues, Stacks

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

UNIT-2 Stack & Queue

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Motivation for Queues

Data Structure. Recitation VII

Algorithms, Data Structures, and Problem Solving

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Queue rear tail front head FIFO

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Transcription:

SYSC 2006 Winter 2012 Linear Collections: Queues Copyright 2000-2012 D.L. Bailey, Systems and Computer Engineering, Carleton University revised March 20, 2011, November 28, 2011, March 30, 2012

Definition A queue is a collection in which the elements are maintained in the order in which they were added A queue is a first-in, first out (FIFO) collection - the first element is the first one retrieved or removed SYSC 2006 - Queues 2

Abstract View create queue add (enqueue) 5 5 add (enqueue) 3 add (enqueue) 7 remove (dequeue) element (returns 5) remove (dequeue) element (returns 3) queue 7 7 7 3 3 3 5 5 queue SYSC 2006 - Queues 3

Implementing a Queue Because elements are always added at the of the queue, but are retrieved and removed from the of the queue, we should pick a data structure that permits efficient manipulation of both ends of the queue there are no operations that insert elements into or remove elements from the middle of a queue, so efficient access to that part of the data structure is not important SYSC 2006 - Queues 4

Array-Based Queue Data Structure An array-based implementation could be similar to the array-based list struct intarrayqueue { int *elems; int capacity; int size; }; typedef struct intarrayqueue IntQueue; SYSC 2006 - Queues 5

Thinking About the Queue Operations We define elems[0] to be the of the queue and elems[size-1] to be the of the queue enqueue running time will be constant - why? running time will be constant - why? dequeue running time will be proportional to the # of items in the queue why? dequeue would be more efficient (its running time reduced) if elements in the array weren't shifted to the left when the item at position 0 is removed SYSC 2006 - Queues 6

Revised Queue Data Structure struct intarrayqueue { int *elems; int capacity; int size; // # of elements in the Q int ; int ; }; typedef struct intarrayqueue IntQueue; SYSC 2006 - Queues 7

Revised Queue Data Structure is the index of the element in the queue; i.e., the index of the array element from which the next integer will be removed is the index of the element in the queue; i.e., the index of the array element where the last integer was stored SYSC 2006 - Queues 8

Revised Queue Data Structure Initially, is 0, is -1, and size is 0 After 5, 3, 7 have been added to the queue: size 3 0 elems 2 5 3 7 [0] [1] [2]?? [3] [4] SYSC 2006 - Queues 9

Rightward Drift Assume that 5, 3, 7, 2, 8 were added to the queue, and 5 and 3 have been removed from the queue Now, == capacity of queue - 1, but there is room in the queue at the beginning of the array for 2 more elements (elems[0], elems[1]) size 3 2 4 elems?? 7 [0] [1] [2] 2 8 [3] [4] SYSC 2006 - Queues 10

Handling Rightward Drift Approach 1: ensure always == 0 after an element is removed from the queue, shift all remaining elements in the array one position to the left, decrement by 1 (an O(n) algorithm) that's what we wanted to avoid e.g., after adding 5, 3, 7, 2, 8, removing 5, 3: size 3 0 2 elems 7 2 8 [0] [1] [2]?? [3] [4] SYSC 2006 - Queues 11

Handling Rightward Drift Approach 2: shift elements only as required when we try to enqueue an element, if == capacity of queue - 1, shift all elements in the array to the left, then append the element (an O(n) algorithm) e.g., after adding 5, 3, 7, 2, 8, removing 5, 3, then adding 6 size 3 0 3 7 2 8 [0] [1] [2] 6? [3] [4] SYSC 2006 - Queues 12

Handling Rightward Drift Approach 3: visualize the array as a circular array (sometimes called a ring buffer) e.g., a queue with capacity = 8 size 4 2 5 [6] [5] [7] SYSC 2006 - Queues 13 [0] [1] 3 4 [2] 6 3 [4] [3]

Handling Rightward Drift When or reaches capacity of queue -1, its value wraps around to 0 size 7 2 0 [6] [5] [7] 7 3 [4] 2 6 [0] 12 3 [3] [1] 4 [2] SYSC 2006 - Queues 14

Full Queue is one slot ahead of, size == 8 size 8 2 [6] [7] 7 2 [0] 12 9 [1] 1 [5] 3 [4] 6 3 4 [3] [2] SYSC 2006 - Queues 15

Empty Queue is one slot ahead of, size == 0 size 0 2 [6] [7]??? [0]? [1] 1 [5]? [4]??? [3] [2] SYSC 2006 - Queues 16

Construct an Empty Queue IntQueue *intqueue_construct(int capacity) { assert(capacity > 0); IntQueue *qp = malloc(sizeof(intqueue)); assert(qp!= NULL); int *pa = malloc(capacity * sizeof(int)); assert(pa!= NULL); SYSC 2006 - Queues 17

Construct an Empty Queue } qp->data = pa; qp->capacity = capacity; qp->size = 0; qp-> = 0; qp-> = capacity - 1; return qp; SYSC 2006 - Queues 18

Determining the Queue's State _Bool intqueue_is_empty(const IntQueue *qp) { assert(qp!= NULL); return qp->size == 0; } _Bool intqueue_is_full(const IntQueue *qp) { assert(qp!= NULL); return qp->size == qp->capacity; } SYSC 2006 - Queues 19

Determining the Queue's State int intqueue_size(const IntQueue *qp) { assert(qp!= NULL); return qp->size; } SYSC 2006 - Queues 20

Insert/Retrieve/Remove Elements intqueue_enqueue() returns true if an element was inserted, false if the queue is full (element not inserted) intqueue_() returns true if an integer was retrieved (and assigned to the variable pointed to by parameter element), false if the queue is empty intqueue_dequeue() returns true if an integer was retrieved and removed (and assigned to the variable pointed to by parameter element), false if the queue is empty SYSC 2006 - Queues 21

Insert an Element into a Queue _Bool intqueue_enqueue(intqueue *qp, int element) { assert(qp!= NULL); if (intqueue_is_full(qp)) return false; qp-> = (qp-> + 1) % qp->capacity; qp->data[qp->] = element; qp->size++; return true; } SYSC 2006 - Queues 22

Retrieve an Element from a Queue _Bool intqueue_(intqueue *qp, int *element) { assert(qp!= NULL); if (intqueue_is_empty(qp)) return false; *element = qp->data[qp->]; return true; } SYSC 2006 - Queues 23

Remove an Element from a Queue _Bool intqueue_dequeue(intqueue *qp, int *element) { assert(qp!= NULL); if (intqueue_is_empty(qp)) return false; *element = qp->data[qp->]; qp-> = (qp-> + 1) % qp->capacity; qp->size--; return true; } SYSC 2006 - Queues 24