Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
COMP 103
Linked Stack
and
Linked Queue
RECAP-TODAY
2
RECAP
Linked data structures: Linked Node
LinkedList class
TODAY
Linked Stack
Linked Queue
Other linked collections
A Linked Stack
3
Implement a Stack using a linked list.
Which end is the top of the stack?
M
data
J
P
C
X
TOP
pop()
A
H
O(n)
push(“A”)
Why is this a bad idea?
A Linked Stack
4
Make the top of the Stack be the head of the list.
M
data
TOP
J
P
C
X
A
pop()
push(“A”)
H
Implementing LinkedStack
5
Implementing a stack using LinkedNode as underlying
data structure (instead of array)
public class LinkedStack <E> extends AbstractCollection<E> {
private LinkedNode<E> data = null;
public LinkedStack() { }
// constructor
data
public int size() { … }
public boolean empty() { … }
public E peek() { … }
// Usually called top
public E pop() { … }
public void push(E item) { … }
public int search(Object o) { … }
// plus others inherited from AbstractCollection, such as toArray() and others...
}
More LinkedStack methods
6
public boolean empty() {
return data == null;
data
data
}
public E peek() {
if (data == null) throw new EmptyStackException();
return data.get();
}
M
public E pop() {
if (data == null) throw new EmptyStackException();
E ans = data.get();
data = data.next();
return ans;
}
public void push(E item) {
if (item == null) throw new IllegalArgumentException();
data = new LinkedNode(item, data);
does 3 jobs...
}
H
Iterator (for any list)
7
Iterating down a stack.
myStack
M
data
X
P
J
C
iterator
node
H
for (String str: myStack)
System.out.println(str)
Iterator
8
public Iterator <E> iterator() {
return new LinkedNodeIterator(data);
}
private class LinkedNodeIterator <E> implements Iterator <E> {
private LinkedNode<E> node; // node containing next item
public LinkedNodeIterator (LinkedNode <E> node) {
this.node = node;
}
public boolean hasNext () {
return (node != null);
}
public E next () {
if (node == null) throw new NoSuchElementException();
E ans = node.get();
node = node.next();
return ans;
}
public void remove () {
throw new UnsupportedOperationException();
}
}
A Linked Queue #1
9
Put the front of the queue at the end of the list
M
back
J
P
C
X
H
A
offer(“A”)
O(1)
poll()
O(n)
Front
A Linked Queue #2
10
Put the front of the Queue at the start of the list.
M
Front
J
P
C
X
H
Back
poll()
offer(“A”)
O(1)
A
O(n) is too Slow!!
A Better Linked Queue
11
Keep pointers to both ends!
M
P
Front
X
J
Back
C
H
poll()
Back
O(1)
A
offer(“A”)
O(1)
Implementing LinkedQueue
12
public class LinkedQueue <E> implements AbstractQueue <E> {
private LinkedNode<E> front = null;
private LinkedNode<E> back = null;
public LinkedQueue() { }
public int size() { …
public boolean isEmpty() { …
public E peek() { …
public E poll() { …
public void offer(E item) { …
public Iterator <E> iterator() { …
M
Front
Back
A
LinkedQueue
13
public boolean isEmpty() {
return front == null;
}
Front
Back
public int size () {
if (front == null) return 0;
else
return front.size();
}
public E peek() {
if (front == null) return null;
else return front.get();
}
M
Front
Back
A
LinkedQueue
14
public E poll() {
if (front == null) return null;
E ans = front.get();
Front
Back
front = front.next();
three cases:
0 items,
1 item,
>1 items
if (front == null) back = null;
return ans;
}
M
Front
Front
A
Back
A
Back
LinkedQueue
15
public boolean offer(E item) {
if (item == null) return false;
Front
if (front == null) {
back = new LinkedNode(item, null);
front = back;
Back
}
else {
back.setNext(new LinkedNode(item, null));
back= back.next();
}
return true;
}
Front
Front
A
Back
Back
M
A
three cases:
>1 item,
1 item,
0 items
Linked Stacks and Queues
16
Use a “header node”
Important to choose the right end.
Easy to add or remove from head of a linked list
Easy to add to last node of linked list (if have pointer to tail too)
Hard to remove the last node of linked list (do you see why?)
Linked Stack and Queue:
Keep link to head node, and maybe last node of linked list
all main operations are O(1)
Can combine Stack and Queue giving Dequeue.
addFirst, addLast, removeFirst (or pushLeft, pushRight, popLeft)
How can we make all operations fast?
See the java “LinkedList” class.
Other linked collections
17
Set
Can
store sorted or unsorted
What is the cost of add, remove, contains, size, equals in each
case?
Bag
Same
as set, but add a count to each node.
Map
Same
as set, but store value as well as key in each node
Or store pointed to a key-value pair
What is the cost of get, put, remove?