41
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

Embed Size (px)

Citation preview

Page 1: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

1

Stacks and Queues

Starring: IndexOutOfBOundsException

Co-Starring: NoSuchElementException

Page 2: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

2

Purpose:

In this lecture series we will discuss two more ADTs

The Stack and Queue data types are used to process data in a linear fashion

Page 3: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

3

Resources:

Barrons Chapter 9 p.300 (EXCEPT Priority Queues p.305-309)

Java Essentials Chapter 19 p.757

Java Essentials Study Guide Chapter 16 p.281

Java Methods Chapter 3 p.61

Page 4: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

4

Handouts:

1. Stack and Queue Interfaces & Implementations

2.The Following Java Code:

Stack.java

Queue.java

ArrayStack.java

ListQueue.java

Page 5: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

5

Intro:

A STACK is a linearly ordered list

A STACK is a data structure used for storing and retrieving data elements in

such a way that the element stored last will be retrieved first.

LIFO --- last in first out

Page 6: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

6

Intro:

A QUEUE is a data structure used for temporary storage from which the data elements are retrieved in the same order as they were stored.

FIFO – first in first outUsed for processing events that have

to be processed in the order of their arrival (events are buffered in the queue)

Page 7: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

7

Stacks:Think of the mountain of clothes piled

in a corner of your room

As you hurry to dress so as not to be late for school, you begin your search from the TOP of the clothes pile

The last stuff to be thrown on to the pile is the first to be examined

This is a “messy” stack

Page 8: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

8

TO implement a Stack ADT, you must provide a way to store (push) and remove (pop) elements from a Stack

Elements on the stack should be of the same type (or in the same class hierarchy)

A Stack has no limits as to its size

Page 9: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

9

TO add an element on to the stack you “Push” that item and it goes to the front of the stack

TO remove an element from the stack, you can only “Pop” off the element currently at the top of the stack

PUSH --- adds elements to the top of the stack

POP --- removes the element from the top of the stack

Page 10: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

10

These operations should be in Constant time O(1)

Other behaviors of the Stack should return the top element of the Stack without removing it (peekTop) and to see if the Stack has any elements left in it (isEmpty)

Page 11: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

11

For testing purposes, we will use the following as the standard Stack Interface (similar to ListNode when dealing with Linked Lists)

Page 12: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

12

