Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Stacks, Queues, and Linked Lists
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues.
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.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structure Dr. Mohamed Khafagy.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
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,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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)
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
April 27, 2017 COSC Data Structures I Review & Final Exam
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues Lecture 4. Lecture Objectives  Learn about queues  Examine various queue operations  Learn how to implement a queue as an array  Learn how.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
Stacks and Queues.
Queues Queues Queues.
The Abstract Data Type Queue
Stack and Queue APURBO DATTA.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues.
Circular List removedNode General Case Single Node Case lastNode aNode
CSC 143 Queues [Chapter 7].
Ch. 8 Queue Stack Queue milk queue stack
Stacks and Queues 1.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Ch. 8 Queue Stack Queue milk queue stack
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

Chapter 8 Queue I CS2006 - Data Structures I COSC2006 April 24, 2017 Chapter 7: Queues

Topics Introduction Queue Application Implementation Linked List

COSC2006 April 24, 2017 Queues A queue differs from a stack in that it follows the first-in-first-out (FIFO) principle. Inserting an item to the rear is known as "enqueueing". Removing an item from the front is known as "dequeueing". cashier Front of line Back of line Chapter 7: Queues

Queues Linear, homogeneous structure Has First-In-First-Out (FIFO) behavior; Items added at one end and removed from the other end Middle elements are logically inaccessible Add/ Enqueue Remove/Dequeue Back/Rear Front/Head

Queue Applications Real-World Applications Buy a movie ticket Cashier lines in any store  Computer Science Applications Print lines of a document Convert digit strings to decimal

ADT Queue Specification: Elements Operations Homogeneous Linear Create an empty queue Check if a queue is empty / Full Enqueue (Enq, Enque, Add, Insert): Adding new element at the back (rear) Dequeue (Deq, Deque, Remove, Serve): Deleting an element from the front Retrieve an element from a queue

Queue Applications Converting Digit Strings to Decimal Enter characters from the keyboard and retain them in order Assumption: No typing mistakes Blank spaces may precede or follow the digits Formula used: Initial value DigitSum = 0

Queue Applications Converting Digit Strings to Decimal Pseudocode // Convert digits in aQueue into decimal integer n // Get first digit, ignoring any leading blanks do { ch=queue.dequeue() } while ( ch is blank) // Assertion: ch contains first digit // Compute n from digits in queue n = 0; done = false; do { n = 10 * n + integer that ch represents if (! queue.isEmpty( ) ) else done = true } while (! done and ch is a digit) // Assertion: n is result

Queue Applications Recognizing Palindromes Uses a queue & a stack Idea: Insert the string in both queue & stack Remove characters from both stack's Top & queue's Front, comparing them Repeat until: a) Either the stack or the queue are empty  String is a palindrome b) The character from the stack and the corresponding character from the queue are not similar  String isn't a palindrome

Queue Applications Recognizing Palindromes Pseudocode IsPal(in str:string) : boolean // Determines whether String is a palindrome aQueue.createQueue ( ) // Create an empty queue aStack.createStack ( ) // Create an empty stack // Insert each character of String into both aQueue and aStack length = length of str for ( i = 1 through length) { nextChar = ith character of str aQueue.enqueue ( nextChar ) aStack.push(NextChar) } // end for // Compare aQueue with aStack charactersAreEqual = true; while ( ! aQueue.isEmpty() && charactersAreEqual) { queueFront =aQueue.peek () stackTop= aStack.top () if ( queueFront equals stackTop) { aQueue.dequeue ( ) aStack.pop ( ) } else charactersAreEqual = false } // end while return charactersAreEqual

ADT Queue Implementation Possible implementations: Linked List-based Linear Circular Array-based ADT List-based

Linked List-Based Implementation More straightforward than array-based Possible options: Linear linked list Two external “pointer” (Front & Back) Circular linked list One “pointer” will be enough (Back)

Linked List Queue Implementation Linked List -Based Implementation: Option 1 Insertion to an empty list front = newNode back = newNode

Linked List Queue Implementation LL -Based Implementation: Option 1 Insertion to a non-empty list newNode.setNext ( NULL); Back.setNext (newNode); back = newNode; Deletion temp = front ; front = front.getNext() temp.setNext ( NULL ) 20 15 front 26 84 back

Linked List Queue Implementation LL -Based Implementation: option 1 Deletion form a one-node (one item) queue If (front = back&&front!=null){ back = null front=null } We will use option 2 to implement our queue

