Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic



Similar documents
STACKS,QUEUES, AND LINKED LISTS

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

DATA STRUCTURE - QUEUE

Data Structures and Algorithms Stacks and Queues

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Programming with Data Structures

Analysis of a Search Algorithm

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

ADTs,, Arrays, Linked Lists

Linked Lists Linked Lists, Queues, and Stacks

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Chapter 3: Restricted Structures Page 1

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

CmpSci 187: Programming with Data Structures Spring 2015

Universidad Carlos III de Madrid

Common Data Structures

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Sequential Data Structures

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Module 2 Stacks and Queues: Abstract Data Types

10CS35: Data Structures Using C

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Data Structure [Question Bank]

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

St S a t ck a ck nd Qu Q eue 1

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

PES Institute of Technology-BSC QUESTION BANK

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Stacks. Linear data structures

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Algorithms and Data Structures

Basic Data Structures and Algorithms

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

DATA STRUCTURES USING C

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Data Structures and Data Manipulation

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Data Structures and Algorithms Lists

Data Structures and Algorithms

Introduction to Data Structures and Algorithms

Algorithms and Data Structures Written Exam Proposed SOLUTION

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

14 Stacks, Queues, And Linked Lists

Application of Stacks: Postfix Expressions Calculator (cont d.)

Unordered Linked Lists

DATA STRUCTURE - STACK

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Binary Search Trees (BST)

Introduction to Stacks

Cpt S 223. School of EECS, WSU

Lecture 12 Doubly Linked Lists (with Recursion)

Ordered Lists and Binary Trees

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structures UNIT III. Model Question Answer

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

Sample Questions Csci 1112 A. Bellaachia

COMPUTER SCIENCE. Paper 1 (THEORY)

Data Structures and Algorithms

Data Structures in the Java API

Binary Heap Algorithms

recursion, O(n), linked lists 6/14

Stacks. Data Structures and Data Types. Collections

Abstract Data Types. Chapter 2

Data Structures and Algorithms

Algorithms and Data Structures

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Chapter 14 The Binary Search Tree

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

The ADT Binary Search Tree

CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions. Linda Shapiro Spring 2016

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

7.1 Our Current Model

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Lecture Notes on Binary Search Trees

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

Data Structures. Level 6 C Module Descriptor

Project 4 DB A Simple database program

Transcription:

Queues Outline and Required Reading: Queues ( 4. except 4..4) COSC, Fall 3, Section A Instructor: N. Vlajic

Queue ADT Queue linear data structure organized according to first-in/first-out (FIFO) principle! queue items are removed in exactly the same order as they were added to the queue! item access and deletion is restricted to the first element (front), while item insertion is restricted to the last place in the queue (rear)! queue is open at its ends - items cannot be added in the middle A B C flow through pipe Examples print server, scheduler in an OS, scheduler in automated on-line systems, packet forwarding in networks

Queue ADT: Interface 3 Fundamental Methods public void enqueue(object element); (Transformers) /* insert an element at the rear of the queue */ dequeue (delete) public Object dequeue(); /* remove the element at the front of the queue */ /* QueueEmptyException if stack is empty */ enqueue (insert) Supporting Methods public int size(); (Observers) /* return the # of objects in the queue */ public boolean isempty(); /* return true if the queue is empty */ public Object front(); /* get the front element without removing it */ /* QueueEmptyException if stack is empty */

Queue ADT: Interface 4 Possible Queue ADT Errors () underflow - trying to dequeue() or front() on an empty queue can occur regardless of the underlying data structure employed () overflow - trying to enqueue() to an already full queue can occur only if the underlying data structure has a fixed capacity

Queue ADT: Array-Based Implementation 5 Trivial Array-Based Implementation let Q[] be the front of the queue (index of the queue front =, at all times) Drawback too costly! on every dequeue, all remaining elements must be moved forward (Θ(n) run time) dequeue front rear move front rear front rear Q[ ] Q[ ] Q[ ] Explain why, in this case, dequeue() has Θ(n), not O(n), run time!

