You are on page 1of 3

Module Module1

Const NullPointer = -1 '// NullPointer should be set to -1 if using array element


with index 0
Structure ListNode '// Declare record type to store data and pointer
Dim Data As String
Dim Pointer As Integer
End Structure
Dim StartPointer As Integer
Dim FreeListPtr As Integer
Dim List(6) As ListNode
Dim NextFreeNodeAddress, CurrentPointer, PreviousPointer As Integer

Sub Main()
Call CreateLinkedList() 'call procedure CreateLinedList to create linked list in
memory
Call AddToList("Zebra")
Call AddToList("Ant")
Call AddToList("Elephant")
Call AddToList("Bee")
Call AddToList("Tiger")
Call AddToList("Lion")
Call AddToList("Cheeta")
Call AddToList("Horse")

Console.WriteLine("....................................................................."
)
Console.WriteLine("Print elements in ordered Linked list ")
Console.WriteLine(List(StartPointer).Data)
Console.WriteLine(List(List(StartPointer).Pointer).Data)
Console.WriteLine(List(List(List(StartPointer).Pointer).Pointer).Data)

Console.WriteLine(List(List(List(List(StartPointer).Pointer).Pointer).Pointer).Data)

Console.WriteLine(List(List(List(List(List(StartPointer).Pointer).Pointer).Pointer).Point
er).Data)

Console.WriteLine(List(List(List(List(List(List(StartPointer).Pointer).Pointer).Pointer).
Pointer).Pointer).Data)

Console.WriteLine(List(List(List(List(List(List(List(StartPointer).Pointer).Pointer).Poin
ter).Pointer).Pointer).Pointer).Data)

Console.WriteLine("....................................................................."
)

Console.ReadKey()

End Sub

Sub CreateLinkedList()
Dim Index As Integer
StartPointer = NullPointer '// set start pointer
FreeListPtr = 0 '// set starting position of free list
For Index = 0 To 5 '// link all nodes to make free list
List(Index).Pointer = Index + 1
Next
List(6).Pointer = NullPointer '// last node of free list
End Sub

1
Sub AddToList(ByVal NewItem As String)

If FreeListPtr <> NullPointer Then ''place item at first node of free list if
free space is available
List(FreeListPtr).Data = NewItem
NextFreeNodeAddress = List(FreeListPtr).Pointer
' search data linked list to get position where to place the new item in it
CurrentPointer = StartPointer ' use this pointer variable to go through each
node

While CurrentPointer <> NullPointer AndAlso List(CurrentPointer).Data <


NewItem
PreviousPointer = CurrentPointer
CurrentPointer = List(CurrentPointer).Pointer
End While

If CurrentPointer = StartPointer Then ''if new item is to be added at start


of data linked list with or without existing nodes
List(FreeListPtr).Pointer = StartPointer 'first node of free list points
to start pointer in data link list
StartPointer = FreeListPtr '' start pointer points to first node in free
list
Else
'if new item is to be added between existing nodes or after all nodes in
existing linked list
List(FreeListPtr).Pointer = CurrentPointer ' first node of free linked
list points to the node whose data is just greater than the first node's data or points
to null value if first node is to be inserted as the last node
List(PreviousPointer).Pointer = FreeListPtr ' the node whose data is just
less thn the first node's data points to the first node in free linked list

End If
FreeListPtr = NextFreeNodeAddress ' set free list pointer to point to new
free node
Console.WriteLine(NewItem & " added to linked list") ' confirm new node added
to linked list
Else
'linked list is full..no more nodes can be added
Console.WriteLine("Sorry..." & NewItem & " can not be added as there is no
free space left in the data linked list")

End If

End Sub

End Module

2
Screen shot

You might also like