73
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 stack as a linked list Discover stack applications Learn about queues Examine various queue operations Learn how to implement a queue as an array Discover queue applications

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

Embed Size (px)

DESCRIPTION

Computer science applications implement method calls –run-time stack (an array in memory storing information about the currently executing program-->successive method calls). As each function returns, the LIFO ordering ensures that control returns to the method that made the most recent call. Stacks are also used to convert recursive algorithms into nonrecursive algorithms typing text on a keyboard and making corrections with the backspace key –(you always erase the last character typed-LIFO!) 3

Citation preview

Page 1: 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

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 stack as a linked list• Discover stack applications • Learn about queues• Examine various queue operations• Learn how to implement a queue as an array• Discover queue applications

Page 2: 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

2

Stacks• Lists of homogeneous elements

– Addition and deletion of elements occur at one end, called the top of the stack

– Middle items are inaccessible

• Stacks are also called Last Input First Output (LIFO) data structures

• Examples:– a box of paper, a stack of coins, dinner trays, parking lot.– Only the top sheet of paper, coin, tray, or car are

accessible at any one time

Page 3: 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

Computer science applications

• implement method calls– run-time stack (an array in memory storing information about

the currently executing program-->successive method calls). As each function returns, the LIFO ordering ensures that control returns to the method that made the most recent call.

• Stacks are also used to convert recursive algorithms into nonrecursive algorithms

• typing text on a keyboard and making corrections with the backspace key – (you always erase the last character typed-LIFO!)

3

Page 4: 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

4

Stacks

Various types of stacks

Page 5: 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

5

Stacks Operations

• Operations performed on stacks– Push: adds an element to the stack– Pop: removes an element from the stack– Peek: looks at the top element of the stack

Page 6: 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

6

Stacks

Stack operations

Page 7: 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

//Interface: StackADT public interface StackADT<T> { public void initializeStack();//initializes stack to an empty state. public boolean isEmptyStack();//determines whether the stack is empty. public boolean isFullStack(); //determines whether the stack is full. public T peek(); //returns the top element of the stack. public void push(T newItem); //adds newItem to the stack. public void pop(); //removes the top element of the stack. }

stacks 7

Page 8: 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

ADT Stack Representations

• Array– one-dimensional array as the representation of a stack – Size limitations

• Linked list– memory is allocated dynamically using reference variables.

• Trade-off in efficiency in terms of speed, storage, and memory usage.

• other factors like flexibility, and ease to understand, code, and maintain

8

Page 9: 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

9

Implementation of Stacks as Arrays

• array of reference variables• Each element of the stack in an array slot

– item at the bottom of the stack is stored in the first array element

– item second to the bottom is stored in the second array element, etc

• top of the stack is the index of the last element added to the stack, stackTop

• Disadvantage - fixed size

Page 10: 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

10

Implementation of Stacks as Arrays (continued)

UML class diagram of the class StackClass

Page 11: 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

11

Implementation of Stacks as Arrays

Example of a stack

Page 12: 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

12