Queue ADT: Array-Based Implementation (cont.) 6 Wrap Around when the rear index reaches the end of array Implementation start using available locations at the front f index to the cell of Q storing the front r index to the next available cell of Q (rear +) rules for incrementing r and f in circular array: enqueue: r = (r + ) mod N dequeue: f = (f + ) mod N enqueue enqueue r 5 front 3 4 f front 3 4 f 4 rear 5 rear 5 3 enqueue?! r?! circular array

Queue ADT: Array-Based Implementation (cont.) 7 Wrap Around () r < f - how to determine Q size?? Problems () f = r - is Q empty or full?? f f r r 3 r 3 f 3 4 4 4 5 5 5 size = r - f size =? full, empty? Wrap Around () max allowed queue size: (N-) Solutions () size: size = (r + N - f) mod N if r > f, works fine, i.e. N has no effect if r < f implies that, in fact, there is an N in between, hence (r+n)

Queue ADT: Array-Based Implementation (cont.) 8 public class ArrayQueue implements Queue { public static final int CAPACITY = ; private Object Q[ ]; private int f,r; public ArrayQueue() { this(capacity); } public ArrayQueue(int cap) { Q = new Object[capacity]; f=; r=; } pubic int size() { if (r>f) return (r-f); else return (r+q.length-f); } public boolean isempty() { return (r == f); } cont.

Queue ADT: Array-Based Implementation (cont.) 9 pubic void enqueue(object obj) throws QueueFullException { if (size() == q.length-) throw new QueueFulException( Queue overflow. ); Q[r] = obj; r = (r+)%q.length; } Avoided in LL implementation. public Object front() throws QueueEmptyExcpetion { if (isempty()) throw new QueueEmptyException( Queue is empty. ); return Q[f]; } public Object dequeue() throws QueueEmptyExcpetion { Object elem; if (isempty()) throw new QueueEmptyException( Queue is empty. ); elem = Q[f]; Q[f] = null; f = (f+)%q.length return elem; } }

Queue ADT: Performance of Array-Based Implement. Run Time Good Good! all methods run in constant time (no loops or recursions) Method size isempty front enqueue dequeue Time Space Usage Poor Poor! O(N), where N array size, n current # of elements in the queue, N>n General Note problems arise if attempting to enqueue N objects

Queue ADT: Singly Linked List - Based Implementation Instance Variables private Node head; /* reference to the head of the SLL */ private Node tail; /* reference to the tail of the SLL */ see Q. private int size; /* current number of elements in the queue */ Choose: front of Q head of SLL, rear of Q tail of SLL. Why?! dequeue front of Q = head of SLL rear of Q = tail of SLL enqueue

Queue ADT: SLL - Based Implementation (cont.) public void enqueue(object obj) { Node node = new Node(); node.setelement(obj); node.setnext(null); if (size == ) head = node; else tail.setnext(node); tail = node; size ++; } head tail enqueue cont.

Queue ADT: Linked List - Based Implement. (cont.) 3 pubic Object dequeue() throws QueueEmptyException { Object obj; if (size == ) throw new QueueEmptyException( Queue is empty. ); obj = head.getelement(); head = head.getnext(); size --; if (size == ) dequeue tail = null; head return obj; } tail

Queue ADT: Performance of LL - Based Implement. 4 Run Time Good! all methods run in constant time Method size isempty front enqueue dequeue Time Space Usage Good! O(n), n current # of elements in the stack General Note no problems with size/overflow

Queue ADT: Array vs. Linked List Implementation 5 Properties of Queues ordered access additions/removals at a cursor frequent resizing Array Implementation + - - Linked List Implementation + + + implementation complexity / cost + - In Linked List implementation: ) each element must contains reference next ) for every en/de-queued element, method setnext()/getnext() must be called

Queue ADT: Questions 6 Q. Assume we want to implement Queue using a SLL, which has only one reference (e.g.) to the head of SLL. What is the main disadvantage of this implementation? What would be the time complexity of the main queue operations, in this case? Q. (textbook C-4.) Describe how to implement the queue ADT using two stacks. What is the running time of the enqueue() and dequeue() methods? Q.3 (textbook C-4.7) Describe in pseudo-code a linear-time algorithm for reversing a queue Q.