-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.go
More file actions
207 lines (179 loc) · 3.98 KB
/
Copy pathlinkedlist.go
File metadata and controls
207 lines (179 loc) · 3.98 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
)
// define node struct
type Node struct {
prev, next *Node
value interface{}
}
// define linked list struct
type List struct {
head *Node
tail *Node
len int
}
// create list
func createList() List {
l := List{}
fmt.Println("Create linkedlist Success!")
return l
}
// get lengh
func (l *List) lengh() int {
return l.len
}
//insert node by index
func (l *List) insertByIndex(index int, n *Node) {
p := l.head.next
for ; p != nil && index > 1; p = p.next {
index--
}
n.prev = p.prev
p.prev = n
n.next = n.prev.next
n.prev.next = n
l.len++
fmt.Println("Insert node success!")
}
// append node at tail
func (l *List) append(v interface{}) {
if l.tail == nil && l.head == nil {
l.head = new(Node)
l.tail = l.head
}
l.tail.next = new(Node)
l.tail.next.prev = l.tail
l.tail = l.tail.next
l.tail.value = v
l.len++
fmt.Printf("Push value: %v success!\n", v)
}
// pop node at tail
func (l *List) pop() {
fmt.Printf("Pop value : %v success!\n", l.tail.value)
l.tail = l.tail.prev
l.tail.next = nil
l.len--
}
//remove node by index
func (l *List) removeByIndex(index int) {
p := l.head.next
fmt.Printf("Remove index is: %d\n", index)
if l.len < index || index <= 0 {
fmt.Println("Index out of range!")
return
} else if index == 1 {
l.head.next = p.next
p.next.prev = l.head
l.len--
fmt.Println("Remove first node success!")
} else if index == l.len {
l.pop()
} else {
for ; p != nil && index >= 1; p = p.next {
if index == 1 {
p.prev.next = p.next
p.next.prev = p.prev
p.next = nil
p.prev = nil
}
index--
}
l.len--
}
}
//contains value
func (l *List) contains(v interface{}) int {
count := 0
if v == nil {
fmt.Println("Your input is nil!")
return 0
}
for p := l.head.next; p != nil; p = p.next {
if p.value == v {
count++
}
}
if count > 0 {
fmt.Printf("This linkedlist contains: %v %d items\n", v, count)
} else {
fmt.Printf("This linkedlist not contains: %v\n", v)
}
return count
}
//update linkedlist value by index
func (l *List) updateValueByIndex(index int, v interface{}) {
p := l.head.next
if l.len < index || index <= 0 {
fmt.Println("Index out of range!")
return
}
for ; p != nil && index >= 1; p = p.next {
if index == 1 {
fmt.Printf("Update value : %v to %v \n", p.value, v)
p.value = v
}
index--
}
}
//is empty
func (l *List) isEmpty() bool {
return l.len == 0
}
//print node info
func (l *List) printNodeInfo(at *Node) {
if at != nil {
fmt.Printf("Node Value is: %v\n", at.value)
} else {
fmt.Println("Node is nil")
}
}
// print linkedlist info
func (l *List) printListInfo() {
if l == nil {
fmt.Println("List is nil!")
}
for p := l.head.next; p != nil; p = p.next {
l.printNodeInfo(p)
}
fmt.Printf("Node len is: %d", l.len)
}
func main() {
// create List
linkedList := createList()
fmt.Printf("\n----------------------------------\n")
//append value
linkedList.append("a")
linkedList.append("b")
linkedList.append("c")
linkedList.printListInfo()
fmt.Printf("\n----------------------------------\n")
// insert node by index
newNode := new(Node)
newNode.value = "k"
linkedList.insertByIndex(2, newNode)
linkedList.printListInfo()
fmt.Printf("\n----------------------------------\n")
//pop node
linkedList.pop()
linkedList.printListInfo()
fmt.Printf("\n----------------------------------\n")
//remove node by index
linkedList.removeByIndex(2)
linkedList.printListInfo()
fmt.Printf("\n----------------------------------\n")
//contains value
linkedList.contains("b")
fmt.Printf("\n----------------------------------\n")
//update value
linkedList.updateValueByIndex(2, "x")
linkedList.printListInfo()
fmt.Printf("\n----------------------------------\n")
//is empty
fmt.Printf("This linkedlist is empty: %v", linkedList.isEmpty())
fmt.Printf("\n----------------------------------\n")
//get linedlist length
fmt.Printf("This linkedlist length is: %d", linkedList.lengh())
fmt.Printf("\n----------------------------------\n")
}