Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.

Similar presentations


Presentation on theme: "1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today."— Presentation transcript:

1 1

2 Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today

3 Java collections library public interface java.util.List booleanadd(int index, E element) Inserts the specified element at the specified position in this list (optional operation). Eremove(int index)Removes the element at the specified position in this list … Implementations: LinkedListdoubly-linked list implementation ArrayListresizable-array implementation 3

4 Example client 4 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); }

5 Example client Input: “judge me by my size do > you > ” 5 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); }

6 Example client Can we implement StackQueue efficiently using a linked list? 6 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); }

7 Example client Can we implement StackQueue efficiently using a linked list? enqueue and dequeue can run in Θ(1) but pop will need to run in Θ(n) we can do better 7 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); }

8 Doubly Linked Lists (Java implementation in Assignment 2) https://www.scss.tcd.ie/Vasileios.Koutavas/teaching/cs2010/michaelmas1516/a ssignment-2/ 8

9 Doubly Linked Lists 9 head  tail  tobeornotto null class DLLNode { String item; DLLNode next; DLLNode prev; }

10 Doubly Linked Lists 10 head  tail  tobeornotto null class DLLNode { String item; DLLNode next; DLLNode prev; } nullto notbe null Empty DLL The only node of a DLL with 1 node The first node of a DLL with >1 node The last node of a DLL with >1 node A middle node of a DLL with >2 items States of nodes:

11 Doubly Linked Lists classDLLofString DoublyLinkedList() void insertFirst(String s)inserts s at the head of the list String getFirst()returns string at the head of the list boolean deleteFirst()removes string at the head of the list void insertLast(String s)inserts s at the end of the list String getLast(String s)returns string at the end of the list boolean deleteLast()removes string at the end of the list void insertBefore(int pos, String s)inserts s before position pos String get(int pos)returns string at position pos boolean deleteAt(int pos)deletes string at position pos Interface somewhat different than that of java.util.List. How to make interface generic? class DoublyLinkedList 11

12 Doubly Linked Lists classDLLofString DoublyLinkedList() void insertFirst(String s)inserts s at the head of the list String getFirst()returns string at the head of the list boolean deleteFirst()removes string at the head of the list void insertLast(String s)inserts s at the end of the list String getLast(String s)returns string at the end of the list boolean deleteLast()removes string at the end of the list void insertBefore(int pos, String s)inserts s before position pos String get(int pos)returns string at position pos boolean deleteAt(int pos)deletes string at position pos Interface somewhat different than that of java.util.List. How to make interface generic? class DoublyLinkedList 12

13 Three main cases to deal with 13 head  tail  tobeornotto null head tail  to null head  null tail  null >1 node 1 node 0 nodes

14 14 /** * Inserts an element at the end of the doubly linked list * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertLast( T data )

15 void insertLast(“be”) 15 DLL of size 0 ? DLL of size 1 ? DLL of size > 1 ? cases for size of the DLL Richard Feynman: “Consider simple examples but not too simple!” (paraphrase)

16 insertLast(“be”) 16 head  tail  tobeornotto null >1 node

17 insertLast(“be”) 17 head  tail  tobeornottobe null >1 node

18 insertLast(“be”) 18 head  tail  tobeornottobe null >1 node

19 insertLast(“be”) 19 head  tail  tobeornottobe null >1 node

20 insertLast(“be”) 20 head = tail  to null 1 nodes

21 insertLast(“be”) 21 head = tail  tobe null 1 node

22 insertLast(“be”) 22 head = tail  tobe null 1 node

23 insertLast(“be”) 23 head  tail  tobe null 1 node

24 insertLast(“be”) 24 head  tail  tobe null 1 node * Same as the case with >1 node

25 insertLast(“be”) 25 head  nulltail  null 0 nodes

26 insertLast(“be”) 26 head  nulltail  null be null 0 nodes

27 insertLast(“be”) 27 head = tail  be null 0 nodes

28 28 /** * Inserts an element at the beginning of the doubly linked list * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertFirst( T data ) how can we implement this?

29 void insertFirst(“be”) 29 DLL of size 0 ? DLL of size 1 ? DLL of size > 1 ? cases for size of the DLL write down an example for each case

30 30 /** * Inserts an element in the doubly linked list * @param pos : The integer location at which the new data should be * inserted in the list. We assume that the first position in the list * is 0 (zero). If pos is less than 0 then add to the head of the list. * If pos is greater or equal to the size of the list then add the * element at the end of the list. * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertBefore( int pos, T data )

31 void insertBefore(pos,“be”) 31 pos == 0 (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 DLL of size 1 DLL of size > 1

32 void insertBefore(pos,“be”) 32 pos == 0 (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) DLL of size 1 insertFirst(“be”) DLL of size > 1 insertFirst(“be”)

33 void insertBefore(pos,“be”) 33 pos == 0 (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”)not possible 0 < pos < -1 DLL of size 1 insertFirst(“be”)not possible 0 < pos < 0 DLL of size > 1 insertFirst(“be”)?

34 void insertBefore(pos,“be”) 34 pos == 0 (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”)not possible 0 < pos < -1 insertFirst(“be”) (here pos == -1) DLL of size 1 insertFirst(“be”)not possible 0 < pos < 0 insertFirst(“be”) (here pos == 0) DLL of size > 1 insertFirst(“be”)??

35 void insertBefore(pos,“be”) 35 pos == 0 (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”)not possible 0 < pos < -1 insertFirst(“be”) (here pos == -1) insertFirst(“be”)InsertLast(“be”) DLL of size 1 insertFirst(“be”)not possible 0 < pos < 0 insertFirst(“be”) (here pos == 0) insertFirst(“be”)InsertLast(“be”) DLL of size > 1 insertFirst(“be”)?? InsertLast(“be”)

36 void insertBefore(pos,“be”) 36 pos == 0 (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”)not possible 0 < pos < -1 insertFirst(“be”) (here pos == -1) insertFirst(“be”)InsertLast(“be”) DLL of size 1 insertFirst(“be”)not possible 0 < pos < 0 insertFirst(“be”) (here pos == 0) insertFirst(“be”)InsertLast(“be”) DLL of size > 1 insertFirst(“be”)?same as case to the left insertFirst(“be”)InsertLast(“be”)

37 insertBefore(2,“nice”) 37 head  tail  tobeornottobe null

38 insertBefore(2,“nice”) 38 head  ptr  tail  tobeornottobe null

39 insertBefore(2,“nice”) 39 head  ptr  tail  tobeornottobe null tmp  nice null

40 insertBefore(2,“nice”) 40 head  ptr  tail  tobeornottobe null tmp  nice null

41 insertBefore(2,“nice”) 41 head  ptr  tail  tobeornottobe null tmp  nice null

42 insertBefore(2,“nice”) 42 head  ptr  tail  tobeornottobe null tmp  nice null

43 insertBefore(2,“nice”) 43 head  ptr  tail  tobeornottobe null tmp  nice


Download ppt "1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today."

Similar presentations


Ads by Google