cmsc 132: object-oriented programming ii · queue queue is an adt data structure similar to stack,...

41
CMSC 132: Object-Oriented Programming II Stack and Queue 1 CMSC 132 Summer 2019

Upload: others

Post on 29-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

CMSC 132: Object-Oriented Programming II

Stack and Queue

1CMSC 132 Summer 2019

Page 2: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack

Allows access to only the last item inserted.

An item is inserted or removed from the stack from one end called the �top� of the stack.

This mechanism is called Last-In-First-Out (LIFO).

2CMSC 132 Summer 2019

Page 3: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Empty Stack

3CMSC 132 Summer 2019

Page 4: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Stack

Push 10

10Top

4CMSC 132 Summer 2019

Page 5: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Stack

Push 10

10

Top

Push 20

20

Push 30

30

5CMSC 132 Summer 2019

Page 6: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Stack

Push 10

10Top

Push 20

20

Push 30

Pop

6CMSC 132 Summer 2019

Page 7: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Stack

Push 10

10

Top

Push 20

20

Push 30

Pop Push 10

10

7CMSC 132 Summer 2019

Page 8: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Applications

JVM stack machineFunction CallExpression EvaluationPushdown AutomataRecursive à Iterative Function Conversion

8CMSC 132 Summer 2019

Page 9: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Interface

9

public interface Stack<T> extends Iterable<T>{void push(T t);T pop();// throws EmptyStackException;T peek() ;//throws EmptyStackException; boolean isEmpty();int size();

}

CMSC 132 Summer 2019

Page 10: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Implement Stack Using an Array

10CMSC 132 Summer 2019

public class ArrayStack<T> implements Stack<T> {private T[] items;private int N; // number of elements in the stackpublic ArrayStack(){

items = (T[])new Object[2];}

}

Page 11: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Empty Stack

N

4

3

2

1

0

11CMSC 132 Summer 2019

Page 12: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Pushing an item

N

4

3

2

1

0

Push 10

10

12CMSC 132 Summer 2019

Page 13: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Pushing more items

N

4

3

2

1

0 10

Push 10

Push 20

Push 30

20

30

What if the array

is full?13

CMSC 132 Summer 2019

Page 14: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Removing an item

N

4

3

2

1

0 10

Push 10

Push 20

Push 30

20Pop

14CMSC 132 Summer 2019

Page 15: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Removing an item

N

4

3

2

1

0 10

Push 10

Push 20

Push 30

20Pop

Pop

15CMSC 132 Summer 2019

Page 16: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Array Implementation: push

16

N

4

3

2

1

0 10CMSC 132 Summer 2019

public void push(T item){if(N == items.length){

resize(2 * items.length);} items[N++] = item;

}

Page 17: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Array Implementation: pop

Why?

17CMSC 132 Summer 2019

public T pop(){if(isEmpty()) throw new NoSuchElementException(); T item = items[--N];items[N] = null;return item;

}

Page 18: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Array Implementation: peek

18CMSC 132 Summer 2019

public T peek(){if(isEmpty())

throw new NoSuchElementException("Stack Underflow"); return items[N-1];

}

Page 19: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Implement Stack Using a Linked List

19

