Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CS Data Structures II Review COSC 2006 April 14, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks, Queues, and Deques
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
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.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and 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,
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.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Queues Introduction Queue applications Implementations.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Elementary Data Structures
Review Array Array Elements Accessing array elements
Queues Rem Collier Room A1.02
Stacks.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Stacks, Queues, and Deques
Presentation transcript:

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony Gaddis and Godfrey Muganda Chapter 21 : Stacks and Queues

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2 Chapter Topics Stacks and Their Applications Array Implementation of Stacks Linked Implementation of Stacks Queues and Their Applications Array Implementation of Queues Linked Implementation of Queues

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Stacks A stack is a collection of items that allows the following operations: –boolean empty() checks if the stack is empty. –void push(E o) adds an object o to the stack. –E pop() removes and returns the item most recently added to the stack.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 Last In First Out A stack is a Last in-First-Out container: the last item added is the first to be removed.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 The Stack peek() Operation Most stacks support a E peek() operation: this returns, but does not remove from the stack, the item most recently added to the stack peek() returns the item that will be removed by the next call to pop()

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6 Applications of Stacks Many collections of items are naturally regarded as stacks: –A stack of plates in a cafeteria. –Cars packed in a narrow driveway. –Return addresses of method calls in an executing program.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7 Plates in a Cafeteria Plates are stacked in a column. A new plate is added to the top of the column of plates. Plates are removed from the top of the column of plates, so that the last plate added is the first removed.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8 Method Return Addresses Programs often make chains of method calls. The last method called is the first to return. The compiler uses a stack to maintain the list of return addresses for executing methods in a stack.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Java Collection Framework Stack Class The JCF provides a generic implementation of a stack: –Stack () (constructor) –E push(E element) –E pop() –E peek() –boolean empty()

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10 The JCF Stack Class The JCF stack can be used to create stacks of String, numeric values, or any other types. Following example demonstrates the use of the JCF Stack class to work with strings.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 The JCF Stack Class i mport java.util.Stack; public class StackDemo { public static void main(String [] args) { // Create a stack of strings and add some names Stack stack = new Stack (); String [ ] names = {"Al", "Bob", "Carol"}; System.out.println("Pushing onto the stack the names:"); System.out.println("Al Bob Carol"); for (String s : names) stack.push(s); // Now pop and print everything on the stack String message = "Popping and printing all stack values:"; System.out.println(message); while (! stack.empty() ) System.out.print( stack.pop() + " “ ); } }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 Stacks of Primitive Values The JCF Stack class does not support stacks of primitive types. To create a stack that can store a primitive type, create a stack of the corresponding wrapper type.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13 Stack of Primitive Types These statements will not compile: Stack myIntStack; Stack myBoolStack; Use stacks of the corresponding wrapper class types: Stack myIntStack; Stack myBoolStack;

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14 Autoboxing Once a stack of a wrapper type has been declared and instantiated, you can use the primitive type in all stack methods. The compiler automatically boxes primitive values passed as parameters to the stack methods to form the required wrapper class types.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15 Unboxing The compiler automatically unboxes wrapper types returned as values by the stack methods to form the corresponding primitive value.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16 Autoboxing and Unboxing public class StackDemo { public static void main(String [] args) { Stack intStack = new Stack (); // Push some numbers onto the stack for (int k =1 ; k < ; k++) intStack.push(k*k); // Pop and print all numbers while (!intStack.empty()) { int x = intStack.pop(); System.out.print( x + " "); } } }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Array Implementation of Stacks

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 Array Implementation of Stacks A stack can be implemented by using an array to hold the items being stored.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 Array Implementation of Stacks A stack can be implemented by using an array to hold the items being stored. push can be implemented so it stores items at the end of the array.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20 Array Implementation of Stacks A stack can be implemented by using an array to hold the items being stored. push can be implemented so it stores items at the end of the array. pop can be implemented so it returns the item at the end of the array.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21 Array Implementation of Stacks A stack can be implemented by using an array to hold the items being stored. push can be implemented so it stores items at the end of the array. pop can be implemented so it returns the item at the end of the array. A variable top can be used to track the actual end of the array, where the next item will be added.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 22 Pushing Items Onto a Stack

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23 Array Implementation of a Stack Use a class ArrayStack with private fields: int [ ] s; int top; // points to actual end of the stack Constructor: ArrayStack(int capacity) { s = new int[capacity]; top = 0; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24 Array Implementation of a Stack The method for adding an item to the stack: void push(int x) { if (top == s.length) throw new IllegalStateException(); s[top] = x; top ++; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25 Array Implementation of a Stack The method to retrieve an item from the stack: int pop() { if (top == 0) then throw new IllegalStateException(); top --; return s[top]; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26 Stack Overflow and Underflow The exception thrown when an attempt is made to push a value onto a stack whose array if full is called a stack overflow exception.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27 Stack Overflow and Underflow The exception thrown when an attempt is made to push a value onto a stack whose array if full is called a stack overflow exception. The exception thrown when an attempt is made to pop an empty stack is sometimes called stack underflow.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 28 Checking for an Empty Stack You check for an empty stack by looking to see if top is 0 : boolean empty() { return top == 0; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 29 Peek() To return the item currently at the “top” of the stack: int peek() { if (top ==0 ) throw new IllegalStateException(); return s[top-1]; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 30 Array-Based Stacks of Other Types Implementing stacks of other types is similar to implementing a stack of int. Main difference: when a value of a reference type is removed from the stack, the array entry must be set to null. s[top-1] = null; Forgetting to set the array entry to null will keep the removed item from being garbage collected.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 31 Popping a Reference Type public String pop() { if (empty()) throw new EmptyStackException(); else { int retVal = s[top-1]; s[top-1] = null; // Facilitate garbage collection top--; return retVal; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 32 Linked Implementation of Stacks A linked list can be used to implement a stack by using the head of the list as the “top” of the stack:

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33 Linked Implementation of Stacks A linked list can be used to implement a stack by using the head of the list as the “top” of the stack: push(E o) adds a new item as the new head of the linked list.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 34 Linked Implementation of Stacks A linked list can be used to implement a stack by using the head of the list as the “top” of the stack: push(E o) adds a new item as the new head of the linked list. E pop() removes and returns the head of the linked list.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35 Implementing a Stack with a Linked List A reference Node top; // list head is used to represent the linked list that holds the stack items.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 36 Linked Implementation of a Stack class LinkedStack { // Node class used for linked list private class Node { String element; Node next; Node (String e, Node n) { element = e; next = n; } private Node top = null; // head of list is top of stack }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37 Linked Implementation of a Stack A linked stack after pushing two items:

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 38 Linked Implementation of Stacks The linked stack of the previous slide after pushing the string Carol.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 39 Linked Implementation of Stack Methods All of the stack methods have straightforward implementations using linked lists: –boolean empty() –String pop() –void push(String e) –String peek()

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 40 Checking for an Empty Stack The stack is empty if the head pointer top is null: boolean empty() { return top == null; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 41 The Stack push Operation Adds a new item at the beginning of the underlying linked list: void push(String s) { top = new Node(s, top); }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 42 The Stack pop Operation Removes and returns the element at the head of the stack’s linked list: public String pop() { if (empty()) throw new IllegalStateException(); else { String retValue = top.element; top = top.next; return retValue; } }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 43 The Stack peek Operation Return without removing the element stored in the head of the list. public String peek() { if (empty()) throw new IllegalStateException(); else return top.element; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44 Queues

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45 Queues A Queue is a collection that allows elements to be added and removed in a First-In-First-Out order. Elements are removed from the queue in the same order in which they are added.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 46 Queue Applications Many applications have clients that arrive needing service, and depart after being served. Clients who arrive are added to a queue. Clients are removed from the queue, receive service, and leave.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 47 Queue Applications A print server may service many workstations in a computer network. The print server uses a queue to hold print job requests waiting to be sent to the printer. A simulation of cars going through a toll booth would use a queue to hold cars lining up to pay toll.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 48 Queue Operations enqueue(E x) : adds an item to the queue.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 49 Queue Operations enqueue(E x) : adds an item to the queue. E dequeue() : removes and returns an item from the queue.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 50 Queue Operations enqueue(E x) : adds an item to the queue. E dequeue() : removes and returns an item from the queue. E peek() : returns, but does not remove, the item that will be returned by the next call to dequeue().

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 51 Queue Operations enqueue(E x) : adds an item to the queue E dequeue() : remove and return an item from the queue E peek() : return, but do not remove, the item that will be returned by the next call to dequeue() boolean empty() : check to see if the queue is empty

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 52 Implementation of Queues An array-based implementation uses an array to hold queue elements.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 53 Implementation of Queues An array-based implementation uses an array to hold queue elements. A linked list implementation uses a linked list to hold queue elements.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 54 Conceptual View of an Array A queue can be viewed as a linear sequence of items where –new items are added at one end (the rear of the queue) –new items are removed from the other end (the front of the queue)

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 55 Array Implementation of a Queue Requires an array to hold the queue elements: String [ ] q; // Queue will hold strings

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 56 Array Implementation of a Queue Requires an array to hold the queue elements: String [ ] q; // Queue will hold strings Requires an index to keep track of the array slot that will hold the next item to be added to the queue: int rear; // Where next item will be added

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 57 Array Implementation of a Queue Requires an array to hold the queue elements: String [ ] q; // Queue will hold strings Requires an index to keep track of the array slot that will hold the next item to be added to the queue: int rear; // Where next item will be added Requires an index to keep track of the array slot the holds the next item to be removed from the queue: int front; // Next item to be removed

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 58 An Invariant for the Array Implementation front: index of next item to be dequeued. This slot is normally filled, but is unfilled when the queue is empty. rear: index of slot that will receive the next item to be enqueued. This slot is normally unfilled, but is filled when the array is completely filled.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 59 Simplistic Implementation of enqueue Initially rear points to the first available unfilled position in the array. A new item x is added to the queue at rear as follows: q[rear] = x; rear ++; As a new item is added, rear is incremented to point to the next unfilled position in the array.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 60 Simplistic Implementation of dequeue The item is removed from the queue at the index front. The item at front is saved so it can be returned, and front is incremented to point to the next item in the queue: String element = q[front]; q[front] = null; front ++;

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 61 Use of the Array as a Circular Buffer The simplistic enqueue fills array slots in order of increasing index The simplistic dequeue empties array slots behind enqueue, also in order of increasing index. By the time enqueue gets to the end of the array, dequeue may have emptied out slots at the beginning, so enqueue should wrap around to the beginning of the array when it gets to the end.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 62 Use of an Array-Based Queue

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 63 Using an Array as a Circular Buffer

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 64 Tracking the Size of the Queue Tracking the number of elements in the queue is useful in determining if the queue is empty, and when the queue is full. An integer variable is used to track the size: int size = 0; The variable size is incremented by enqueue, and decremented by dequeue.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 65 Implementation of enqueue The correct implementation of enqueue must –Increment the size variable. –Add the new item at rear. –Increment rear and wrap around to 0 when rear reaches the end of the array.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 66 The Array-Based Enqueue Method public void enqueue(String s) { if (size == q.length) throw new IllegalStateException(); else { // Add to rear size ++; q[rear] = s; rear ++; // If at end of array, wrap around if (rear == q.length) rear = 0; } }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 67 Implementation of dequeue The correct implementation of dequeue must –Decrement the size variable. –Remove an item at front. –Increment front and wrap around to 0 when front reaches the end of the array.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 68 Implementation of dequeue public String dequeue() { if (empty()) throw new IllegalStateException(); else { size --; // Remove from front String value = q[front]; // Facilitate garbage collection q[front] = null; // Update front front++; if (front == q.length) front = 0 ; return value; } }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 69 Linked Implementation of Queues

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 70 Linked Implementation of Queues A linked list can be used to implement a queue. The head of the linked list is used as the front of the queue. The end of the linked list is used as the rear of the queue.

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 71 Linked Implementation of a Queue class LinkedQueue { private class Node { String element; Node next; Node(String e, Node n) { element = e; next = n; } Node front = null; // head of list, where items are removed Node rear = null; // last node in list, where items are added }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 72 Checking if Queue is Empty This is done by looking to see if there is a node at front, that is, if front is null: boolean empty() { return front = null; }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 73 Linked Implementation of Enqueue public void enqueue(String s) { if (rear != null) { rear.next = new Node(s, null); rear = rear.next; } else { rear = new Node(s, null); front = rear; } }

Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley 74 Linked Implementation of dequeue public String dequeue() { if (empty()) throw new IllegalStateException(); else { String value = front.element; front = front.next; if (front == null) rear = null; return value; } }