Constructors• Default constructorpublic StackClass(){ maxStackSize = 100; stackTop = 0; //set stackTop to 0 list = (T[]) new Object[maxStackSize]; //create the array}//end default constructor

Page 13: 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

13

Constructors//Constructor with a parameter public StackClass(int stackSize) {

if (stackSize <= 0) { System.err.println("The size must be positive."); System.err.println("Creating an array of the size 100.");

maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = (T[]) new Object[maxStackSize]; //create the array

}

Page 14: 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

14

Initialize Stack• Method initializeStackpublic void initializeStack(){ for (int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0;}//end initializeStack

Page 15: 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

15

Empty Stack• Method isEmptyStackpublic boolean isEmptyStack(){ return (stackTop == 0);}//end isEmptyStack

Page 16: 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

16

Full Stack• Method isFullStackpublic boolean isFullStack(){ return (stackTop == maxStackSize);}//end isFullStack

Page 17: 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

17

Push• Method pushpublic void push(T newItem){ if (isFullStack())

System.err.println("Cannot push in a full stack!"); else

{ list[stackTop] = newItem; //add newItem at the

//top of the stack stackTop++; //increment stackTop }}//end push

Page 18: 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

18

Peek• Method peekpublic T peek(){ if (isEmptyStack()){ System.err.println("No top - the stack is empty!");

return (T) list[0]; }

return (T) list[stackTop - 1];

}//end peek

Page 19: 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

19

Pop• Method poppublic void pop() { if (isEmptyStack())

System.err.println("Cannot pop from an empty stack!"); else

{ stackTop--; list[stackTop] = null;

}}//end pop

Page 20: 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

//Client - test print using a temp stack public class ClientStackArray { public static void main(String[] args) { StackClass<Integer> intStack = new StackClass<Integer>(10); StackClass<Integer> tempStack = new StackClass<Integer>(10); Integer stackItem; intStack.push(37); intStack.push(10); intStack.push(20); intStack.push(27); System.out.println("After push (37, 10, 20, 27) the top is: " + intStack.peek()); System.out.println("The original stack in reverse order is: "); while (!intStack.isEmptyStack()) { stackItem = intStack.peek(); System.out.print(stackItem + " "); tempStack.push(stackItem); intStack.pop(); }

stacks 20

Page 21: 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

System.out.println("\nAfter print, the original stack is empty, temp stack isthe reverse copy of the original."); System.out.println("The original stack in direct order (temp stack in reverse order)is:"); while (!tempStack.isEmptyStack()) { stackItem = tempStack.peek(); System.out.print(stackItem + " "); intStack.push(stackItem); tempStack.pop(); } System.out.println("\nAfter print, the copy stack is empty, the original stack was rebuilt."); System.out.println("Can use the original stack in the remaining program."); } }

OUTPUT: After push (37, 10, 20, 27) the top is: 27 The original stack in reverse order is: 27 20 10 37 After print, the original stack is empty, temp stack is the reverse copy of the original. The original stack in direct order (temp stack in reverse order)is: 37 10 20 27 After print, the copy stack is empty, the original stack was rebuilt. Can use the original stack in the remaining program.

stacks 21

Page 22: 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

22

Linked Implementation of Stacks• Arrays have fixed sizes

– Only a fixed number of elements can be pushed onto the stack

• Dynamically allocate memory using reference variables – Implement a stack dynamically

• Similar to the array representation, stackTop is used to locate the top element– stackTop is now a reference variable

Page 23: 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

23

Linked Implementation of Stacks

Nonempty linked stack

Page 24: 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

24

public class LinkedStackClass<T> implements StackADT<T> { //Node Definition: inner class StackNode private class StackNode<T> { public T info; public StackNode<T> link; public StackNode() {//Default constructor info = null; link = null; } public StackNode(T value, StackNode<T> ptr) {//Alternate constructor info = value; link = ptr; } public String toString() { return info.toString(); } }

Page 25: 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

25

//Instance variable for class LinkedStackClass private StackNode<T> stackTop; //reference variable to top public LinkedStackClass() {//Default constructor

stackTop = null; } public void initializeStack() { stackTop = null; }

Page 26: 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

26

Empty Stack and Full Stack• Methods isEmptyStack and isFullStack

public boolean isEmptyStack(){ return (stackTop == null);}//end isEmptyStack

public boolean isFullStack(){ return false;}//end isFullStack

Page 27: 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

27

Push• Method pushpublic void push(T newValue) {

StackNode<T> newNode; //reference variable to create the new node newNode = new StackNode<T>(newValue, stackTop); stackTop = newNode;

}

Page 28: 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

28

Peek• Method peekpublic T peek() {

if (stackTop == null){ System.err.println("No top - the stack is empty!"); return stackTop.info; } else return stackTop.info;

}

Page 29: 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

29

Pop• Method poppublic void pop() {

if (stackTop == null) System.err.println("Cannot pop from an empty stack!"); else stackTop = stackTop.link;

}

Page 30: 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

//Client - test print with a temp stack public class ClientStackLL { public static void main(String[] args) { LinkedStackClass<Integer> intStack = new LinkedStackClass<Integer>(); LinkedStackClass<Integer> tempStack = new LinkedStackClass<Integer>(); Integer stackItem; intStack.push(37); intStack.push(10); intStack.push(20); intStack.push(27); System.out.println("After push (37, 10, 20, 27) the top is: " + intStack.peek()); System.out.println("The original stack in reverse order is: "); while (!intStack.isEmptyStack()) { stackItem = intStack.peek(); System.out.print(stackItem + " "); tempStack.push(stackItem); intStack.pop(); }

stacks 30

Page 31: 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

System.out.println("\nAfter print, the original stack is empty, temp stack is the reverse copy of the original."); System.out.println("The original stack in direct order (temp stack in reverse order)is:"); while (!tempStack.isEmptyStack()) { stackItem = tempStack.peek(); System.out.print(stackItem + " "); intStack.push(stackItem); tempStack.pop(); } System.out.println("\nAfter print, the copy stack is empty, the original stack was rebuilt."); System.out.println("Can use the original stack in the remaining program."); } }

