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

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

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

CMPT 125: Practice Midterm Answer Key

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

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

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

Data Structures and Algorithms for Engineers

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

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

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.

List, Stack and Queue Implementation

ITI Introduction to Computing II

UNIT-2 Stack & Queue

Stacks and Queues. CSE Data Structures April 12, 2002

Chapter 9 STACK, QUEUE

Solution for Data Structure


Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Abstract Data Types. Definitions, Implementations, and Uses

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

.:: UNIT 4 ::. STACK AND QUEUE

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

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Arrays and Linked Lists

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

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

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

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

Queues. CITS2200 Data Structures and Algorithms. Topic 5

CS 261: Data Structures. Dynamic Array Queue

Hash Tables. Hash functions Open addressing. November 24, 2017 Hassan Khosravi / Geoffrey Tien 1

School of Electrical Engineering & Computer Science Oregon State University. CS Recitation 3 Spring 2012

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

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

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

CSC 1052 Algorithms & Data Structures II: Linked Queues

Ashish Gupta, Data JUET, Guna

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Queue ADT. CMPT 125 Feb. 28

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

CE204 Data Structures and Algorithms Part 2

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

8. Fundamental Data Structures

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

SYSC 2006 Winter 2012 Linear Collections: Queues

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

Data Structure - Stack and Queue-

Data Structures and Algorithms. Roberto Sebastiani

Introduction to Data Structures and Algorithms

Stack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Lecture Data Structure Stack

C Programming, Autumn 2013, Exercises for the Second Week

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

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

Basic Data Structures

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stack and Queue. Stack:

1 P age DS & OOPS / UNIT II

Queues Fall 2018 Margaret Reid-Miller

Basic Data Structures 1 / 24

An Introduction to Queues With Examples in C++

Chapter 20: Binary Trees

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

DS Assignment II. Full Sized Image

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

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

Queue: Queue Representation: Basic Operations:

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

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

CSC 1052 Algorithms & Data Structures II: Queues

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

CS24 Week 4 Lecture 2

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

DS L9: Queues

CS 2412 Data Structures. Chapter 9 Graphs

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

BBM 201 DATA STRUCTURES

Final Exam Data Structure course. No. of Branches (5)

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

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

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

Queue Linked List Implementation

Linked Structures. See Section 3.2 of the text.

Midterm Review. CS 211 Fall 2018

Programming. Lists, Stacks, Queues

Stacks and Queues. CSE 373 Data Structures Lecture 6

15. Stacks and Queues

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

Chapter 17: Linked Lists

This document contains the questions and solutions to the CS107 midterm given in Winter 2018 by instructor Chris Gregg. This was a 120-minute exam.

Queue with Array Implementation

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

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Linked Lists and other Dynamic Data Structures

STACKS AND QUEUES. Problem Solving with Computers-II

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

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Chapter 17: Linked Lists

Transcription:

Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from the front of the queue peek return the element at the front of the queue isempty does the queue contain any items initialization, etc. The Queue ADT is completely described by the above behaviour a client using the queue only needs to understand how to call the functions like the stack, queue ADT can be implemented using different data structures, transparently to client code October 20, 2017 Hassan Khosravi / Geoffrey Tien 2

Queue implementation Using an array Insertions happen at the back of the queue Let's try making the back of the array the back of the queue and the front of the array the front of the queue If the front is always index 0, we need to shuffle with every dequeue O n So insertions will increment the back index, and Removals will increment the front index front myqueue -1 012 enqueue 8 dequeue dequeue back capacity 45 6 arr 4 7 3 1 6 8 Problem? October 20, 2017 Hassan Khosravi / Geoffrey Tien 3

Circular arrays Trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end of the array wraps around to the start of the array 7 0 6 1 0 1 2 3 4 5 6 7 5 2 4 3 October 20, 2017 Hassan Khosravi / Geoffrey Tien 4