public interface Stack { // postcondition: returns true if stack is empty, false otherwise boolean isEmpty(); // precondition: stack is [e1, e2, ..., en] with n >= 0 // postcondition: stack is [e1, e2, ..., en, x] void push(Object x); // precondition: stack is [e1, e2, ..., en] with n >= 1 // postcondition: stack is [e1, e2, ..., e(n-1)]; returns en // throws an unchecked exception if the stack is empty Object pop(); // precondition: stack is [e1, e2, ..., en] with n >= 1 // postcondition: returns en // throws an unchecked exception if the stack is empty Object peekTop(); }

Page 13: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

13

RUNTIME STACK

Remember the “runtime stack” ?

It uses stacks to handle function calls

The stack holds the function specific data PLUS the memory address of the NEXT instruction to execute

Elements are “on the stack”

Page 14: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

14

The stack is controlled by the two operations: PUSH & POP

Possible Implementation of a Stack is a singly linked list enhanced by an additional reference to the tail of the list Elements are added at the tail of the list and removed at the head of the list.

However, lets look at an ArrayList Implementation

Page 15: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

15

public class ArrayStack implements Stack { private ArrayList array; public ArrayStack() {

array = new ArrayList(); } public void push(Object x) {

array.add(x); } public Object pop() { return array.remove(array.size() - 1); } public Object peekTop() { return array.get(array.size() - 1); } public boolean isEmpty() { return array.size() == 0; } }

Page 16: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

16

To use the Stack:// SPVMMyStack mS = new ArrayStack();mS.push(new String (“Billy”);mS.push(new String (“Joe”);mS.push(new String (“Bob”);mS.push(new String (“Sally”);mS.push(new String (“Lucy”); The Stack Should now look like this:

LucySallyBobJoeBilly

Page 17: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

17

Object o = mS.pop( );

String s = (String)mS.pop( );

String y = (String)mS.peekTop( );

Boolean empty = mS.isEmpty( );The Stack Should now look like this:

Bob

Joe

Billy

The String y contains “Bob”

The Boolean empty is False

Page 18: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

18

Errors:

The pop and peekTop methods can throw an Unchecked Exception

IndexOutOfBOundsException or NoSuchElementException

in cases where the stack is empty

Page 19: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

19

If you use the ArrayList or the LinkedList Java class to implement your Stack, then these errors are handled , for you, from within these classes

Page 20: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

20

Queues:

Its lunch time and you leave class early to get into the food line

“Dr.Nick” dashes as fast as he can and is first to grab a tray and get into line

After Dr.Nick, “Naum” trips over a floor tile and skins his knee

Page 21: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

21

As a result, “Alden” stops to help wipe away Naum’s tears and to give him a Scooby Doo bandaid

So, Alden winds up behind Dr.Nick and Naum follows

Since the line is implemented as a Queue, Dr.Nick gets the best lunch choices and Naum gets the crumbs

Page 22: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

22

TO implement a Queue ADT, you must provide a way to store (enqueue) and remove (dequeue) elements from a Queue

Elements on the Queue should be of the same type (or in the same class hierarchy)

A Queue has no limits as to its size

Page 23: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

23

TO add an element on to the Queue you “enqueue” that item and it goes to the end of the Queue

TO remove an element from the Queue, you can only “dequeue” off the element currently at the top of the Queue

Page 24: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

24

ENQUEUE --- adds elements to the end of the Queue

DEQUEUE --- removes the element from the top of the Queue

These operations should be in Constant time O(1)

Page 25: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

25

Other behaviors of the Queue should return the top element of the Queue without removing it (peekFront) and to see if the Queue has any elements left in it (isEmpty)

For testing purposes, we will use the following as the standard Queue Interface:

Page 26: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

26

public interface Queue { // postcondition: returns true if queue is empty, false otherwise boolean isEmpty(); // precondition: queue is [e1, e2, ..., en] with n >= 0 // postcondition: queue is [e1, e2, ..., en, x] void enqueue(Object x); // precondition: queue is [e1, e2, ..., en] with n >= 1 // postcondition: queue is [e2, ..., en]; returns e1 // throws an unchecked exception if the queue is empty Object dequeue();

// precondition: queue is [e1, e2, ..., en] with n >= 1 // postcondition: returns e1 // throws an unchecked exception if the queue is empty Object peekFront(); }

Page 27: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

27

Ring Buffer An array used in a circular manner.

Adjust the pointer that defines the “logical” first array element.

The state of the queue is maintained with the help of 2 indices, FRONT and

REAR.

Page 28: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

28

Front points to the first element in the queue (returned by the next call to the

dequeue)

Rear points to the empty slot following the last stored element.

Enqueue method stores the next element in the slot pointed to by the rear and increments the rear index.

Page 29: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

29

PC’s have a keyboard queue implemented as a ring buffer. When a key is pressed its code does not go directly to the active program but is placed in the keyboard buffer until the program requests it.

Page 30: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

30

Lets look at a LinkedList Implementation:

Page 31: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

31

public class ListQueue implements Queue { private LinkedList list; public ListQueue() { list = new LinkedList(); } public void enqueue(Object x) { list.addLast(x); } public Object dequeue() { return list.removeFirst(); } public Object peekFront() { return list.getFirst(); } public boolean isEmpty() { return list.size() == 0; } }

Page 32: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

32

To use the Queue:// SPVMMyQueue mQ = new ListQueue();mQ.enqueue(new String (“Billy”);mQ.enqueue(new String (“Joe”);mQ.enqueue(new String (“Bob”);mQ.enqueue(new String (“Sally”);mQ.enqueue(new String (“Lucy”);

The Stack Should now look like this:BillyJoeBobSallyLucy

Page 33: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

33

Object o = mQ.dequeue( );

String s = (String)mQ.dequeue( );

String y = (String)mQ.peekFront( );

Boolean empty = mQ.isEmpty( );

The Stack Should now look like this:Bob

Sally

Lucy

The String y contains “Bob”The Boolean empty is False

Page 34: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

34

Queues are best when simulating bank lines, or any other system where there is a “First Come First Served” basis

Page 35: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

35

Lets look at Barrons Redial Telephone Feature on Page 305

Page 36: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

36

TPS: Implement a Queue using an Array (or ArrayList)

Implement the Possible Unchecked Exceptions

Draw array implementation on board (RingBuffer)

Page 37: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

37

AP AB Subset Requirements:

Although there is no standard Java Interface we need to be able to work with the AP interfaces for Stacks and Queues (handouts)

Students are also responsible for understanding that these ADT’s can be implemented as an array or a linked list, although you will NOT be tested on any SPECIFIC implementation

Page 38: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

38

AP AB Subset Requirements:

For our reference purposes we will look at an Array implementation of a stack called ArrayStack.java and we will look at a LinkedList implementation of a Queue called ListQueue.java

Page 39: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

39

AP AB Subset Requirements:

You will see Multiple Choice questions regarding analysis of Stack and Queue implementations and processes

Multiple Choice questions may focus on your ability to evaluate the effectiveness of and to determine the best use of Stacks and Queues

Page 40: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

40

Projects:

Barrons M/C Questions Chapter 9 p.310

Do ALL Questions Except the following:

# 14, 15

Decimal to Binary Expression McDonalds Palendrome

Page 41: 1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

41

TEST FOLLOWS COMPLETION OF

PROJECTS !!!