OUTPUT: After push (37, 10, 20, 27) the top is: 27 The original stack in reverse order is: 27 20 10 37 After print, the original stack is empty, temp stack is the reverse copy of the original. The original stack in direct order (temp stack in reverse order)is: 37 10 20 27 After print, the copy stack is empty, the original stack was rebuilt. Can use the original stack in the remaining program.

31

Page 32: 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

32

Applications of Stacks: Postfix Expression Calculator

• Infix notation– The operator is written between the operands

• Prefix or Polish notation– Operators are written before the operands– Does not require parentheses

• Reverse Polish or postfix notation– Operators follow the operands– Has the advantage that the operators appear in the order

required for computation

Page 33: 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

33

Applications of Stacks: Postfix Expression Calculator

Infix expressions and their equivalent postfix expressions

Page 34: 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

34

Applications of Stacks: Postfix Expression Calculator

• Algorithm to evaluate postfix expressions– Scan the expression from left to right– When an operator is found, back up to get the

required number of operands– Perform the operation– Continue processing the expression

Page 35: 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

• 6 3 +• 9 3 /• 4 5 7 2 + - *

stacks 35

Page 36: 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

36

Nonrecursive Algorithm to Print a Linked List Backward

Naïve approach Get the last node of the list and print it

Traverse the link starting at the first node Repeat this process for every node

Traverse the link starting at the first node until you reach the desired node

Very inefficient

Page 37: 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

37

Print a Linked List Backward

current = first; //Line 1while (current != null) //Line 2{ stack.push(current); //Line 3 current = current.link; //Line 4}

Page 38: 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

while (!stack.isEmptyStack()) //Line 5{ current = stack.peek(); //Line 6 stack.pop(); //Line 7 System.out.print(current + “ “); //Line 8}

20 15 10 5

stacks 38

Page 39: 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

class Stack members• public Stack();• public boolean empty();• public Object peek () throws EmptyStackException• public Object pop () throws EmptyStackException• public Object push(Object obj)• public int search(Object obj)

stacks 39

Page 40: 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

40

Queues Data structure in which the elements are added at

one end, called the rear, and deleted from the other end, called the front

A queue is a First In First Out data structure As in a stack, the middle elements of the queue are

inaccessible

Page 41: 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

Real-life analogies of a queue

– Waiting lines: the first person in line is the first person served (out of line) while new people will join the line at its back (rear).

– Assembly lines: products enter the assembly line from one end and exit from the opposite end in the same order in which they entered.

– A phone call to a 1-800-xxx-xxxx number: your call enters a queue and you keep waiting for the next available agent till you reach the first position in the queue.

stacks 41

Page 42: 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

Computer science applications of a queue

• A network printer and the printer queue: your request to print enters a queue to wait its turn (FIFO order)

• Printing a large paper: the computer sends lines faster than the printer can print them, so the lines are held in a queue (FIFO order)

• Buffered input: – Input characters via the keyboard, each character is stored in a buffer (a

special area in memory that functions as a queue). – the operating system enqueues them until a carriage return character is

received. Next, the OS will examine the characters end remove them from the queue in the same order in which they were typed.

• the operating systems makes extensive use of queues; each I/O device has a queue associated with it in order to be able to satisfy multiple requests.

stacks 42

Page 43: 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

43

Queue Operations isEmptyQueue- Determine whether the queue is currently empty or

