-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathDoublyLinkedList.go
156 lines (142 loc) · 2.7 KB
/
DoublyLinkedList.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
156
package DoublyLinkedList
import "cmp"
type Node[T cmp.Ordered] struct {
data T
next *Node[T]
prev *Node[T]
}
type LinkedList[T cmp.Ordered] struct {
head *Node[T]
tail *Node[T]
}
func (list *LinkedList[T]) InsertFirst(i T) {
data := &Node[T]{data: i}
if list.head != nil {
list.head.prev = data
data.next = list.head
}
list.head = data
}
func (list *LinkedList[T]) InsertLast(i T) {
data := &Node[T]{data: i}
if list.head == nil {
list.head = data
list.tail = data
return
}
if list.tail != nil {
list.tail.next = data
data.prev = list.tail
}
list.tail = data
}
func (list *LinkedList[T]) RemoveByValue(i T) bool {
if list.head == nil {
return false
}
if cmp.Compare(list.head.data, i) == 0 {
list.head = list.head.next
list.head.prev = nil
return true
}
if cmp.Compare(list.tail.data, i) == 0 {
list.tail = list.tail.prev
list.tail.next = nil
return true
}
current := list.head
for current.next != nil {
if cmp.Compare(current.next.data, i) == 0 {
if current.next.next != nil {
current.next.next.prev = current
}
current.next = current.next.next
return true
}
current = current.next
}
return false
}
func (list *LinkedList[T]) RemoveByIndex(i int) bool {
if list.head == nil {
return false
}
if i < 0 {
return false
}
if i == 0 {
list.head.prev = nil
list.head = list.head.next
return true
}
current := list.head
for u := 1; u < i; u++ {
if current.next.next == nil {
return false
}
current = current.next
}
if current.next.next != nil {
current.next.next.prev = current
}
current.next = current.next.next
return true
}
func (list *LinkedList[T]) SearchValue(i T) bool {
if list.head == nil {
return false
}
current := list.head
for current != nil {
if cmp.Compare(current.data, i) == 0 {
return true
}
current = current.next
}
return false
}
func (list *LinkedList[T]) GetFirst() (T, bool) {
var t T
if list.head == nil {
return t, false
}
return list.head.data, true
}
func (list *LinkedList[T]) GetLast() (T, bool) {
var t T
if list.head == nil {
return t, false
}
current := list.head
for current.next != nil {
current = current.next
}
return current.data, true
}
func (list *LinkedList[T]) GetSize() int {
count := 0
current := list.head
for current != nil {
count += 1
current = current.next
}
return count
}
func (list *LinkedList[T]) GetItemsFromStart() []T {
var items []T
current := list.head
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}
func (list *LinkedList[T]) GetItemsFromEnd() []T {
var items []T
current := list.tail
for current != nil {
items = append(items, current.data)
current = current.prev
}
return items
}