Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Queues and Linked Lists
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
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.
Ceng-112 Data Structures I Chapter 5 Queues.
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.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
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? A queue is a FIFO “first in, first out” structure.
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
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.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
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.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
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.
Stack ADT Operations Push, Pop, Top, isEmpty Application: Expression Evaluation Arithmetic Expression Infix-to-Postfix Postfix to Quadruples Boolean Expressions.
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!
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks and Queues.
Queues Queues Queues.
Queues 11/9/2018 6:32 PM Queues.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Circular queue.
Queues.
Stack ADT Operations Application: Expression Evaluation
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Revised based on textbook author’s notes.
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,
ADT list.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
CSE 373 Data Structures Lecture 6
Data Structures & Programming
Presentation transcript:

Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order Tree Traversal

Implementing a Queue Using Vector /Array –requires estimate of maximum queue length –may grow dynamically –Ø = empty slots –Can contain varied data/objects (not necessarily homogeneous) 28-17Golf #1 ØØØ 212 rules! rearfront

Implementing a Queue Using Linked List –flexible, adjusts to problem size –implementing a linked list nodes and references/links/pointers front 212 rules! Ø rear Golf #1

Implementing a Queue Using Linked List –implementing a linked list cursor implementation front = 5 freelist = Golf #1 212 rules! Ø Ø Ø Ø Ø Ø rear = 1

Implementing a Queue Vector/Array Linked List –enqueueO(1) * O(1) –dequeueO(1)O(1) –peekO(1)O(1) –isEmptyO(1)O(1) *assuming no need for Vector/Array expansion

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 0 Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 0 1 enqueue (“A”) A Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 1 2 enqueue (“B”) A B Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 2 3 enqueue (“C”) A B C Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 1 rear = 3 dequeue() // returns “A” A B C Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 1 2 rear = 3 dequeue() // returns “B” A B C Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 2 3 rear = 3 dequeue() // returns “C” A B C Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { } // isEmpty front = 3 rear = 3 // Is the queue empty? // Complete isEmpty() method. A B C Q

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 3 4 enqueue(“D”); A B C Q D

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 4 5 enqueue(“E”); A B C Q DE

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 5 6 enqueue(“F”); A B C Q F DE

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 6 7 enqueue(“G”); A B C Q F DE G

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 7 0 enqueue(“H”); A B C Q F DE G H

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 0 1 enqueue(“I”); I B C Q F DE G H

enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 1 2 enqueue(“J”); I J C Q F DE G H

front = 3 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { } // isFull // Is the queue full? // Complete isFull() method. I J C Q F DE G H

front = 3 4 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { return front==(rear+1)%Q.length; } // isFull dequeue(); // returns “D” I J C Q F DE G H

front = 4 5 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { return front==(rear+1)%Q.length; } // isFull dequeue(); // returns “E” I J C Q F DE G H

front = 5 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { return front==(rear+1)%Q.length; } // isFull // How many elements does the // queue have? Generalize. I J C Q F DE G H

Level-Order Traversal B D A E IJ K MNL F H C G

B D A E IJ K MNL F H C G Level

B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q = Ø output =

B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q  A output =

B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q  B  C output = A

B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q  C  D  E output = A B

B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q  D  E  F  G output = A B C etc.