Chapter 5 The Queue ADT. Chapter 5: The Queue ADT 5.1 – Queues 5.2 – Formal Specification 5.3 – Array-Based Implementations 5.4 – Application: Palindromes.

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

Stack & Queues COP 3502.
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
CHAPTER 7 Queues.
Fundamentals of Python: From First Programs Through Data Structures
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Topic 3 The Stack ADT.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass.
Stacks And Queues Chapter 18.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
Advanced Higher Computing Science Stacks Queues and linked lists.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 7 Queues Introduction Queue applications Implementations.
Chapter 3 The Stack ADT. Chapter 3: The Stack ADT 3.1 – Stacks 3.2 – Collection Elements 3.3 – Exceptional Situations 3.4 – Formal Specification 3.5 –
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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 CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
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)
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
The Queue ADT.
Section 3.7 Linked-Based Implementations
Chapter 3 The Stack ADT.
Sections 3.4 Formal Specification
Review Array Array Elements Accessing array elements
Queues Rem Collier Room A1.02
Stacks and Queues.
Queues Queues Queues.
Section 5.7 Concurrency, Interference, and Synchronization.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 4 The Queue ADT.
Data Structures & Programming
Presentation transcript:

Chapter 5 The Queue ADT

Chapter 5: The Queue ADT 5.1 – Queues 5.2 – Formal Specification 5.3 – Array-Based Implementations 5.4 – Application: Palindromes 5.5 – Application: The Card Game of War 5.6 – Link-Based Implementations 5.7 – Case Study: Average Waiting Time

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) structure

Operations on Queues Constructor –new - creates an empty queue Transformers –enqueue - adds an element to the rear of a queue –dequeue - removes and returns the front element of the queue

Effects of Queue Operations

Using Queues Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur. Computer systems must often provide a “holding area” for messages between two processes, two programs, or even two systems. This holding area is usually called a “buffer” and is often implemented as a queue. Our software queues have counterparts in real world queues. We wait in queues to buy pizza, to enter movie theaters, to drive on a turnpike, and to ride on a roller coaster. Another important application of the queue data structure is to help us simulate and analyze such real world queues

5.2 Formal Specification (of our Queue ADT) Recall from Section 5.1 that a queue is a “first-in first-out" structure, with primary operations –enqueue - adds an element to the rear of a queue –dequeue - removes and returns the front element of the queue push - adds an element to the top of the stack In addition we need a constructor that creates an empty queue Our Queue ADT will be generic, allowing the class of objects held by the queue to be specified upon instantiation of the queue.

Completing the Formal Specification To complete the formal specification of the Queue ADT we need to –Identify and address any exceptional situations –Determine boundedness –Define the Queue interface or interfaces We use the same approach we used for Stacks

Exceptional Situations dequeue – what if the queue is empty? –throw a QueueUnderflowException –plus define an isEmpty method for use by the application enqueue – what if the queue is full? –throw a QueueOverflowException –plus define an isFull method for use by the application

Boundedness We support two versions of the Queue ADT –a bounded version –an unbounded version We define three interfaces –QueueInterface : features of a queue not affected by boundedness –BoundedQueueInterface : features specific to a bounded queue –UnboundedQueueInterface : features specific to an unbounded queue

QueueInterface // // QueueInterface.java by Dale/Joyce/Weems Chapter 5 // // Interface for a class that implements a queue of Objects. // A queue is a first-in, first-out structure. // package ch05.queues; public interface QueueInterface { T dequeue() throws QueueUnderflowException; // Throws QueueUnderflowException if this queue is empty, // otherwise removes front element from this queue and returns it. boolean isEmpty(); // Returns true if this queue is empty, otherwise returns false. }