public class LinkedStack<T> implements Stack<T> {private int N; private Node first;

private class Node{private T data;private Node next;Node(T item){

data = item;}

}

CMSC 132 Summer 2019

Page 20: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Stack Example

Empty Stack

first = nullN =0

20CMSC 132 Summer 2019

Page 21: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Pushing an item

firstN =1

10

Push 10

21

public void push(T item){Node old = first; first = new Node(item); first.next = old; N++;

}

CMSC 132 Summer 2019

Page 22: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Pushing more items

firstN =3

30

Push 10

Push 20

Push 30

1020

22

public void push(T item){Node old = first; first = new Node(item); first.next = old; N++;

}

CMSC 132 Summer 2019

Page 23: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Pushing more items

firstN =2

20

Push 10

Push 20

Push 30

10

Pop

23

public T pop(){if(isEmpty()){

throw new NoSuchElementException();} T item = first.data;first = first.next;N--;return item;

}CMSC 132 Summer 2019

Page 24: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Other methodsPeek

size

isEmpty

24

public T peek(){if(isEmpty()){throw new NoSuchElementException();}return first.data;

}

public int size(){return N;

}

public boolean isEmpty(){return first == null;

}CMSC 132 Summer 2019

Page 25: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Testing the Stack

Size:77,7,6,6,5,5,4,4,3,3,2,2,1,1

25CMSC 132 Summer 2019

Page 26: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

QueueQueue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed.

This mechanism is called First-In-First-Out (FIFO).

Placing an item in a queue: enqueue.

Removing an item from a queue: dequeue

Applications: printer queue, keystroke queue, etc.

26CMSC 132 Summer 2019

Page 27: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Queue

27CMSC 132 Summer 2019

public interface Queue<T> extends Iterable<T> {void enqueue(T t);T dequeue();// throws EmptyStackException; T peek() ;//throws EmptyStackException;boolean isEmpty();int size();

}

Page 28: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Queue Example

Empty Queue

28CMSC 132 Summer 2019

Page 29: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Adding an item

Empty Queue

enqueue 10

10

29CMSC 132 Summer 2019

Page 30: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Adding more items

Empty Queue

enqueue 10

30

enqueue 20

20

enqueue 30

10

30CMSC 132 Summer 2019

Page 31: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Adding more items

Empty Queue

enqueue 10

30

enqueue 20

20

enqueue 30dequeue

31CMSC 132 Summer 2019

Page 32: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Circular Array Implementation

32

0 1 2 3 4 5

first last

N = 0

CMSC 132 Summer 2019

Page 33: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Circular Array Implementation

33

10 20 300 1 2 3 4 5

first last

enqueue 10enqueue 20enqueue 30

N =3

CMSC 132 Summer 2019

Page 34: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Circular Array Implementation

34

20 300 1 2 3 4 5

first last

enqueue 10enqueue 20enqueue 30

dequeue

N =2

CMSC 132 Summer 2019

Page 35: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Circular Array Implementation

35

20 30 40 500 1 2 3 4 5

first last

enqueue 10enqueue 20enqueue 30

dequeue

N =4

enqueue 40enqueue 50

CMSC 132 Summer 2019

Page 36: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Circular Array Implementation

36

20 30 40 50 600 1 2 3 4 5

firstlast

enqueue 10enqueue 20enqueue 30

dequeue

N =5

enqueue 40enqueue 50enqueue 60

CMSC 132 Summer 2019

Page 37: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Circular Array Implementation

37

30 40 50 60

0 1 2 3 4 5

firstlast

enqueue 10enqueue 20enqueue 30

dequeue

N =4

enqueue 40enqueue 50enqueue 60dequeue

CMSC 132 Summer 2019

Page 38: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Implementing push (enqueue)

38

30 40 50 600 1 2 3 4 5

firstlast

CMSC 132 Summer 2019

public void enqueue(E item) {if (N == q.length) resize(2*q.length); q[last++] = item;if (last == q.length) last = 0; //wrap-around N++;

}

Page 39: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

public E dequeue() {if(isEmpty()) throw

new NoSuchElementException("Queue underflow"); E item = q[first];q[first] = null;N--;first++;if (first == q.length) first = 0;// wrap-around

// shrink size of array if necessaryif (N > 0 && N == q.length/4)

resize(q.length/2); return item;

}

Implementing pop (dequeue)

39

30 40 50 600 1 2 3 4 5

firstlastCMSC 132 Summer 2019

Page 40: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

Implementing resize

40

70 30 40 50 60

0 1 2 3 4 5

30 40 50 60 70

first lastCMSC 132 Summer 2019

private void resize(int max){assert max >= N;E[] temp = (E[]) new Object[max]; for (int i = 0; i < N; i++) {

temp[i] = q[(first + i) % q.length];} q = temp;first = 0;last = N;

}

firstlast

Page 41: CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism

41

Linked List Queue

firstN =3

10 3020

last

• Add a new item• New node is added to tail (last.next)

• Remove a item• Remove the first node (first = first.next)

CMSC 132 Summer 2019