Queue Interface public interface QueueInterface { COSC2006 April 24, 2017 Queue Interface public interface QueueInterface { public boolean isEmpty(); public void enqueue(Object newItem) throws QueueException; public Object dequeue() throws QueueException; public void dequeueAll(); public Object peek() throws QueueException; } Chapter 7: Queues

Queue Exception public class QueueException extends RuntimeException { COSC2006 April 24, 2017 Queue Exception public class QueueException extends RuntimeException { public QueueException(String s) { super(s); } // end constructor } // end QueueException Chapter 7: Queues

LListQueue Implementation COSC2006 April 24, 2017 LListQueue Implementation public class LListQueue implements QueueInterface { private Node lastNode; // we use option 2 public LListQueue() { lastNode = null; } // end default constructor // queue operations: public boolean isEmpty() { return lastNode == null; } // end isEmpty public boolean isFull() { return false; } // end isFull public void dequeueAll() { } // end dequeueAll Chapter 7: Queues

LListQueue Implementation COSC2006 April 24, 2017 LListQueue Implementation public class LListQueue implements QueueInterface { private Node lastNode; public LListQueue() { lastNode = null; } // end default constructor // queue operations: public boolean isEmpty() { return lastNode == null; } // end isEmpty public boolean isFull() { return false; } // end isFull public void dequeueAll() { } // end dequeueAll Chapter 7: Queues

LListQueue Implementation (2) COSC2006 April 24, 2017 LListQueue Implementation (2) public void enqueue(Object newItem) { Node newNode = new Node(newItem); if(isFull()) throw new QueueException("QueueException on enqueue:"+ "queue full"); // insert the new node if (isEmpty()) { // insertion into empty queue newNode.setNext(newNode); } else { // insertion into nonempty queue newNode.setNext(lastNode.getNext()); lastNode.setNext(newNode); } // end if lastNode = newNode; // new node is at back } // end enqueue Chapter 7: Queues

LListQueue Implementation (3) COSC2006 April 24, 2017 LListQueue Implementation (3) public Object dequeue() throws QueueException { if (!isEmpty()) { // queue is not empty; remove front Node firstNode = lastNode.getNext(); if (firstNode == lastNode) { // special case? lastNode = null; // yes, one node in queue } else { lastNode.setNext(firstNode.getNext()); } // end if return firstNode.getItem(); throw new QueueException("QueueException on dequeue:" + "queue empty"); } // end dequeue Chapter 7: Queues

LListQueue Implementation (4) COSC2006 April 24, 2017 LListQueue Implementation (4) public Object peek() throws QueueException { if (!isEmpty()) { // queue is not empty; retrieve front Node firstNode = lastNode.getNext(); return firstNode.getItem(); } else { throw new QueueException("QueueException on peek:" + "queue empty"); } // end if } // end peek } // end ListQueue Chapter 7: Queues

LListQueue Implementation (4) COSC2006 April 24, 2017 LListQueue Implementation (4) public Object peek() throws QueueException { if (!isEmpty()) { // queue is not empty; retrieve front Node firstNode = lastNode.getNext(); return firstNode.getItem(); } else { throw new QueueException("QueueException on peek:" + "queue empty"); } // end if } // end peek } // end ListQueue Chapter 7: Queues

LListQueue Test public class LListQueueTest { COSC2006 April 24, 2017 LListQueue Test public class LListQueueTest { public static void main(String[ ] args) { LListQueue aQueue = new LListQueue(); System.out.println("Enqueuing:"); for (int i = 0; i < 9; i++) { System.out.print(" "+i); aQueue.enqueue(new Integer(i)); } // end for System.out.println("\nDequeuing:"); System.out.print(" "+aQueue.dequeue()); System.out.println(); } // end main } // QueueTest Chapter 7: Queues

Review In a queue, items can be added ______. only at the front of the queue only at the back of the queue either at the front or at the back of the queue at any position in the queue

Review Operations on a queue can be carried out at ______. its front only its back only both its front and back any position in the queue

Review Which of the following is NOT an ADT queue operation? enqueue isEmpty Pop peek

Review The ______ operation retrieves and removes the front of a queue. isEmpty enqueue dequeue peek

Review The ______ operation retrieves the item that was added earliest to a queue, but does not remove that item. enqueue dequeue dequeueAll peek

Review A reference-based implementation of a queue that uses a linear linked list would need at least ______ external references. one two three four

Review A reference-based implementation of a queue that uses a circular linked list would need at least ______ external references. one two three four

Review Which of the following operations leaves a queue unchanged? enqueue dequeue dequeueAll peek