The Remaining Queue Interfaces The BoundedQueueInterface package ch05.queues; public interface BoundedQueueInterface extends QueueInterface { void enqueue(T element) throws QueueOverflowException; // Throws QueueOverflowException if this queue is full, // otherwise adds element to the rear of this queue. boolean isFull(); // Returns true if this queue is full, otherwise returns false. } The UnboundedQueueInterface package ch05.queues; public interface UnboundedQueueInterface extends QueueInterface { void enqueue(T element); // Adds element to the rear of this queue. }

Relationships among Queue Interfaces and Exception Classes

5.3 Array-Based Implementations In this section we study two array-based implementations of the Queue ADT –a bounded queue version –an unbounded queue version First we establish how we will hold the queue elements in the array

Fixed Front Design After four calls to enqueue with arguments ‘A’, ‘B’, ‘C’, and ‘D’: Dequeue the front element: Move every element in the queue up one slot The dequeue operation is inefficient, so we do not use this approach

Floating Front Design We use this approach

Wrap Around with Floating Front Design

The ArrayBndQueue Class package ch05.queues; public class ArrayBndQueue implements BoundedQueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] queue; // array that holds queue elements protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear; // index of rear of queue public ArrayBndQueue() { queue = (T[]) new Object[DEFCAP]; rear = DEFCAP - 1; } public ArrayBndQueue(int maxSize) { queue = (T[]) new Object[maxSize]; rear = maxSize - 1; }

The enqueue operation public void enqueue(T element) // Throws QueueOverflowException if this queue is full, // otherwise adds element to the rear of this queue. { if (isFull()) throw new QueueOverflowException("Enqueue attempted on a full queue."); else { rear = (rear + 1) % queue.length; queue[rear] = element; numElements = numElements + 1; }

The dequeue operation public T dequeue() // Throws QueueUnderflowException if this queue is empty, // otherwise removes front element from this queue and returns it. { if (isEmpty()) throw new QueueUnderflowException(“Dequeue attempted on empty queue.”); else { T toReturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numElements = numElements - 1; return toReturn; }

Remaining Queue Operations public boolean isEmpty() // Returns true if this queue is empty, otherwise returns false { return (numElements == 0); } public boolean isFull() // Returns true if this queue is full, otherwise returns false. { return (numElements == queue.length); }

The ArrayUnbndQueue Class The trick is to create a new, larger array, when needed, and copy the queue into the new array –Since enlarging the array is conceptually a separate operation from enqueing, we implement it as a separate enlarge method –This method instantiates an array with a size equal to the current capacity plus the original capacity We can drop the isFull method from the class, since it is not required by the Unbounded Queue Interface The dequeue and isEmpty methods are unchanged

The ArrayUnbndQueue Class package ch05.queues; public class ArrayUnbndQueue implements UnboundedQueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] queue; // array that holds queue elements protected int origCap; // original capacity protected int numElements = 0; // number of elements n the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbndQueue() { queue = (T[]) new Object[DEFCAP]; rear = DEFCAP - 1; origCap = DEFCAP; } public ArrayUnbndQueue(int origCap) { queue = (T[]) new Object[origCap]; rear = origCap - 1; this.origCap = origCap; }

The enlarge operation private void enlarge() // Increments the capacity of the queue by an amount // equal to the original capacity. { // create the larger array T[] larger = (T[]) new Object[queue.length + origCap]; // copy the contents from the smaller array into the larger array int currSmaller = front; for (int currLarger = 0; currLarger < numElements; currLarger++) { larger[currLarger] = queue[currSmaller]; currSmaller = (currSmaller + 1) % queue.length; } // update instance variables queue = larger; front = 0; rear = numElements - 1; }

The enqueue operation public void enqueue(T element) // Adds element to the rear of this queue. { if (numElements == queue.length) enlarge(); rear = (rear + 1) % queue.length; queue[rear] = element; numElements = numElements + 1; }

5.4 Application: Palindromes Examples –A tribute to Teddy Roosevelt, who orchestrated the creation of the Panama Canal: A man, a plan, a canal—Panama! –Allegedly muttered by Napoleon Bonaparte upon his exile to the island of Elba: Able was I ere, I saw Elba. Our goal is to write a program that identifies Palindromic strings –we ignore blanks, punctuation and the case of letters

