-
Notifications
You must be signed in to change notification settings - Fork 1
/
llist-noduplicate.go
98 lines (82 loc) · 1.39 KB
/
llist-noduplicate.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
package main
import "fmt"
var m map[string]int
var count int
var ok bool
var elem int
type List struct {
head *Node
tail *Node
}
type Node struct {
value string
prev *Node
next *Node
}
func (l *List) Show() {
for l.head != nil {
fmt.Println(l.head.value)
l.head = l.head.next
}
fmt.Println()
}
func (l *List) Insert_Back(value string) {
n := &Node{
value: value,
prev: l.tail,
}
if l.tail != nil {
l.tail.next = n
}
l.tail = n
if l.head == nil {
l.head = n
}
}
func (l *List) Size() int {
count = 0
for l.head != nil {
l.head = l.head.next
count = count + 1
}
return count
}
func (l *List) NoDuplicates() *List {
m := make(map[string]int)
for l.tail != nil {
elem, ok = m[l.tail.value]
if ok == false {
m[l.tail.value] = 1
l.tail = l.tail.prev
}
if ok == true && l.tail.prev != nil {
m[l.tail.value] = elem + 1
cur := l.tail
l.tail = l.tail.prev
l.tail.next = cur.next
}
if ok == true && l.tail.prev == nil {
m[l.tail.value] = elem + 1
l.tail = l.tail.next
l.tail.prev = nil
l.head = l.tail
break
}
}
fmt.Println(m)
return l
}
func main() {
l := List{}
l.Insert_Back("15")
l.Insert_Back("15")
l.Insert_Back("10")
l.Insert_Back("10")
l.Insert_Back("22")
l.Insert_Back("10")
fmt.Printf("head: %v\n", l.head)
fmt.Printf("tail: %v\n", l.tail)
fmt.Println("No Duplicate list:")
new := l.NoDuplicates()
new.Show()
}