9
Linked Lists Single Linked Lists Double Linked Lists

Linked Lists

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Linked Lists

Linked Lists

Single Linked Lists

Double Linked Lists

Page 2: Linked Lists

Single-Direction Linked List

Data Nodes “linked” into a “list

FooStartcontent

Next Node Address

Boo Goo

Current

Page 3: Linked Lists

Single-Direction Linked List

• Can add and remove nodes from center and either end– Here “boo” has been deleted and “Hoo” has

been added

FooStartcontent

Next Node Address

Boo Goo Hoo

Page 4: Linked Lists

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.

FooStart

Boo Goo

Current

Page 5: Linked Lists

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.

FooStart

Boo Goo

Current

NextPrevious

NullPointer

Page 6: Linked Lists

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

Page 7: Linked Lists

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 • ….

Page 8: Linked Lists

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

Page 9: Linked Lists

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