forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unordered_linked_list.go
155 lines (122 loc) · 3.46 KB
/
unordered_linked_list.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Lists - Unordered linear list
*
* Implementation of a sequential list where elements are not ordered
*
* Go Playground link: https://play.golang.org/p/3uFOquVlTD8
*/
package main
import "fmt"
var maxSize = 50
// Structure that will be stored in each position of the list
type Record struct {
value int
// Other fields can be added here
}
// Structure that holds an array of Records and the number of elements in the array
type List struct {
recordArray []Record
numberOfElements int
}
// Creates a new list
func createList() List {
list := List{
recordArray: make([]Record, maxSize),
numberOfElements: 0,
}
return list
}
// Resets the list's element counter
func initialize(list *List) {
list.numberOfElements = 0
}
// Retrieves the number of elements in the list
func size(list *List) int {
return list.numberOfElements
}
// Prints the values of the elements in the list
func show(list *List) {
for i := 0; i < list.numberOfElements; i++ {
fmt.Printf("%v ", list.recordArray[i].value)
}
fmt.Println()
}
// Performs a sequential search in the list, iterating through each item
func sequentialSearch(list *List, value int) int {
i := 0
for i < list.numberOfElements {
if value == list.recordArray[i].value {
return i
}
i++
}
return -1
}
// Performs a sentinel search in the list, iterating through each item
func sentinelSearch(list *List, value int) int {
i := 0
list.recordArray[list.numberOfElements].value = value
for value != list.recordArray[i].value {
i++
}
if i == list.numberOfElements {
return -1
}
return i
}
// Inserts elements into the list at a specific position and moves all other elements to the right
func insertElement(list *List, record Record, position int) bool {
if (list.numberOfElements == maxSize) || position < 0 || position > list.numberOfElements {
return false
}
for j := list.numberOfElements; j > position; j-- {
list.recordArray[j] = list.recordArray[j-1]
}
list.recordArray[position] = record
list.numberOfElements++
return true
}
// Deletes an element from the list
func deleteElement(list *List, value int) bool {
position := sequentialSearch(list, value)
if position == -1 {
return false
}
for i := position; i < list.numberOfElements-1; i++ {
list.recordArray[i] = list.recordArray[i+1]
}
list.numberOfElements--
return true
}
func main() {
list := createList()
initialize(&list)
fmt.Println("Inserting values into the list...")
insertElement(&list, Record{value: 20}, 0)
insertElement(&list, Record{value: 10}, 0)
insertElement(&list, Record{value: 70}, 0)
insertElement(&list, Record{value: 30}, 0)
insertElement(&list, Record{value: 60}, 0)
insertElement(&list, Record{value: 90}, 0)
insertElement(&list, Record{value: 80}, 0)
insertElement(&list, Record{value: 15}, 0)
insertElement(&list, Record{value: 1}, 0)
fmt.Println()
fmt.Println("Printing the list...")
show(&list)
fmt.Println("Size of the list:", size(&list))
fmt.Println()
fmt.Println("Deleting element 80 from the list...")
deleteElement(&list, 80)
fmt.Println()
fmt.Println("Printing the list...")
show(&list)
fmt.Println("Size of the list:", size(&list))
fmt.Println()
fmt.Println("Searching for values in the list:")
fmt.Println()
fmt.Println("Searching for the position of number 15:")
fmt.Printf("Position of number 15: %v \n\n", sequentialSearch(&list, 15))
fmt.Println("Searching for the position of value 100:")
fmt.Printf("Position of number 100: %v \n\n", sequentialSearch(&list, 100))
}