Você está na página 1de 20

Linked Lists:

Stacks and Queues

COMP 103 #15


Menu

• A Stack using a Linked List


• A Queue using a Linked List

• Test (Reminder):
• There will be a 90 minute test
• Friday 15th at 3-5pm
• EA006

COMP103 – 2006/T3 2
A Linked Stack I
• Implement a Stack using a linked list.

M Which end is the top of the stack?


data

J
P C

X
• pop()
TOP
A H
• push(“A”)

Why is this a bad idea?

COMP103 – 2006/T3 3
A Linked Stack II
• Make the top of the Stack be the front of the list.

M
data
TOP
J
P C

A
H

• pop()

• push(“A”) Why is this also a bad


idea? 4
COMP103 – 2006/T3
Linked Stack III

Problem: the stack pointer is changing every


time.
data

• stack = stack.push(“A”); M

• stack = stack.pop();
• But how do we return the value pop’d?

• We have to have a head node for the list


that hides the fact that the first node
changes.
• The head node is often (but not always) a
COMP103 – 2006/T3 5
different type.
Head Nodes

• A head node ‘wraps’ the list so that


changes to the first list node do not
change the reference to the start of the
list.
• A head node does not store list data, and
must be present, even if the list is empty.
• A head node provides a useful place to
store useful maintenance information
about the list:
• number of elements.
• pointers to say, the tail of the list. 6
COMP103 – 2006/T3
LinkedStack
public class LinkedStack <E> extends AbstractCollection <E> {

private LinkedNode<E> data = null;

public LinkedStack(){  }

public int size(){…

public boolean isEmpty(){…

public E peek(){…

public E pop(){…

public void push(E item){…

public Iterator <E> iterator(){

COMP103 – 2006/T3 7
LinkedStack
public boolean isEmpty(){
return data==null;
}
data
public int size () { M
if (data == null)  
return 0; H
else  
return data.size();
}

• Need a size method in LinkedNode class to return the number of


nodes, or
• We can store the number of elements (push +1, pop -1) and
simply return that value: O(1) rather than O(n).

COMP103 – 2006/T3 8
LinkedStack
public E peek(){
if (data==null) throw new EmptyStackException();
return data.get();
} data

public E pop(){
if (data==null) throw new EmptyStackException();
E ans = data.get();
data = data.next();
return ans; M
}
public void push(E item){
if (item == null) throw new IllegalArgumentException();
data = new LinkedNode(item, data);
}
public Iterator <E> iterator(){ H
    return new LinkedNodeIterator(data);
}

COMP103 – 2006/T3 9
LinkedNode methods
Size() returns number of nodes in list starting with this
node
B

public int size (){


if (next == null) return 1;
else            return 1 + next.size(); M
} H

 OR

public int size (){


 int ans = 0;
 for (LinkedNode<E> rest = this; rest!=null; rest=rest.next)
     ans++;
 return ans;
}
COMP103 – 2006/T3 10
Iterator
public class LinkedNodeIterator <E> implements Iterator <E>{
private LinkedNode<E> node;        // node containing next item
to return
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();
  }
COMP103 – 2006/T3 11
}
Iterator
• Iterating down a stack.
myStack
M X
data P

C
itr

node
H

for (Iterator<String> itr=myStack.iterator(); iterator.hasNext(); )


System.out.println(itr.next())

COMP103 – 2006/T3 12
Linked Queue I
• Which is the front of the queue, which is the back?

M
back

J
P C
X

H Front
A

• poll() (deQueue) O(n) is too Slow!!


• offer(“A”) (enQueue) O(1) is fine

COMP103 – 2006/T3 13
A Linked Queue II
• Implement a Queue using a linked list.

M
Front

J
P C

Back
• poll() deQueue
A

• offer(“A”) enQueue O(n) is too Slow!!


COMP103 – 2006/T3 14
A Better Linked Queue
• Implement a Queue using a linked list.

M P

Front
X J

Back

• poll() (deQueue) H Back


Both O(1)!

• offer(“A”) (enQueue)
A
COMP103 – 2006/T3 15
LinkedQueue
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(){


COMP103 – 2006/T3 16
LinkedQueue
public boolean isEmpty(){
return front==null; M
} Front

// Or store it as in LinkedStack Back


public int size () { A
if (front == null) return 0;
else return front.size();
}

Front
Front
A
Back
Back

COMP103 – 2006/T3 17
LinkedQueue
public boolean offer(E item){ M
if (item == null) return false; Front
if (front == null){
      back = new LinkedNode(item, null);
Back
      front = back; A
} else {
      back.setNext(new LinkedNode(item, null));
      back= back.next();
}
return true; Front
} Front
A Back
Back

COMP103 – 2006/T3 18
LinkedQueue
public E peek(){
if (front==null) return null; M
else return front.get(); Front
}
Back
A
public E poll(){
if (front==null) return null;
E ans = front.get();
front = front.next();
if (front==null) back = null; Front
return ans;
}
Front Back
A

Back

COMP103 – 2006/T3 19
Linked Stack and Queue
• Uses a “header node”
• contains link to head node, and maybe last node of
linked list
• management information about the data.

• 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 we have pointer
to tail
• hard to remove from last node of a linked list

• Linked Stack and Queue:


• all main operations are O(1)

•COMP103
Next –Lecture:
2006/T3
Variations of Lists. 20

Você também pode gostar