stacks and queues - university of southern californiabytes.usc.edu › cs455 › curr › lectures...

19
Stacks & Queues [Bono] 1 Stacks and Queues Introduction to abstract data types (ADTs) Stack ADT – applications interface for Java Stack ex use: reverse a sequence of values Queue ADT – applications interface for Java Queue Time permitting: additional example of implementing an interface

Upload: others

Post on 29-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 1

Stacks and Queues

• Introduction to abstract data types (ADTs)• Stack ADT

– applications– interface for Java Stack– ex use: reverse a sequence of values

• Queue ADT– applications– interface for Java Queue

• Time permitting: additional example of implementing an interface

Page 2: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Announcements

• MT 2 Sample Exams will be published in the next few days

• Class participation in online lecture:– Your mic should be muted most of the time– Click the hand icon to raise your hand and I can call on

you– Or, unmute and speak to get my attention– Can also use chat window; but I may not notice

Stacks & Queues [Bono] 2

Page 3: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Abstract Data Types (ADTs)• An abstract idea of a data structure• Can be implemented with a class• ADT operations = class methods• Some ADT examples:

Stack, Queue, Set, Map• Some concrete data structure examples:

array, linked list, hash table.• The ADT is implemented as a class that encapsulates a

concrete data structure, and often has more than one possible implementation

• Can also be modeled using Java interface feature and classes that implement the interface

Stacks & Queues [Bono] 3

Page 4: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 4

Stacks

• a collection of things (like an array is)• but with restricted access• the only item you can look at or remove is the last item you

inserted. Last In First Out (LIFO).• E.g. stack of dishes

– push a plate on the top of the stack– pop a plate from the top of the stack– examine the plate at the top of the stack– ask if the stack is empty

Page 5: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 5

Stacks for method call/return

• a second example of a stack is the system stack (a.k.a., run-time stack, or call stack)

• element is called a stack frame (aka, activation record):– all the data associated with that call: e.g., locals, params, return addr.

• method call/return follows LIFO order.• last method called will return before any ones that called it.• Let’s look at an example:

void main() {B();D();B();

}

void B() {C();D();

}

void C() {D();

}

void D() {return;

}

Page 6: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 6

Example of method call/returnvoid main() {B();D();B();

}

void B() {C();D();

}

void C() {D();

}

void D() {return;

}

Page 7: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 7

Java Stack classimport java.util.Stack;

Stack<Integer> s = new Stack<Integer>();// creates an empty stack

s.push(3); // add an element to the top of// the stack

int n = s.peek(); // returns top element in the// stack (does not modify stack)

int top = s.pop(); // pops top element off of// stack and returns it

s.empty(); // tells whether stack is empty

Page 8: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 8

Using Stack operationsStack<Character> s = new Stack<Character>();

// creates an empty stack of characters

s.push(‘a’);s.push(‘b’);s.push(‘c’);System.out.println(s.peek());

s.pop();s.push(‘d’);System.out.println(s.peek());

s.pop();s.pop();System.out.println(s.empty());

Page 9: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 9

Ex: Reversing a sequence• Stacks are good for reversing things

– The last item you put on is the first one you get out– The second to last item you put on will be the second

item you get out, etc.

• Example problem: read in a bunch of integer values and then print them out in reverse order. void reverse(Scanner in) {

Page 10: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 10

Stack representations

• How to represent?• Where should top be?• What is big-O of each operation?

Page 11: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 11

Queue

• A container of elements that can be accessed/inserted/removed like standing in line.

• Enter queue at the end (enqueue)• Exit queue at the front (dequeue)• Can access front element• First-in First-out (FIFO)

Page 12: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 12

Applications of Queues

• Major application is to represent lines (queues) in software.

• E.g., big in operating systems– print queue – print jobs waiting to print. They are

processed in FIFO order.– process queue – processes waiting for their turn to

execute.

Page 13: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 13

Queue applications (cont.)

• also in simulations– queue of events waiting to be processed– simulating queues from the world we are simulating

(e.g., Bank line, airplanes waiting to take off)• and Java GUI system

– queue of user input events waiting to be processed– (e.g., mouse clicks, keyboard strokes)

Page 14: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 14

Java Queue• Queue is an interface rather than a class.• LinkedList implements this interface.• To create one:

Queue<MyType> q = new LinkedList<MyType>();

• means we intend to only use the LL ops specified by Queue.

Page 15: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 15

Java Queue interfaceimport java.util.Queue;import java.util.LinkedList;Queue<Integer> q = new LinkedList<Integer>();

// creates an empty queue of integers

q.add(3); // add an element to the end

int n = q.peek(); // returns first element in// the queue (does not modify queue)

int front = q.remove();// removes first element from queue// and returns it

q.isEmpty(); // tells whether queue is empty

Page 16: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 16

Using queue operations

Queue<Character> q = new LinkedList<Character>();

q.add(‘a’);q.add(‘b’);q.add(‘c’);System.out.println(q.peek());

q.remove();q.add(‘d’);System.out.println(q.peek());

q.remove();q.remove(); System.out.println(q.isEmpty());

Page 17: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 17

Queue representations

• How to represent?• Where is front and end?• What is big-O of each operation?

Page 18: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Stacks & Queues [Bono] 18

Related data structures• Why is Queue an interface?

– some other queue implementations are used in multi-thread Java applications

– one queue implementation is a priority queue• not FIFO, but…• an element’s priority determines how soon it gets to

the front of the line.

Page 19: Stacks and Queues - University of Southern Californiabytes.usc.edu › cs455 › curr › lectures › notes › stackQueue.pdf · Stacks & Queues [Bono] 5 Stacks for method call/return

Additional example of implementing an interface

• Problem: sort an array of Rectangle’s in increasing order by area.

• Do not implement your own sort method!

public static void sortIncrByArea(Rectangle[] rects) {

Arrays.sort(

Stacks & Queues [Bono] 19