Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Linked Stack and Linked Queue.

Similar presentations


Presentation on theme: "COMP 103 Linked Stack and Linked Queue."— Presentation transcript:

1 COMP 103 Linked Stack and Linked Queue

2 RECAP-TODAY RECAP TODAY Linked data structures: Linked Node
LinkedList class TODAY Linked Stack Linked Queue Other linked collections

3 A Linked Stack Implement a Stack using a linked list.
Which end is the top of the stack? pop() push(“A”) Why is this a bad idea? data M J P C X TOP O(n) A H

4 A Linked Stack Make the top of the Stack be the head of the list. TOP
pop() push(“A”) data M TOP J P C X A H

5 Implementing LinkedStack
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 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... } data

6 More LinkedStack methods
public boolean empty() { return data == null; } public E peek() { if (data == null) throw new EmptyStackException(); return data.get(); 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); data M H data does 3 jobs...

7 Iterator (for any list)
Iterating down a stack. for (String str: myStack) System.out.println(str) myStack M X data P J C iterator node H

8 Iterator 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();

9 A Linked Queue #1 Put the front of the queue at the end of the list
offer(“A”) O(1) poll() O(n) back M A J P C H X Front

10 A Linked Queue #2 Put the front of the Queue at the start of the list.
poll() O(1) offer(“A”) O(n) is too Slow!! Front M J P C X H Back A

11 A Better Linked Queue Keep pointers to both ends! poll() O(1)
offer(“A”) O(1) M P Front Back X J C H Back A

12 Implementing 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() { … M Front Back A

13 LinkedQueue public boolean isEmpty() { return front == null; }
public int size () { if (front == null)   return 0; else   return front.size(); public E peek() { if (front == null) return null; else return front.get(); Front Back Front Back M A

14 LinkedQueue three cases: 0 items, 1 item, >1 items
public E poll() { if (front == null) return null; E ans = front.get(); front = front.next(); if (front == null) back = null; return ans; } Front Back three cases: 0 items, 1 item, >1 items Front Back M Front Back A A

15 LinkedQueue three cases: >1 item, 1 item, 0 items
public boolean offer(E item) { if (item == null) return false; if (front == null) {     back = new LinkedNode(item, null);     front = back; } else {     back.setNext(new LinkedNode(item, null));     back= back.next(); } return true; } Front Back M A three cases: >1 item, 1 item, 0 items Front Back Front Back A

16 Linked Stacks and Queues
Use a “header node” Keep link to head node, and maybe last node of linked list 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: 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.

17 Other linked collections
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?


Download ppt "COMP 103 Linked Stack and Linked Queue."

Similar presentations


Ads by Google