1 / 66

Linked Lists

Linked Lists. A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of contiguity of memory. Singly-linked Lists. A list in which there is a preferred direction. A minimally linked list.

deniser
Télécharger la présentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists • A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of contiguity of memory.

  2. Singly-linked Lists • A list in which there is a preferred direction. • A minimally linked list. • The item before has a pointer to the item after.

  3. Singly-linked List • Implement this structure using objects and references.

  4. 4 11 1 7 Singly-linked List head

  5. 4 11 1 7 Singly-linked List head

  6. Singly-linked List class ListElement { Object datum ; ListElement nextElement ; . . . } datum nextElement

  7. Singly-linked List ListElement newItem = new ListElement(new Integer(4)) ; ListElement p = null ; ListElement c = head ; while ((c != null) && !c.datum.lessThan(newItem)) { p = c ; c = c.nextElement ; } newItem.nextElement = c ; p.nextElement = newItem ;

  8. 4 11 1 7 Singly-linked List newElement p c head

  9. Analysing Singly-linked List • Accessing a given location is O(n). • Setting a given location is O(n). • Inserting a new item is O(n). • Deleting an item is O(n) • Assuming both a head at a tail pointer, accessing, inserting or deleting can be O(1).

  10. Doubly-linked Lists • A list without a preferred direction. • The links are bidirectional: implement this with a link in both directions.

  11. Doubly-linked List head tail

  12. Doubly-linked List head tail

  13. Doubly-linked List class ListElement { Object datum ; ListElement nextElement ; ListElement previousElement ; . . . } previousElement datum nextElement

  14. Doubly-linked List ListElement newItem = new ListElement(new Integer(4)) ; ListElement c = head ; while ((c.next != null) && !c.next.datum.lessThan(newItem)) { c = c.nextElement ; } newItem.nextElement = c.nextElement ; newItem.previousElement = c ; c.nextElement.previousElement = newItem ; c.nextElement = newItem ; Spot the deliberate mistake. What needs to be done to correct this?

  15. Doubly-linked List head c newItem tail

  16. Doubly-linked List • Performance of doubly-linked list is formally similar to singly linked list. • The complexity of managing two pointers makes things very much easier since we only ever need a single pointer into the list. • Iterators and editing are made easy.

  17. Doubly-linked List • Usually find the List type in a package is a doubly-linked list. • Singly-linked list are used in other data structures.

  18. Stack and Queue • Familiar with the abstractions of stack and queue.

  19. Stack push pop isEmpty top

  20. Queue isEmpty remove insert

  21. Implementing Stack tos

  22. Implementing Queue head tail

  23. Multi-lists • Multi-lists are essentially the technique of embedding multiple lists into a single data structure. • A multi-list has more than one next pointer, like a doubly linked list, but the pointers create separate lists.

  24. Multi-lists head

  25. Multi-lists head

  26. Multi-lists (Not Required) head head

  27. Linked Structures • A doubly-linked list or multi-list is a data structure with multiple pointers in each node. • In a doubly-linked list the two pointers create bi-directional links • In a multi-list the pointers used to make multiple link routes through the data.

  28. Linked Structures • What else can we do with multiple links? • Make them point at different data: create Trees (and Graphs).

  29. Trees degree root children parent Level 1 Level 2 node Level 3 leaf node height = depth = 3

  30. Trees • Crucial properties of Trees: • Links only go down from parent to child. • Each node has one and only one parent (except root which has no parent). • There are no links up the data structure; no child to parent links. • There are no sibling links; no links between nodes at the same level.

  31. Trees • If we relax the restrictions, it is not a Tree, it is a Graph. • A Tree is a directed, acyclic Graph that is single parent.

  32. Trees • Binary Trees have degree 2. • Red–Black Trees and AVL Trees are Binary Trees with special extra properties; they are balanced. • B-Trees, B+-Trees, B*-Trees are more complicated Trees with flexible branching factor: these are used very extensively in databases.

  33. Binary Trees • Trees are immensely useful for sorting and searching. • Look at Binary Trees as they are the simplest.

  34. Binary Trees This is a complete binary tree.

  35. Binary Trees • How to insert something in the list? • Need a metric, there must be an order relation defined on the nodes. • The elements are in the tree in a given order; assume ascending order.

  36. Binary Trees • Inserting an element in the Binary Tree involves: • If the tree is empty, insert the element as the root.

  37. Binary Trees • If the tree is not empty: • Start at the root. • For each node decide whether the element is the same as the one at the node or comes before or after it in the defined order. • When the child is a null pointer insert the element.

  38. Binary Tree 37 root 37

  39. Binary Tree 37, 9, 3 root 37 9 3

  40. Binary Trees 37, 9, 3, 68, 14, 54 root 37 9 68 3 14 54

  41. Binary Trees Delete this one

  42. Binary Trees

  43. Binary Trees Delete this one

  44. Binary Trees Assume ascending order.

  45. Binary Trees Delete this one

  46. Binary Trees Assume ascending order.

  47. Binary Tree • In Java: class Unit { public Unit(Object o, Unit l, Unit r) { datum = o ; left = l ; right = r ; } Object datum ; Unit left ; Unit right ; }

  48. Binary Tree • Copying can be done recursively: public Object clone() { return new Unit(datum, (left != null) ? ((Unit)left).clone() : null, (right != null) ? ((Unit)right).clone() : null ) ; }

  49. Binary Tree • Can take a tour around the tree, doing something at each stage: void inOrder (Function f) { if (left != null) { left.inOrder(f) ; } f.execute(this) ; if (right != null) { right.inOrder(f); } }

  50. Binary Tree • Can take a different tour around the tree, doing something at each stage: void preOrder (Function f) { f.execute(this) ; if (left != null) { left.preOrder(f) ; } if (right != null) { right.preOrder(f); } }

More Related