1 / 9

Linked Lists

Linked Lists. Single Linked Lists Double Linked Lists. Single-Direction Linked List. Data Nodes “linked” into a “list. Start. content. Foo. Boo. Goo. Next Node Address. Current. Single-Direction Linked List. Can add and remove nodes from center and either end

Download Presentation

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 Single Linked Lists Double Linked Lists

  2. Single-Direction Linked List Data Nodes “linked” into a “list Start content Foo Boo Goo Next Node Address Current

  3. Single-Direction Linked List • Can add and remove nodes from center and either end • Here “boo” has been deleted and “Hoo” has been added Start content Foo Boo Goo Hoo Next Node Address

  4. Single-Direction Linked List - But deletion is tricky and takes considerable time as need pointer to previous node to be able to change it’s pointer to the next node (“foo” must point to “goo” after deletion. - To find previous node, must start at beginning and linearly search through list until find current. Current Start Foo Boo Goo

  5. Double-Direction Linked List • A double-direction linked list has both next and previous points. Deletion and insertion are much faster but each node takes up more space. Current Start Null Pointer Foo Boo Goo Next Previous

  6. Node Implementation • Public Class DoubleLinkedListNode • Private nodeValuep As Object • Something of type object can contain integers, strings, or anything • Private nextNodeP As DoubleLinkedListNode • Each node contains a pointer to the next node, which is also an instanceof the DoubleLinkedListNode class. This is containment, not inheritance.Thie project uses object-based, not object oriented techniques. • Private previousNodeP As DoubleLinkedListNode • Public Property nextNode() As DoubleLinkedListNode • … • End Property • Public Property previousNode() As DoubleLinkedListNode • … • End Property • Public Property nodeValue() • … • End Property • End Class

  7. DoubleLinkedList Class - Move • Public Class DoubleLinkedList • Protected currentNode As DoubleLinkedListNode • Protected firstNode As DoubleLinkedListNode • Protected lastNode As DoubleLinkedListNode • Public Function moveNext() As Object • End Function • MovePrevious changes the currentNode pointer to point to the previous node and returns the value stored in the new current node. • Public Function movePrevious() As Object • If currentNode Is Nothing OrElse currentNode.previousNode Is Nothing Then • Return Nothing • Else • currentNode = currentNode.previousNode • Return currentNode.nodeValue • End If • End Function • ….

  8. Main Insert Method • Public Sub insertAfterCurrent(ByVal nodeValue As Object) • Several conditions: • List is empty, • current node pointing to last node in list • Current node pointing to a node in the middle of the list • If currentNode Is Nothing Then • insertFirstNode(nodeValue) • ElseIf currentNode.nextNode Is Nothing Then • insertEndNode(nodeValue) • ElseIf Not (currentNode.nextNode Is Nothing And currentNode.previousNode Is Nothing) Then • insertMiddleNode(nodeValue) • End If • End Sub

  9. Insert First Node in List • Private Sub insertFirstNode(ByVal nodeValue As Object) • Dim n As New DoubleLinkedListNode() • Creates new node • n.nodeValue = nodeValue ‘sets value • n.nextNode = Nothing ‘nothing to point to • n.previousNode = Nothing • currentNode = n • firstNode = n • lastNode = n • These three pointers in the doubleLinkedListClass track various parts ofthe double linked list. When there is only one node in the list, all three point to it. • End Sub

More Related