CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack,...
Transcript of CMSC 132: Object-Oriented Programming II · Queue Queue is an ADT data structure similar to stack,...
CMSC 132: Object-Oriented Programming II
Stack and Queue
1CMSC 132 Summer 2019
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
Stack Example
Empty Stack
3CMSC 132 Summer 2019
Stack Example
Stack
Push 10
10Top
4CMSC 132 Summer 2019
Stack Example
Stack
Push 10
10
Top
Push 20
20
Push 30
30
5CMSC 132 Summer 2019
Stack Example
Stack
Push 10
10Top
Push 20
20
Push 30
Pop
6CMSC 132 Summer 2019
Stack Example
Stack
Push 10
10
Top
Push 20
20
Push 30
Pop Push 10
10
7CMSC 132 Summer 2019
Applications
JVM stack machineFunction CallExpression EvaluationPushdown AutomataRecursive à Iterative Function Conversion
8CMSC 132 Summer 2019
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
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];}
}
Stack Example
Empty Stack
N
4
3
2
1
0
11CMSC 132 Summer 2019
Pushing an item
N
4
3
2
1
0
Push 10
10
12CMSC 132 Summer 2019
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
Removing an item
N
4
3
2
1
0 10
Push 10
Push 20
Push 30
20Pop
14CMSC 132 Summer 2019
Removing an item
N
4
3
2
1
0 10
Push 10
Push 20
Push 30
20Pop
Pop
15CMSC 132 Summer 2019
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;
}
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;
}
Array Implementation: peek
18CMSC 132 Summer 2019
public T peek(){if(isEmpty())
throw new NoSuchElementException("Stack Underflow"); return items[N-1];
}
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
Stack Example
Empty Stack
first = nullN =0
20CMSC 132 Summer 2019
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
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
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
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
Testing the Stack
Size:77,7,6,6,5,5,4,4,3,3,2,2,1,1
25CMSC 132 Summer 2019
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
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();
}
Queue Example
Empty Queue
28CMSC 132 Summer 2019
Adding an item
Empty Queue
enqueue 10
10
29CMSC 132 Summer 2019
Adding more items
Empty Queue
enqueue 10
30
enqueue 20
20
enqueue 30
10
30CMSC 132 Summer 2019
Adding more items
Empty Queue
enqueue 10
30
enqueue 20
20
enqueue 30dequeue
31CMSC 132 Summer 2019
Circular Array Implementation
32
0 1 2 3 4 5
first last
N = 0
CMSC 132 Summer 2019
Circular Array Implementation
33
10 20 300 1 2 3 4 5
first last
enqueue 10enqueue 20enqueue 30
N =3
CMSC 132 Summer 2019
Circular Array Implementation
34
20 300 1 2 3 4 5
first last
enqueue 10enqueue 20enqueue 30
dequeue
N =2
CMSC 132 Summer 2019
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
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
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
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++;
}
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
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
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