Announcements Queues. Queues

Similar documents
A queue is a linear collection whose elements are added on one end and removed from the other

LECTURE OBJECTIVES 6-2

Queue rear tail front head FIFO

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

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

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

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

ECE Fall st Midterm Examination (120 Minutes, closed book)

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

Priority Queue. How to implement a Priority Queue? queue. priority queue

ITI Introduction to Computing II

Lists. ordered lists unordered lists indexed lists 9-3

CE204 Data Structures and Algorithms Part 2

Basic Data Structures

CS302 - Data Structures using C++

Basic Data Structures 1 / 24

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

CSC 273 Data Structures

Stacks and Queues III

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

CSC 1052 Algorithms & Data Structures II: Linked Queues

Stack and Queue. Stack:

More Data Structures (Part 1) Stacks

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

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Queues Fall 2018 Margaret Reid-Miller

LIFO : Last In First Out

CSE 143 SAMPLE MIDTERM

CSC 1052 Algorithms & Data Structures II: Queues

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Stacks and Queues as Collections

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

.:: UNIT 4 ::. STACK AND QUEUE

CS350: Data Structures Stacks

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

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

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

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

CS24 Week 4 Lecture 2

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

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

csci 210: Data Structures Stacks and Queues

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

1. Introduction. Lecture Content

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

Queue: Queue Representation: Basic Operations:

CSEN 301 Data Structures and Algorithms

Data Structures and Object-Oriented Design III. Spring 2014 Carola Wenk

1 P age DS & OOPS / UNIT II

Chapter 18: Stacks And Queues

Lecture Data Structure Stack


Ashish Gupta, Data JUET, Guna

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

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Stacks and Queues

Interfaces & Generics

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Data Abstraction and Specification of ADTs

Chapter 18: Stacks And Queues

THE UNIVERSITY OF WESTERN AUSTRALIA

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

No Aids Allowed. Do not turn this page until you have received the signal to start.

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

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Searching and Sorting Chapter 18. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Abstract Data Type: Stack

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

UNIT-2 Stack & Queue

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

Abstract Data Types. Abstract Data Types

CS 206 Introduction to Computer Science II

CSCD 326 Data Structures I Queues

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

! Instan:a:ng a Group of Product objects. ! InstanEaEng a Group of Friend objects. ! CollecEons can be separated into two categories

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

Introduction to Computing II (ITI 1121) Final Examination

Data Structure Advanced

Linked Structures. See Section 3.2 of the text.

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

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

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

Queues. CITS2200 Data Structures and Algorithms. Topic 5

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

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

ECE 242 Data Structures and Algorithms. Linked List I. Lecture 10. Prof.

Stacks and Queues. David Greenstein Monta Vista

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

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

Lists, Stacks, and Queues

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

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

The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

data structures and algorithms lecture 6

Data Structure - Stack and Queue-

Transcription:

Announcements. Queues Queues A queue is consistent with the general concept of a waiting line to buy movie tickets a request to print a document crawling the web to retrieve documents A queue is a linear collection whose elements are added on one end and removed from the other Adding an element Removing an element rear of queue front of queue Queue elements are processed in a first in, first out (FIFO) manner Elements are removed from the queue in the same order in which they are placed on the queue - -

javafoundations.queue Radix Sort public interface Queue<T> { //Adds element to rear of the queue A radix sort is unusual because it does not involve comparisons between keys! public void enqueue (T element); //Removes and returns element at front of queue public T dequeue(); //Return reference to first element without removing public T first(); //Returns true if queue contains no elements The technique used in the radix sort is based on the structure of the sort key Separate queues are created for each possible value of each digit or character of the sort key Radix = The number of queues, or the number of possible values public boolean isempty(); //Returns number of elements public int size(); //Returns string representation if we were sorting strings made up of lowercase alphabetic characters, the radix would be, one for each possible character if we were sorting decimal numbers, then the radix would be, one for each digit to 9 The radix sort makes a pass through the values for each position in the sort key public String tostring(); - Radix Sort (st pass) Digit s position Radix Sort (nd pass begins) Digit s position Original list Original list - -

Radix Sort (nd pass results) Digit s position Radix Sort (rd pass results) Digit s position for (int digitval = ; digitval <= 9; digitval++)//create q digitqueues[digitval] = new ArrayQueue<Integer>(); // sort the list that contains the numbers for (int position=; position <= ; position++){// -digit nums for (int scan = ; scan < list.length; scan++) { temp = String.valueOf (list[scan]); digit = Character.digit (temp.charat(-position), ); digitqueues[digit].enqueue (list[scan]); // gather numbers from the queues back into list num = ; for (int digitval = ; digitval <= 9; digitval++) { while (!(digitqueues[digitval].isempty())) { list[num] = digitqueues[digitval].dequeue().intvalue(); num++; - -9 RadixSort.java Implementing Queues with Arrays Adds a new element to the rear of the queue, which is stored at the high end of the array Left-shift toa Dequeue correct queue A B C D E Enqueue E - -

javafoundations.arrayqueue public T dequeue() throws EmptyCollectionException { if ( == )throw new EmptyCollectionException ( Dequeue failed. Queue empty ); T result = queue[]; --; import javafoundations.exceptions.*; //left shift the elements to keep the front at element for(int index = ; index < ; index++) public class ArrayQueue<T> implements Queue<T> { queue[index] = queue[index+]; private final int DEFAULT_CAPACITY = ; queue[] = null; private int ; return result; private T[] queue; //-args constructor: Creates empty queue //Left as programming projects: public ArrayQueue(){ //public T first() throws EmtpyCollectionException{ //public void enqueue (T element){ = ; queue = (T[]) (new Object[DEFAULT_CAPACITY]); //public boolean isempty(){ //public int size() { //public String tostring() {.9 Implementing Queues with Circular Arrays Implementing Queues with Circular Arrays n- As elements are dequeued, the front of the queue will move further into the array n The challenge comes when the rear of the queue reaches the end of the array front When this occurs, it rear wraps around to the front of the array Use two variables, front and rear, to represent the location where the first element is stored, and where the next available slot in the array is located (respectively) - A As elements are enqueued, the rear of the queue will also move further into the array B C D -

javafoundations.circulararrayqueue.9 The Changing State of a Circular Array Q // CircularArrayQueue.java Java Foundations // // Represents an array implementation of a queue in which neither // end of the queue is fixed in the array. The variables storing the // indexes of the front and rear of the queue continually increment // as elements are removed and added, cycling back to when they // reach the end of the array. 9 D A B E C A D B E C D A E B C front rear import javafoundations.exceptions.*; public class CircularArrayQueue<T> implements Queue<T> { private final int DEFAULT_CAPACITY = ; private int front, rear, ; private T[] queue; (more ) - - javafoundations.circulararrayqueue javafoundations.circulararrayqueue //----------------------------------------------------------------// Creates a new array to store the contents of this queue with // twice the capacity of the old one. //----------------------------------------------------------------public void expandcapacity() { T[] larger = (T[])(new Object[queue.length*]); //----------------------------------------------------------------// Creates an empty queue using the default capacity. //----------------------------------------------------------------public CircularArrayQueue() { front = rear = = ; queue = (T[]) (new Object[DEFAULT_CAPACITY]); for (int index=; index < ; index++) larger[index] = queue[(front+index) % queue.length]; //----------------------------------------------------------------// Adds the specified element to the rear of this queue, expanding // the capacity of the queue array if necessary. //----------------------------------------------------------------public void enqueue (T element) { if ( == queue.length) expandcapacity(); front = ; rear = ; queue = larger; //----------------------------------------------------------------// The following methods are left as Programming Projects. //----------------------------------------------------------------// public T dequeue () throws EmptyCollectionException { // public T first () throws EmptyCollectionException { // public int size() { // public boolean isempty() { // public String tostring() { - queue[rear] = element; rear = (rear+) % queue.length; ++; (more ) - 9

. Implementing Queues with Links rear front javafoundations.linkedqueue // LinkedQueue.java Java Foundations // // Represents a linked implementation of a queue. import javafoundations.exceptions.*; A B C D - javafoundations.linkedqueue //----------------------------------------------------------------// Adds the specified element to the rear of this queue. //----------------------------------------------------------------public void enqueue (T element) { LinearNode<T> node = new LinearNode<T>(element); //----------------------------------------------------------------// Creates an empty queue. //----------------------------------------------------------------public LinkedQueue() { = ; front = rear = null; (more ) - Analysis of Stack and Queue Implementations Both stacks and queues can be implemented very efficiently In almost all cases, the operations are not affected by the number of elements in the collection if ( == ) front = node; else rear.setnext(node); All operations for a stack (push, pop, peek, etc.) are O() rear = node; ++; Almost all operations for a queue are O() //----------------------------------------------------------------// The following methods are left as Programming Projects. //----------------------------------------------------------------// public T dequeue () throws EmptyCollectionException { // public T first () throws EmptyCollectionException { // public boolean isempty() { // public int size() { // public String tostring() { - public class LinkedQueue<T> implements Queue<T> { private int ; private LinearNode<T> front, rear; The only exception is the dequeue operation for the ArrayQueue implementation the shifting of elements makes it O(n) The dequeue operation for the CircularArrayQueue is O() because of the ability to eliminate the shifting of elements -