Computer Science(9608) notes and Resources
Linked
List: A linked list is a data structure that provides links between data items.
The data items are in an arranged order in the linked list.
Implementation of Linked List in Visual basic.
Module Module1
Dim
Itemlist(4) As ListNode
Dim
headpointer, freepointer As Integer
Dim
nextFreeAddress As Integer
Dim
currentPointer, previousPointer As Integer
Structure ListNode
Dim
name As String
Dim
pointer As Integer
End Structure
Sub Main()
createLinkedList()
additem("apple")
additem("orange")
additem("grapes")
additem("banana")
Console.WriteLine(" Printing items of linked list: ")
Console.WriteLine(Itemlist(headpointer).name)
Console.WriteLine(Itemlist(Itemlist(headpointer).pointer).name)
Console.WriteLine(Itemlist(Itemlist(Itemlist(headpointer).pointer).pointer).name)
Console.WriteLine(Itemlist(Itemlist(Itemlist(Itemlist(headpointer).pointer).pointer).pointer).name)
Console.WriteLine(" Removing items: ")
removeitem("orange")
removeitem("apple")
Console.WriteLine(" Adding
items: ")
additem("coconut")
additem("potato")
Console.WriteLine(" Printing items of linked list: ")
Console.WriteLine(Itemlist(headpointer).name)
Console.WriteLine(Itemlist(Itemlist(headpointer).pointer).name)
Console.WriteLine(Itemlist(Itemlist(Itemlist(headpointer).pointer).pointer).name)
Console.WriteLine(Itemlist(Itemlist(Itemlist(Itemlist(headpointer).pointer).pointer).pointer).name)
Console.WriteLine()
Console.Write("
Type an item be searched: ")
Dim
itemSearch As String
itemSearch = Console.ReadLine()
searchItem(itemSearch)
Console.ReadLine()
End Sub
Sub
createLinkedList()
For
index = 1 To 4
Itemlist(index).pointer = index + 1
Next
Itemlist(4).pointer = 0
freepointer = 1
headpointer = 0
End Sub
Sub
removeitem(ByVal newItem As String)
Dim
found As Boolean
= False
currentPointer = headpointer
If
Itemlist(currentPointer).name = newItem Then
headpointer =
Itemlist(currentPointer).pointer
Itemlist(currentPointer).pointer =
freepointer
freepointer = currentPointer
found = True
Else
While
currentPointer <> 0 And found = False
previousPointer =
currentPointer
currentPointer =
Itemlist(currentPointer).pointer
If
Itemlist(currentPointer).name = newItem Then
Itemlist(previousPointer).pointer = Itemlist(currentPointer).pointer
Itemlist(currentPointer).pointer = freepointer
freepointer =
currentPointer
found = True
End
If
End
While
End If
If
found = False Then
Console.WriteLine(" Item to be deleted not found in linked list!")
End If
End Sub
Sub
searchItem(ByVal newItem As String)
Dim
found As Boolean
= False
currentPointer = headpointer
While
currentPointer <> 0 And found = False
If
Itemlist(currentPointer).name = newItem Then
Console.WriteLine(newItem &
" is found at address: " & CStr(currentPointer) & "
and it points to the node at adress: " & CStr(Itemlist(currentPointer).pointer))
found = True
End
If
currentPointer =
Itemlist(currentPointer).pointer
End While
If
found = False Then
Console.WriteLine(" Item not found in linked List! ")
End If
End Sub
Sub
additem(ByVal newItem As
String)
If
freepointer <> 0 Then
Itemlist(freepointer).name =
newItem
nextFreeAddress =
Itemlist(freepointer).pointer
currentPointer = headpointer
While
Itemlist(currentPointer).name < newItem And
currentPointer <> 0
previousPointer =
currentPointer
currentPointer =
Itemlist(currentPointer).pointer
End
While
If
currentPointer = headpointer Then
Itemlist(freepointer).pointer =
headpointer
headpointer = freepointer
Else
Itemlist(freepointer).pointer =
currentPointer
Itemlist(previousPointer).pointer = freepointer
End
If
freepointer = nextFreeAddress
Else
Console.WriteLine(" There is no free space! ")
End If
End Sub
End Module
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )