-
Notifications
You must be signed in to change notification settings - Fork 18
/
doubly-linked-list.go
156 lines (147 loc) · 3.47 KB
/
doubly-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
156
/*
filename: doubly-linked-list.go
author: Lex Sheehan
copyright: Lex Sheehan LLC
license: GPL
status: published
comments: http://l3x.github.io/golang-code-examples/2014/07/23/doubly-linked-list.html
*/
package main
import (
"fmt"
"errors"
"strings"
)
type Value struct {
Name string
MilesAway int
}
type Node struct {
Value // Embedded struct
next, prev *Node
}
type List struct {
head, tail *Node
}
func (l *List) First() *Node {
return l.head
}
func (n *Node) Next() *Node {
return n.next
}
func (n *Node) Prev() *Node {
return n.prev
}
// Create new node with value
func (l *List) Push(v Value) *List {
n := &Node{Value: v}
if l.head == nil {
l.head = n // First node
} else {
l.tail.next = n // Add after prev last node
n.prev = l.tail // Link back to prev last node
}
l.tail = n // reset tail to newly added node
return l
}
func (l *List) Find(name string) *Node {
found := false
var ret *Node = nil
for n := l.First(); n != nil && !found; n = n.Next() {
if n.Value.Name == name {
found = true
ret = n
}
}
return ret
}
func (l *List) Delete(name string) bool {
success := false
node2del := l.Find(name)
if node2del != nil {
fmt.Println("Delete - FOUND: ", name)
prev_node := node2del.prev
next_node := node2del.next
// Remove this node
prev_node.next = node2del.next
next_node.prev = node2del.prev
success = true
}
return success
}
var errEmpty = errors.New("ERROR - List is empty")
// Pop last item from list
func (l *List) Pop() (v Value, err error) {
if l.tail == nil {
err = errEmpty
} else {
v = l.tail.Value
l.tail = l.tail.prev
if l.tail == nil {
l.head = nil
}
}
return v, err
}
func main() {
dashes := strings.Repeat("-", 50)
l := new(List) // Create Doubly Linked List
l.Push(Value{Name: "Atlanta", MilesAway: 0})
l.Push(Value{Name: "Las Vegas", MilesAway: 1961})
l.Push(Value{Name: "New York", MilesAway: 881})
processed := make(map[*Node]bool)
fmt.Println("First time through list...")
for n := l.First(); n != nil; n = n.Next() {
fmt.Printf("%v\n", n.Value)
if processed[n] {
fmt.Printf("%s as been processed\n", n.Value)
}
processed[n] = true
}
fmt.Println(dashes)
fmt.Println("Second time through list...")
for n := l.First(); n != nil; n = n.Next() {
fmt.Printf("%v", n.Value)
if processed[n] {
fmt.Println(" has been processed")
} else { fmt.Println() }
processed[n] = true
}
fmt.Println(dashes)
var found_node *Node
city_to_find := "New York"
found_node = l.Find(city_to_find)
if found_node == nil {
fmt.Printf("NOT FOUND: %v\n", city_to_find)
} else {
fmt.Printf("FOUND: %v\n", city_to_find)
}
city_to_find = "Chicago"
found_node = l.Find(city_to_find)
if found_node == nil {
fmt.Printf("NOT FOUND: %v\n", city_to_find)
} else {
fmt.Printf("FOUND: %v\n", city_to_find)
}
fmt.Println(dashes)
city_to_remove := "Las Vegas"
successfully_removed_city := l.Delete(city_to_remove)
if successfully_removed_city {
fmt.Printf("REMOVED: %v\n", city_to_remove)
} else {
fmt.Printf("DID NOT REMOVE: %v\n", city_to_remove)
}
city_to_remove = "Chicago"
successfully_removed_city = l.Delete(city_to_remove)
if successfully_removed_city {
fmt.Printf("REMOVED: %v\n", city_to_remove)
} else {
fmt.Printf("DID NOT REMOVE: %v\n", city_to_remove)
}
fmt.Println(dashes)
fmt.Println("* Pop each value off list...")
for v, err := l.Pop(); err == nil; v, err = l.Pop() {
fmt.Printf("%v\n", v)
}
fmt.Println(l.Pop()) // Generate error - attempt to pop from empty list
}