java linkedlist
play

Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev - PowerPoint PPT Presentation

COMP 250 Lecture 5 doubly linked lists Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev element head Each node has a reference to the next node and to the previous node. tail 2 class DNode< E > { DNode< E


  1. COMP 250 Lecture 5 doubly linked lists Java LinkedList Sept. 16, 2016 1

  2. Doubly linked lists next prev element head Each node has a reference to the next node and to the previous node. tail 2

  3. class DNode< E > { DNode< E > next; Dnode< E > prev; E element; // constructor DNode( E e ) { element = e; prev = null; next = null; next element } } prev 3

  4. Motivation: recall removeLast ( ) for singly linked lists next element head The only way to access the element before the tail was to loop through all elements from the head. This took time O(N) where N is the size tmp of the list. tail 4

  5. For a doubly linked list, removing the last element is much faster. next prev element head removeLast(){ tail = tail.prev tail.next = null size = size – 1 : } tail 5

  6. Time Complexity (N = list size) array list SLinkedList DLinkedList addFirst O( N ) O( 1 ) O( 1 ) removeFirst O( N ) O( 1 ) O( 1 ) addLast O( 1 ) O( 1 ) O( 1 ) removeLast O( 1 ) O( N ) O( 1 ) 6

  7. List Operations null get(i) head set(i,e) add(i,e) remove(i) tail : null For a linked list, many operations require access to node i. The “edge cases” ( i = 0, i = size – 1) usually require extra code, which can lead to coding errors . 7

  8. Common linked list trick: avoid edge cases with “dummy nodes” null dummyHead null i = 0 i = 1 i = 2 i = 3 null dummyTail 8 null

  9. class DLinkedList<E>{ DNode<E> dummyHead; DNode<E> dummyTail; null int size; dummyHead null : dummyTail null // constructor null DLinkedList<E>(){ dummyHead = new DNode<E>(); dummyTail = new DNode<E>(); dummyHead.next = dummyTail; dummyTail.prev = dummyHead; size = 0; } 9

  10. dummyHead size 4 DLinkedList< Shape > object dummyTail Q: How many objects in total in this figure? A: 1 + 6 + 4 = 11 10

  11. remove( i ) { // recall end of lecture 3 on arrays node = getNode( i ) // next slide node.next.prev = node.prev node.prev.next = node.next size = size - 1 } BEFORE AFTER next prev element next prev element i – 1 node i i + 1 11

  12. [SLIDE WAS ADDED AFTER LECTURE] getNode( i ) { // check that 0 <= i < size (omitted) node = dummyHead.next for (k = 0; k < i; k ++) node = node.next return node } 12

  13. More efficient (half the time)… getNode( i ) { if ( i < size/2 ){ // iterate from head node = dummyHead.next for (k = 0; k < i; k ++) node = node.next; } else{ // iterate from tail node = dummyTail.prev for ( k = size-1; k > i; k -- ) node = node.prev } return node } 13

  14. Time Complexity (N = list size) array list SLinkedList DLinkedList addFirst O( N ) O( 1 ) O( 1 ) removeFirst O( N ) O( 1 ) O( 1 ) addLast O( 1 ) O( 1 ) O( 1 ) removeLast O( 1 ) O( N ) O( 1 ) remove( i ) ? ? ? 14

  15. Time Complexity (N = list size) array list SLinkedList DLinkedList addFirst O( N ) O( 1 ) O( 1 ) removeFirst O( N ) O( 1 ) O( 1 ) addLast O( 1 ) O( 1 ) O( 1 ) removeLast O( 1 ) O( N ) O( 1 ) remove( i ) O(N) O( N ) O( N ) As I will discuss that later, “O( )” ignores constant factors. 15

  16. Java LinkedList class https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html It uses a doubly linked list as the underlying data structure. It has some methods that ArrayList doesn’t have e.g.: • addFirst() • removeFirst() • addLast() • removeLast() 16

  17. Q: What is the time complexity of the following ? LinkedList< E > list = new LinkedList< E >( ) ; for (k = 0; k < N; k ++) // N is some constant list.addFirst ( new E( …. ) ); // or addLast(..) for (k = 0; k < k < list.size(); k ++) list.get( k ); 17

  18. Q: What is the time complexity of the following ? LinkedList< E > list = new LinkedList< E >( ) ; for (k = 0; k < N; k ++) // N is some constant list.addFirst ( new E( …. ) ); // or addLast(..) 𝟐 + 𝟐 + 𝟐 + … . 𝟐 = 𝑶 𝑷( 𝑶 ) A: for (k = 0; k < k < list.size(); k ++) list.get( k ); I am omitting what I would do with this element since that’s not the point here e.g. I could print it. 18

  19. Q: What is the time complexity of the following ? LinkedList< E > list = new LinkedList< E >( ) ; for (k = 0; k < N; k ++) // N is some constant list.addFirst ( new E( …. ) ); // or addLast(..) 𝟐 + 𝟐 + 𝟐 + … . 𝟐 = 𝑶 𝑷( 𝑶 ) A: for (k = 0; k < k < list.size(); k ++) // size == N list.get( k ); 𝑶 𝑶+𝟐 𝑷( 𝑶 𝟑 ) 𝟐 + 𝟑 + 𝟒 + … . 𝐎 = A: 𝟑 Here I am assuming the first getNode(i) is used, which always starts at the head. See the Exercises for the expression when the more efficient getNode(i) method is used. 19

  20. ASIDE: Java ‘enhanced for loop’ for (k = 0; k < list.size(); k ++) ……. A more efficient way to iterate through elements in a LinkedList object is to use: for (E e : list) // ‘list’ references the LinkedList< E > object // Do something with each element e in list 20

  21. What about “Space Complexity” ? null null All three data structures use space O(N) for a list of N elements. 21

  22. Java terminology (time permitting) • method “signature” • name • number and type of parameters, • return type • method “overloading” • add( int index, E element) • add( E element ) • remove(E element) • remove(int i) 22

  23. Java terminology What is method “overloading” vs. “overriding” ? Classes can “inherit” methods from other classes. Sometimes you do not want a class to inherit the method, however, and so you “override” it by writing a more suitable one. We will learn about inheritance formally at the end of the course….. 23

  24. Announcements • Assignment 1 posted (due in ~2 weeks) • Exercises for singly linked lists (practice coding) • Eclipse (IDE) tutorials next week. Goodbye DrJava ! Hello Eclipse ! 24

  25. I asked the TA’s which IDE they use: • Eclipse or sublime • jetbrains, IntelliJ • Eclipse • Eclipse, also IntelliJ • Eclipse • Simple text editor / vim + command line. (However I had to use Eclipse and DrJava in the past.) • Eclipse, but I like using text editor + command line for small things. • Eclipse (netbeans for GUIs) 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend