-
Notifications
You must be signed in to change notification settings - Fork 6
/
single_linkedlist.vb
140 lines (119 loc) · 4.11 KB
/
single_linkedlist.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Module Module1
Structure LinkedList
Dim Data As String
Dim Pointer As Integer
End Structure
Dim List1(4) As LinkedList
Dim d As String
Dim Free As Integer = 0
Dim Temp, Temp2 As Integer
Dim Current As Integer
Dim ListStart = 0
Sub Main()
Dim MenuOption As Integer
Call listInitialise()
Do
Console.WriteLine("1. Add item to list.")
Console.WriteLine("2. Delete item from list.")
Console.WriteLine("3. Display items in list.")
Console.WriteLine("4. Search item in List.")
Console.WriteLine("0. Quit.")
Console.WriteLine()
Console.Write("Enter your choice.....")
MenuOption = Console.ReadLine
Select Case MenuOption
Case 1 : Insert()
Case 2 : Delete()
Case 3 : Display()
Case 4 : Search()
End Select
Loop Until MenuOption = 0
End Sub
Sub listInitialise()
Dim i As Integer
For i = 0 To 4
List1(i).Data = ""
List1(i).Pointer = -1
Next
End Sub
Sub Display()
Dim a As Integer
For a = 0 To 4
Console.WriteLine(a & ". " & List1(a).Data & " : " & List1(a).Pointer)
Next
Console.ReadKey()
End Sub
Sub Insert()
Console.Write("Enter item to add.....")
d = Console.ReadLine()
If Free = -1 Then
Console.WriteLine("OverFlow")
Else
If Free = 0 Then
List1(Free).Data = d
ListStart = Free
If List1(Free).Pointer = -1 Then Free = Free + 1 Else Free = List1(Free).Pointer
If Free = 5 Then Free = -1
Else
If List1(ListStart).Data > d Then
List1(Free).Data = d
List1(Free).Pointer = ListStart
ListStart = Free
If List1(Free).Pointer = -1 Then Free = Free + 1 Else Free = List1(Free).Pointer
If Free = 5 Then Free = -1
Else
Current = ListStart
While List1(List1(Current).Pointer).Data < d
Current = List1(Current).Pointer
If List1(Current).Pointer = -1 Then Exit While
End While
List1(Free).Data = d
Temp = List1(Current).Pointer
List1(Current).Pointer = Free
List1(Free).Pointer = Temp
If List1(Free).Pointer = -1 Then Free = Free + 1 Else Free = List1(Free).Pointer
If Free = 5 Then Free = -1
End If
End If
End If
End Sub
Sub Delete()
Dim isDeleted As Boolean = False
d = Console.ReadLine()
If ListStart = -1 Then
Console.WriteLine("UnderFlow")
Else
isDeleted = False
Current = ListStart
While List1(List1(Current).Pointer).Data <> d
Current = List1(Current).Pointer
If List1(Current).Pointer = -1 Then Exit While
End While
If List1(Current).Pointer <> -1 Then
Temp = List1(Current).Pointer
List1(Current).Pointer = List1(List1(Current).Pointer).Pointer
Temp2 = Free
Free = Temp
List1(Free).Pointer = Temp2
isDeleted = True
End If
If isDeleted = False Then
Console.WriteLine("Data not deleted as it is not found.")
Console.ReadKey()
End If
End If
End Sub
Sub Search()
d = Console.ReadLine()
Current = ListStart
While List1(Current).Data <> d Or List1(Current).Pointer <> -1
Current = List1(Current).Pointer
End While
If List1(Current).Data = d Then
Console.WriteLine("Item Found.")
Else
Console.WriteLine("Item Not Found.")
End If
Console.ReadKey()
End Sub
End Module