data structures in java
play

Data Structures in Java Lecture 2: Array and Linked Lists. 9/9/2015 - PowerPoint PPT Presentation

Data Structures in Java Lecture 2: Array and Linked Lists. 9/9/2015 Daniel Bauer 1 The List ADT A 0 A 1 A 2 A 3 A 4 A 5 A 6 The List ADT A list L is a sequence of N objects A 0 , A 1, A 2, , A N-1 A 0 A 1 A 2 A 3 A 4 A 5 A 6 The List


  1. Data Structures in Java Lecture 2: Array and Linked Lists. 9/9/2015 Daniel Bauer 1

  2. The List ADT A 0 A 1 A 2 A 3 A 4 A 5 A 6

  3. The List ADT • A list L is a sequence of N objects A 0 , A 1, A 2, …, A N-1 A 0 A 1 A 2 A 3 A 4 A 5 A 6

  4. The List ADT • A list L is a sequence of N objects A 0 , A 1, A 2, …, A N-1 • N is the length/size of the list. List with length N=0 is called the empty list. A 0 A 1 A 2 A 3 A 4 A 5 A 6

  5. The List ADT • A list L is a sequence of N objects A 0 , A 1, A 2, …, A N-1 • N is the length/size of the list. List with length N=0 is called the empty list. • A i follows/succeeds A i-1 for i > 0. A 0 A 1 A 2 A 3 A 4 A 5 A 6

  6. The List ADT • A list L is a sequence of N objects A 0 , A 1, A 2, …, A N-1 • N is the length/size of the list. List with length N=0 is called the empty list. • A i follows/succeeds A i-1 for i > 0. • A i precedes A i+1 for i < N. A 0 A 1 A 2 A 3 A 4 A 5 A 6

  7. Typical List Operations A 0 A 1 A 2 A 3 A 4 A 5 A 6

  8. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6

  9. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty()

  10. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size()

  11. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k)

  12. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k) • boolean insert(x, k), append(x)

  13. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k) • boolean insert(x, k), append(x) • boolean remove(k)

  14. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k) • boolean insert(x, k), append(x) • boolean remove(k) • int find(x) / indexOf(x)

  15. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k) • boolean insert(x, k), append(x) • boolean remove(k) • int find(x) / indexOf(x) • Object next()

  16. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k) • boolean insert(x, k), append(x) • boolean remove(k) • int find(x) / indexOf(x) • Object next() • Object previous()

  17. Typical List Operations • void printList() A 0 A 1 A 2 A 3 A 4 A 5 A 6 • void makeEmpty() • int size() • Object findKth(k) / get(k) • boolean insert(x, k), append(x) • boolean remove(k) • int find(x) / indexOf(x) • Object next() • Object previous() • void removeCurrent()

  18. Array Lists • Just a thin layer wrapping an array. public class SimpleArrayList implements List{ public static final int DEFAULT_CAPACITY = 10; private int theSize; private Integer[] theItems; public SimpleArrayList() { theItems = new Integer[DEFAULT_CAPACITY]; } } 1 7 3 5 2 1 3

  19. Running Time for Array List Operations 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 Operation Number of Steps printList find(x) findKth(k) insert(x,k) remove(x)

  20. Running Time for Array List Operations 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 Operation Number of Steps N printList N find(x) findKth(k) insert(x,k) remove(x)

  21. Running Time for Array List Operations 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 Operation Number of Steps N printList N find(x) 1 findKth(k) insert(x,k) remove(x)

  22. Array List: Insert/Remove 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 insert(x,k) remove(x)

  23. Array List: Insert/Remove 1 7 3 5 2 1 3 5 N=7 0 1 2 3 4 5 6 7 8 9 insert(5,7): 1 step insert(x,k) remove(x)

  24. Array List: Insert/Remove 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 insert(5,7): 1 step best case remove(7): 1 step insert(x,k) remove(x)

  25. Array List: Insert/Remove 7 moves 5 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 insert(5,7): 1 step best case remove(7): 1 step insert(5,0): 7 steps worst case insert(x,k) remove(x)

  26. Array List: Insert/Remove 7 moves 5 1 7 3 5 2 1 3 N=7 0 1 2 3 4 5 6 7 8 9 insert(5,7): 1 step best case remove(7): 1 step insert(5,0): 7 steps worst case remove(0): O(N) N insert(x,k) N remove(x)

  27. Expanding Array Lists • What if we are running out of space during append/ insert • first copy all elements into a new array of sufficient size newCapacity = arr.length * 2; Integer[ ] old = theItems; theItems = new Integer[newCapacity]; for( int i = 0; i < size( ); i++ ) 1 7 3 5 2 theItems[ i ] = old[ i ]; 1 3 2 7 5 0 1 2 3 4 5 6 7 8 9

  28. Simple Linked Lists • Series of Nodes. Each Node contains: • A reference to the data object it contains. • A reference to the next node in the List. public class Node { public Integer data; public Node next; public Node(Integer d, Node n) { data = d; next = n; } } A 0 A 1 A 2 A 3 null

  29. Running Time for Simple Linked List Operations 42 23 5 9 null printList find(x) findKth(k) next()

  30. Running Time for Simple Linked List Operations 42 23 5 9 null N printList find(x) findKth(k) next()

  31. Running Time for Simple Linked List Operations 42 23 5 9 null N printList N find(x) findKth(k) next()

  32. Running Time for Simple Linked List Operations 42 23 5 9 null N printList N find(x) k findKth(k) next()

  33. Running Time for Simple Linked List Operations 42 23 5 9 null N printList N find(x) k findKth(k) 1 next() In many applications we can use next() instead of findKth(k). (for every element in the list do… / filter the list … )

  34. Simple Linked List Removal 42 23 5 9 null findKth(k) k next() 1 insert(x,k) remove(k)

  35. Simple Linked List Removal 42 23 5 9 null remove(2) findKth(k) k next() 1 insert(x,k) search time + 1 remove(k)

  36. Simple Linked List Removal 42 23 9 null remove(2) findKth(k) k next() 1 insert(x,k) search time + 1 remove(k)

  37. Simple Linked List Insertion 42 23 9 null insert(x,k)

  38. Simple Linked List Insertion 42 23 9 null insert(5,2) insert(x,k)

  39. Simple Linked List Insertion 42 23 9 null insert(5,2) insert(x,k)

  40. Simple Linked List Insertion 42 23 5 9 null insert(5,2) insert(x,k)

  41. Simple Linked List Insertion 42 23 5 9 null insert(5,2) insert(x,k)

  42. Simple Linked List Insertion 42 23 5 9 null insert(5,2) search time +1 insert(x,k)

  43. Simple Linked List Insertion 42 23 5 9 null insert(5,2) search time +1 insert(x,k) Inserting in position 0? Inserting in position N-1?

  44. Simple Linked List Insertion 42 23 5 9 null insert(5,2) search time +1 insert(x,k) Inserting in position 0? Inserting in position N-1? Linked list should remember the first and last object.

  45. Doubly Linked Lists • Also maintain reference to previous node in the list. • Speeds up append at end of list. private class Node { public Integer data; public Node next; public Node prev; public Node(Integer d, Node n, Node p) { data = d; next = n; prev = n; } } A 0 A 1 A 2 A 3

  46. Doubly Linked List with Sentinel Nodes • header node, tail node • make implementation of next / previous easier . • Remove other special cases 
 (e.g. removing first node/last node) head A 0 A 1 A 2 tail A 3

  47. Empty Doubly Linked List with Sentinel Nodes head tail

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