not. isFullQueue- Determine whether the queue is currently full or not. front- If queue not empty, return a copy of the front item back- If queue not empty, return a copy of the back item. enqueue(T newItem): Add newItem to the rear dequeue- Removes the item at the front of the queue makeEmpty: Set queue to an empty state.

Page 44: 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

44

QueueException Class Adding an element to a full queue and removing an

element from an empty queue would generate errors or exceptions Queue overflow exception Queue underflow exception

Classes that handle these exceptions QueueException extends RunTimeException QueueOverflowException extends QueueException QueueUnderflowException extends QueueException

Page 45: 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

public class QueueException extends RuntimeException { public QueueException(String msg) { super(msg); } } public class QueueUnderflowException extends QueueException { public QueueUnderflowException() { super("Queue Underflow"); } public QueueUnderflowException(String msg){ super(msg); } } public class QueueOverflowException extends QueueException { public QueueOverflowException() { super("Queue Overflow"); } public QueueOverflowException(String msg){ super(msg); } }

stacks 45

Page 46: 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

ADT Queue Representations:

one-dimensional array as the representation of a queue

static array-based implementation. In particular, the solution uses a ring buffer implementation (a circular array).

As with previous solutions for other ADTs, the major shortcoming of the array implementation is that queues must be limited in size.

An alternative that eliminates this restriction is representing a queue as a linked list.

stacks 46

Page 47: 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

//Interface: QueueADT public interface QueueADT<T> { public void initializeQueue(); //initialize the queue to an empty state. public boolean isEmptyQueue(); //determine whether the queue is empty. public boolean isFullQueue(); //determine whether the queue is full. public T front() throws QueueUnderflowException;//return the first element of the queue. public T back() throws QueueUnderflowException; //return the last element of the queue. public void enqueue(T newItem) throws QueueOverflowException; //Method to add newItem to the queue. public void dequeue() throws QueueUnderflowException; //Method to remove the first element of the queue. }

stacks 47

Page 48: 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

48

Implementation of Queues as Arrays

Instance variables An array to store the queue elements queueFront: keeps track of the first element queueRear: keeps track of the last element maxQueueSize: specifies the maximum size of the

queues

Page 49: 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

enqueue and dequeue • To enqueue an element x we increment rear and set the element with

index rear to x.• To dequeue an element we increment front. • If we keep a counter for the number of elements in the queue, the

enqueue operation will also increment the counter, while the dequeue operation will decrement the counter.

stacks 49

Page 50: 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

enQueue('A');

stacks 50

Page 51: 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

51

enQueue('B');enQueue('C');

Queue after two more enqueue operations

Page 52: 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

52

deQueue();

Queue after the dequeue operation

Page 53: 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

Problem with this implementation:

stacks 53

after a number of enqueue and dequeue operations the queue may appear to be full (since rear may have reached the last valid index in the array) when in reality it may hold just a few elements (because some of them may have been dequeued).

Page 54: 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

54

Implementation of Queues as Arrays

Problems with this implementation Arrays have fixed sizes After various insertion and deletion operations, queueRear will point to the last array position Giving the impression that the queue is full

Solutions Slide all of the queue elements toward the first array

position Use a circular array

Page 55: 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

55

Implementation of Queues as Arrays (continued)

Circular queue

Page 56: 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

56

Implementation of Queues as Arrays (continued)

Queue with two elements at positions 98 and 99

Page 57: 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

57

Implementation of Queues as Arrays (continued)

Queue after one more enqueue operation

Page 58: 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

• Circular representation treats the first and the last elements of the array as though they were adjacent.

• Each time an item is added to the queue (enqueued), the value of rear is incremented by 1 (the index of the next element in the ring) and the item is stored in that position.

• When queueRear is the same as maxQueueSize, the next operation to add an item resets queueRear to 0.

• The statement that causes queueRear to rotate around the ring is (queueRear + 1) % maxQueueSize;

• The index queueFront also advances in the ring; queueFront is given by the expression (queueFront + 1) % maxQueueSize;

stacks 58

Page 59: 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

59

Constructors Default constructor//Default constructorpublic QueueClass(){ maxQueueSize = 100; queueFront = 0; //initialize queueFront queueRear = maxQueueSize - 1; //initialize queueRear count = 0; list = (T[]) new Object[maxQueueSize]; //create the //array to implement the queue}

Page 60: 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

60

initializeQueue Method initilializeQueuepublic void initializeQueue(){ for (int i = queueFront; i < queueRear;i = (i + 1) % maxQueueSize) list[i] = null; queueFront = 0; queueRear = maxQueueSize - 1; count = 0;}

Page 61: 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

61

Empty Queue and Full Queue Methods isEmptyQueue and isFullQueuepublic boolean isEmptyQueue(){ return (count == 0);}

public boolean isFullQueue(){ return (count == maxQueueSize);}

Page 62: 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

62

Front Method frontpublic T front() throws QueueUnderflowException{ if (isEmptyQueue()) throw new QueueUnderflowException(); return (T) list[queueFront];}

Page 63: 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

63

Back Method backpublic T back() throws QueueUnderflowException{ if (isEmptyQueue()) throw new QueueUnderflowException(); return (T) list[queueRear];}

Page 64: 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

64

enqueue Method enqueuepublic void enqueue(T queueElement) throws QueueOverflowException{ if (isFullQueue()) throw new QueueOverflowException(); queueRear = (queueRear + 1) % maxQueueSize; //use the //mod operator to advance queueRear //because the array is circular count++; list[queueRear] = queueElement;}

Page 65: 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

65

dequeue Method dequeuepublic void dequeue() throws QueueUnderflowException{ if (isEmptyQueue()) throw new QueueUnderflowException(); count--; list[queueFront] = null; queueFront = (queueFront + 1) % maxQueueSize; //use the //mod operator to advance queueFront //because the array is circular}

Page 66: 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

66

Linked Implementation of Queues

• Simplifies many of the special cases of the array implementation

• Because the memory to store a queue element is allocated dynamically, the queue is never full

• Class LinkedQueueClass implements a queue as a linked data structure– It uses nodes of type QueueNode

Page 67: 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

//Class: LinkedQueueClass implements //Interface: QueueADT //Linked list implementation public class LinkedQueueClass<T> implements QueueADT<T> { //Node Definition: inner class QueueNode protected class QueueNode<T> { T info; QueueNode<T> link; //Default constructor public QueueNode() { info = null; link = null; } //Alternate constructor public QueueNode(T elem, QueueNode<T> ptr) { info = elem; link = ptr; } public String toString() { return info.toString(); } } //Instance variable for class LinkedQueueClass private QueueNode<T> queueFront; //reference variable to the first queue element private QueueNode<T> queueRear; //reference variable to the last queue element

stacks 67

Page 68: 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

//Default constructor public LinkedQueueClass() { queueFront = null; queueRear = null; }

68

Page 69: 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

69

Linked Implementation of Queues (continued)

• Method initializeQueuepublic void initializeQueue(){ queueFront = null; queueRear = null; }

Page 70: 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

70

Methods isEmptyQueue and isFullQueue

public boolean isEmptyQueue(){ return (queueFront == null);}

public boolean isFullQueue(){ return false;}

Page 71: 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

71

enqueuepublic void enqueue(T newElement){ QueueNode<T> newNode; newNode = new QueueNode<T>(newElement, null); //create //newNode and assign newElement to newNode if (queueFront == null) //if initially the queue is empty { queueFront = newNode; queueRear = newNode; } else //add newNode at the end { queueRear.link = newNode; queueRear = queueRear.link; }}//end enqueue

Page 72: 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

72

front and backpublic T front() throws QueueUnderflowException{ if (isEmptyQueue()) throw new QueueUnderflowException(); return queueFront.info;}

public T back() throws QueueUnderflowException{ if (isEmptyQueue()) throw new QueueUnderflowException(); return queueRear.info;}

Page 73: 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

73

dequeue

• Method dequeuepublic void dequeue() throws QueueUnderflowException{ if (isEmptyQueue()) throw new QueueUnderflowException(); queueFront = queueFront.link; //advance queueFront if (queueFront == null) //if after deletion the queue queueRear = null; //is empty, set queueRear to null} //end dequeue