1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Queue Overview Queue ADT Basic operations of queue
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Review Array Array Elements Accessing array elements
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queue, Deque, and Priority Queue Implementations
Stacks, Queues, and Deques
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
DATA STRUCTURE QUEUE.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Revised based on textbook author’s notes.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Queues FIFO Enqueue Dequeue Peek.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
ADT Queue (Array Implementation)
More Data Structures (Part 1)
Queues Definition of a Queue Examples of Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Queues and Priority Queue Implementations
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

1 Joe Meehean

2

 empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue  peek return (do not remove) item at front of queue 3

 Similar to List ADT with restricted operations implemented using arrays or linked nodes 4

 Fields Node *phead_; int size_; 0 0 size_: phead_: 1 1 //Empty //After q.enqueue(A) A A 5 size_: phead_:

2 2 //After q.enqueue(B) A A B B 2 2 //After q.enqueue(B) B B A A OR  Enqueue to end or insert at the front? 6 size_: phead_: size_: phead_:

EnQ at Front Single TailPtr Double TailPtr enqueue???? dequeue???? peek???? EnQ at Back Single TailPtr Double TailPtr enqueue???? dequeue???? peek???? 7

EnQ at Front Single TailPtr Double TailPtr enqueueO(1) dequeueO(N) O(1) peekO(N)O(1)O(N)O(1) EnQ at Back SingleDouble TailPtr enqueueO(N) O(1) dequeueO(1) peekO(1) 8

 Fields T *items_; int size_; int capacity_; X X //Empty //After q.enqueue(X) capacity_: size_: items_: size_: items_:

2 2 X X Y Y //After q.enqueue(Y) 2 2 Y Y X X //After q.enqueue(Y) OR  Enqueue to end or insert at the front? capacity_: size_: items_: 4 4 capacity_: size_: items_:

Method enQ at front dQ at end enQ at end dQ at front enqueue ?? dequeue ?? peek ?? 11

Method enQ at front dQ at end enQ at end dQ at front enqueue avg: O(1) dequeue O(1) peek O(1) 12

13

 Array implementation of queue 14 Method enQ at front dQ at end enQ at end dQ at front enqueue avg: O(1) dequeue O(1) peek O(1)

 O(1) for enqueue and dequeue  Don’t force 1 st item to index 0  New fields int first_ int last_ 3 3 X X Y Y Z Z A A q.enqueue(X) “”(Y) “”(Z) q.dequeue() q.enqueue(A) 15 size_: items_: first_ last_

void enqueue(const T& newItem){ if(size_ == capacity_) {...} last_++; items_[last_] = newItem; size++; } T dequeue(){ size--; first_++; return items_[first_ - 1]; }  O(1) if array not full (avg case) 16

 What if we call q.enqueue(C)? could expand array? wastes space X X Y Y Z Z A A B B size_: items_: first_ last_

Z A B X X Y Y Z Z A A B B size_: items_: first_ last_ XY first_ last_

Z A B C C Y Y Z Z A A B B size_: items_: first_ last_ CY first_ last_ q.enqueue(C) //enqueued at //pos 0

void enqueue(const T& newItem){ if( size_ == capacity_ ){ doubleCapacity(); } // increment the last index last_++; // wrapping if necessary if(last_ == capacity_){ last_ = 0; } items_[last] = newItem; size_++; } 20

 Expanding the array C C D D A A B B items_: C C D D A A B B 4 4 size_: 4 4 capacity_: items_: size_: capacity_: first_ last_ first_ last_

 Expanding the array C C D D A A B B items_: C C D D A A B B 4 4 size_: 4 4 capacity_: 0123 Big hole in the middle 22 items_: size_: capacity_: first_ last_ first_ last_

A A B B C C D D 5 67 C C D D A A B B first_ last_ first_ last_

 Allocate new array of twice the size: tmp  Copy items_[first] to items_[capacity_- 1] into tmp starting at position first  Copy items_[0] to items_[first - 1] into tmp starting at position capacity_  Fix member data set items_ to point at tmp delete old items last_ = first_ + capacity_ – 1 capacity_ *= 2 24

 Space linked list stores pointer and data array stores only data array can be mostly empty toss up  Time linked all O(1) circular array all O(1) in avg case  Ease of implementation linked by a mile 25

26