The Balanced Class To help us identify palindromic strings we create a class called Palindrome, with a single exported static method test test takes a candidate string argument and returns a boolean value indicating whether the string is a palindrome Since test is static we invoke it using the name of the class rather than instantiating an Object of the class The test method uses both the stack and queue data structures

The test method approach The test method creates a stack and a queue It then repeatedly pushes each input letter onto the stack, and also enqueues the letter onto the queue It discards any non-letter characters To simplify comparison later, we push and enqueue only lowercase versions of the characters After the characters of the candidate string have been processed, test repeatedly pops a letter from the stack and dequeues a letter from the queue As long as these letters match each other the entire way through this process, we have a palindrome

Test for Palindrome (String candidate) Create a new stack Create a new queue for each character in candidate if the character is a letter Change the character to lowercase Push the character onto the stack Enqueue the character onto the queue Set stillPalindrome to true while (there are still more characters in the structures && stillPalindrome) Pop character1 from the stack Dequeue character2 from the queue if (character1 != character2) Set stillPalindrome to false return (stillPalindrome)

Code and Demo Instructors can now walk through the code contained in Palindrome.java and PalindromeApp.java, and demonstrate the running program.

Program Architecture

5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players repeatedly battle with their cards –A battle consists of each player placing the top card from their hand, face up, on the table. –Whoever has the higher of the two cards wins the cards –A tied battle means war! In a war –Each player adds three more cards to the prize pile, and then turns up another battle card. –Whoever wins this battle wins the entire prize pile –If that battle is also a tie then there is another war … The card game continues until one of the players runs out of cards, either during a regular battle or during a war. That player loses.

Our War Program Simulates several games of war and tells us, on average, how many battles are waged in each game Models player’s hands and the prize pile as queues Simulates the game by coordinating the dequeing and enqueing operations among the two hands and the prize pile according to the rules Requires two input values - the number of games to simulate and the maximum number of battles allowed before a game is discontinued Output from the program consists of –the number of discontinued games –the number of completed games –the average number of battles waged in completed games

The RankCardDeck Class To help solve our problem we create a class called RankCardDeck, that models a deck of cards Since we are only interested in card “ranks” we do not model suits Three methods are exported –shuffle – randomizes card order –hasMoreCards – returns a boolean –nextCard – returns an int

The WarGame Class To help solve our problem we create another class called WarGame, that simulates a game of War The constructor requires an argument indicating the maximum number of battles to allow before discontinuing the game The class exports two methods –play that simulates a game until it is finished or discontinued, returning true if the game finished normally and false if the game was discontinued –getNumBattles that returns the number of battles waged in the most recent game

The play method Creates three queues –player1’s hand –player2’s hand –the prize pile of cards shuffles the deck of cards and "deals" them repeatedly calls a battle method, which enacts the battle between the two players, until the game is over or discontinued the battle method is recursive – do you see why?

The battle method battle() Get player1's card from player1's hand Put player1's card in the prize pile Get player2's card from player2's hand Put player2's card in the prize pile if (player1's card > player2's card) { remove all the cards from the prize pile and put them in player1's hand } else if (player2's card > player1's card) { remove all the cards from the prize pile and put them in player2's hand } else // war! { each player puts three cards in the prize pile battle() }

Code and Demo Instructors can now walk through the code contained in RankCardDeck.java, WarGame.java and WarGameApp.java, review the programming notes from the chapter, and demonstrate the running program.

Program Architecture

5.6 Linked-Based Implementations In this section we develop a link-based implementations of the Unbounded Queue ADT, and discuss a second link-based approach. For nodes we use the same LLNode class we used for the linked implementation of stacks. After discussing the link-based approaches we compare all of our queue implementation approaches.

