Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Linked Stack and Linked Queue. 2 RECAP-TODAY RECAP  Linked data structures: Linked Node  LinkedList class TODAY  Linked Stack  Linked Queue.

Similar presentations


Presentation on theme: "COMP 103 Linked Stack and Linked Queue. 2 RECAP-TODAY RECAP  Linked data structures: Linked Node  LinkedList class TODAY  Linked Stack  Linked Queue."— Presentation transcript:

1 COMP 103 Linked Stack and Linked Queue

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

3 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 ? J H M C X P data A TOP O(n)

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

5 5 Implementing LinkedStack  Implementing a stack using LinkedNode as underlying data structure (instead of array) public class LinkedStack extends AbstractCollection { private LinkedNode 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 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); } M H data does 3 jobs...

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

8 8 Iterator public Iterator iterator() { return new LinkedNodeIterator(data); } private class LinkedNodeIterator implements Iterator { private LinkedNode node; // node containing next item public LinkedNodeIterator (LinkedNode 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 9 A Linked Queue #1  Put the front of the queue at the end of the list  offer(“A”) O(1)  poll() O(n) J M C X P H back A Front

10 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!! J H M C X P Front A Back

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

12 12 Implementing LinkedQueue public class LinkedQueue implements AbstractQueue { private LinkedNode front = null; private LinkedNode back = null; public LinkedQueue() { } public int size() { … public boolean isEmpty() { … public E peek() { … public E poll() { … public void offer(E item) { … public Iterator iterator() { … M A Front Back

13 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(); } M A Front Back Front Back

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

15 15 LinkedQueue 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; } M A Front Back A Front Back Front Back three cases: >1 item, 1 item, 0 items

16 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 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. 2 RECAP-TODAY RECAP  Linked data structures: Linked Node  LinkedList class TODAY  Linked Stack  Linked Queue."

Similar presentations


Ads by Google