The modulo operator The mod operator (%) calculates remainders: 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3 The mod operator can be used to calculate the front and back positions in a circular array Thereby avoiding comparisons to the array size The back of the queue is: (front + num) % capacity where num is the number of items in the queue After removing an item, the front of the queue is: (front + 1) % capacity typedef struct { int front; int num; int capacity; int* arr; Queue; October 20, 2017 Hassan Khosravi / Geoffrey Tien 5

Queue implementation Using an array typedef struct { int front; int num; int capacity; int* arr; Queue; void initqueue(queue* q) { q->front = 0; q->num = 0; q->capacity = 6; // or some other value q->arr = (int*) malloc(q->capacity * sizeof(int)); int isempty(queue* q) { if (q->num == 0) return TRUE; else return FALSE; int isfull(queue* q) { if (q->num == q->capacity) return TRUE; else return FALSE; October 20, 2017 Hassan Khosravi / Geoffrey Tien 6

Queue implementation Using an array int enqueue(queue* q, int val) { if (isfull(q)) return FALSE; else { q->arr[(q->front + q->num) % q->capacity] = val; q->num++; return TRUE; int dequeue(queue* q) { if (isempty(q)) return FALSE; else { q->arr[q->front] = -1; q->front = (q->front + 1) % q->capacity; q->num--; return TRUE; October 20, 2017 Hassan Khosravi / Geoffrey Tien 7

Array queue example Queue myq; 0 01 initqueue(&myq); front num enqueue(&myq, 6); 6 0 1 2 3 4 5 Insert item at (front + num) % capacity, then increment num October 20, 2017 Hassan Khosravi / Geoffrey Tien 8

Array queue example Queue myq; initqueue(&myq); enqueue(&myq, 6); enqueue(&myq, 4); enqueue(&myq, 7); enqueue(&myq, 3); enqueue(&myq, 8); dequeue(&myq); dequeue(&myq); 01 2 front 6 num Insert item at (front + num) % capacity, then increment num 54 3 4 7 3 8 0 1 2 3 4 5 Remove item at front, then decrement num and make front = (front + 1) % capacity October 20, 2017 Hassan Khosravi / Geoffrey Tien 9

Array queue example Queue myq; initqueue(&myq); enqueue(&myq, 6); enqueue(&myq, 4); enqueue(&myq, 7); enqueue(&myq, 3); enqueue(&myq, 8); dequeue(&myq); dequeue(&myq); enqueue(&myq, 9); 2 front 5 num Insert item at (front + num) % capacity, then increment num 34 5 7 3 8 0 1 2 3 4 5 Remove item at front, then decrement num and make front = (front + 1) % capacity 9 enqueue(&myq, 5); enqueue is possible as long as the array is not full October 20, 2017 Hassan Khosravi / Geoffrey Tien 10

Array queue resizing Suppose we have an array-based queue with (theoretically) unlimited enqueueing and we have performed some enqueue and dequeue operations Then we perform more enqueues to fill the array How should we resize the array to allow for more enqueue operations? 12 5 76 33 2 41 front???????????? October 20, 2017 Hassan Khosravi / Geoffrey Tien 11

Queue implementation Removing items from the front of the queue is straightforward Items should be inserted at the back of the queue in constant time So we must avoid traversing through the list Use a back pointer! typedef struct { struct Node* front; struct Node* back; int num; Queue; void initqueue(queue* q) { q->front = NULL; q->back = NULL; q->num = 0; Using a linked list struct Node { int data; struct Node* next; ; int isempty(queue* q) { if (q->front == NULL) return TRUE; else return FALSE; October 20, 2017 Hassan Khosravi / Geoffrey Tien 12

Queue implementation int enqueue(queue* q, int val) { struct Node* newnode = (struct Node*) malloc(sizeof(struct Node)); if (q->front == NULL) { // special case: empty list q->front = newnode; q->back = newnode; else { // general case q->back->next = newnode; q->back = newnode; q->num++; return TRUE; int peek(queue* q) { if (isempty(q)) return FALSE; else return q->front->data; int dequeue(queue* q) { struct Node* temp = front; if (isempty(q)) return FALSE; else { q->front = q->front->next; free(temp); temp = NULL; q->num--; if (isempty(q)) q->back = NULL; return TRUE; October 20, 2017 Hassan Khosravi / Geoffrey Tien 13

List queue example Queue myq; initqueue(&myq); enqueue(&myq, 6); front 6 enqueue(&myq, 4); enqueue(&myq, 7); enqueue(&myq, 3); dequeue(&myq); back 4 7 3 October 20, 2017 Hassan Khosravi / Geoffrey Tien 14

Exercise For the array-based queue, write the enqueue function to support array resizing! See the array resizing stack example for reference, but be aware of the issues mentioned in slide #11 October 20, 2017 Hassan Khosravi / Geoffrey Tien 15

Readings for this lesson Thareja Chapter 8.1 8.3, 8.5 (Queues) Next class Thareja Chapter 7.7.4 (Recursion) October 20, 2017 Hassan Khosravi / Geoffrey Tien 16