The LinkedUnbndQueue Class package ch05.queues; import support.LLNode; public class LinkedUnbndQueue implements UnboundedQueueInterface { protected LLNode front; // reference to the front of this queue protected LLNode rear; // reference to the rear of this queue public LinkedUnbndQueue() { front = null; rear = null; }...

The enqueue operation Enqueue (element) 1. Create a node for the new element 2. Insert the new node at the rear of the queue 3. Update the reference to the rear of the queue

Code for the enqueue method public void enqueue(T element) // Adds element to the rear of this queue. { LLNode newNode = new LLNode (element); if (rear == null) front = newNode; else rear.setLink(newNode); rear = newNode; }

The dequeue operation Dequeue: returns Object 1. Set element to the information in the front node 2. Remove the front node from the queue if the queue is empty Set the rear to null return element

Code for the dequeue method public T dequeue() // Throws QueueUnderflowException if this queue is empty, // otherwise removes front element from this queue and returns it. { if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue."); else { T element; element = front.getInfo(); front = front.getLink(); if (front == null) rear = null; return element; }

An Alternative Approach - A Circular Linked Queue

Comparing Queue Implementations Storage Size –Array-based: takes the same amount of memory, no matter how many array slots are actually used, proportional to current capacity –Link-based: takes space proportional to actual size of the queue (but each element requires more space than with array approach) Operation efficiency –All operations, for each approach, are O(1) –Except for the Constructors: Array-based: O(N) Link-based: O(1) Special Case – For the ArrayUnbndQueue the size “penalty” can be minimized but the enlarge method is O(N)

5.7 Concurrency, Interference, and Synchronization Multitask: Perform more than one task at a time Concurrency: Several interacting code sequences are executing simultaneously –through interleaving of statements by a single processor –through execution on several processors

Counter Class // // Counter.java by Dale/Joyce/Weems Chapter 5 // // Tracks the current value of a counter. // package ch05.threads; public class Counter { private int count; public Counter() { count = 0; } public void increment() { count++; } public String toString() { return "Count is:\t" + count; }

Demo One - Basic import ch05.threads.; public class Demo01 { public static void main(String[] args) { Counter myCounter = new Counter(); myCounter.increment(); System.out.println(myCounter); } The output of the program is: Count is: 3

Demo Two - Threads package ch05.threads; public class Increase implements Runnable { private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; } public void run() { for (int i = 1; i <= amount; i++) c.increment(); } import ch05.threads.*; public class Demo02 { public static void main(String[] args) throws InterruptedException5 { Counter c = new Counter(); Runnable r = new Increase(c, 10000); Thread t = new Thread(r); t.start(); System.out.println("Count is: " + c); } Output Varies: 86, 66, 44 ????

Demo Two - Threads

Demo Three - Join package ch05.threads; public class Increase implements Runnable { private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; } public void run() { for (int i = 1; i <= amount; i++) c.increment(); } import ch05.threads.*; public class Demo02 { public static void main(String[] args) throws InterruptedException5 { Counter c = new Counter(); Runnable r = new Increase(c, 10000); Thread t = new Thread(r); t.start(); t.join(); System.out.println("Count is: " + c); } Output is 10000

Demo Four - Interference package ch05.threads; public class Increase implements Runnable { private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; } public void run() { for (int i = 1; i <= amount; i++) c.increment(); } import ch05.threads.*; public class Demo04 { public static void main(String[] args) throws InterruptedException { Counter c = new Counter(); Runnable r1 = new Increase(c, 5000); Runnable r2 = new Increase(c, 5000); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Count is: " + c); } Output Varies: 9861, 9478 ????

Demo Four - Interference Thread t1 Thread t2 Step 1: obtains value 12 Step 2: obtains value 12 Step 3: increments value to 13 Step 4: stores the value 13 Step 5: increments value to 13 Step 6: stores the value 13

Demo Five - Synchronization // SyncCounter.java // Tracks the current value of a counter. // Provides synchronized access package ch05.threads; public class SyncCounter { private int count; public SyncCounter() { count = 0; } public synchronized void increment() { count++; } public String toString() { return "Count is:\t" + count; } import ch05.threads.*; public class Demo05 { public static void main(String[] args) throws InterruptedException { SyncCounter sc = new SyncCounter(); Runnable r1 = new Increase2(sc, 5000); Runnable r2 = new Increase2(sc, 5000); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Count is: " + sc); } Output is 10000