forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly.go
144 lines (116 loc) · 2.62 KB
/
doubly.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
package linkedlist
import "fmt"
// Doubly structure with just the Head Node
// We call it `Doubly` to make it easier to
// understand when calling this in peoples
// own local code to understand and experiment
// with this easily.
// For example, we can use gotools `go get` command to get
// this repository cloned inside the
// $GOPATH/src/github.com/TheAlgorithms/Go (you can do this
// manually as well) and use the import statement as follows:
//
// `import "github.com/TheAlgorithms/Go/structure/linkedlist"`
//
// and call linkedlist.Doubly to create a new doubly linked list.
type Doubly[T any] struct {
Head *Node[T]
}
func NewDoubly[T any]() *Doubly[T] {
return &Doubly[T]{}
}
// AddAtBeg Add a node to the beginning of the linkedlist
func (ll *Doubly[T]) AddAtBeg(val T) {
n := NewNode(val)
n.Next = ll.Head
if ll.Head != nil {
ll.Head.Prev = n
}
ll.Head = n
}
// AddAtEnd Add a node at the end of the linkedlist
func (ll *Doubly[T]) AddAtEnd(val T) {
n := NewNode(val)
if ll.Head == nil {
ll.Head = n
return
}
cur := ll.Head
for ; cur.Next != nil; cur = cur.Next {
}
cur.Next = n
n.Prev = cur
}
// DelAtBeg Delete the node at the beginning of the linkedlist
func (ll *Doubly[T]) DelAtBeg() (T, bool) {
if ll.Head == nil {
var r T
return r, false
}
cur := ll.Head
ll.Head = cur.Next
if ll.Head != nil {
ll.Head.Prev = nil
}
return cur.Val, true
}
// DetAtEnd Delete a node at the end of the linkedlist
func (ll *Doubly[T]) DelAtEnd() (T, bool) {
// no item
if ll.Head == nil {
var r T
return r, false
}
// only one item
if ll.Head.Next == nil {
return ll.DelAtBeg()
}
// more than one, go to second last
cur := ll.Head
for ; cur.Next.Next != nil; cur = cur.Next {
}
retval := cur.Next.Val
cur.Next = nil
return retval, true
}
// Count Number of nodes in the linkedlist
func (ll *Doubly[T]) Count() int {
var ctr int = 0
for cur := ll.Head; cur != nil; cur = cur.Next {
ctr += 1
}
return ctr
}
// Reverse Reverse the order of the linkedlist
func (ll *Doubly[T]) Reverse() {
var Prev, Next *Node[T]
cur := ll.Head
for cur != nil {
Next = cur.Next
cur.Next = Prev
cur.Prev = Next
Prev = cur
cur = Next
}
ll.Head = Prev
}
// Display display the linked list
func (ll *Doubly[T]) Display() {
for cur := ll.Head; cur != nil; cur = cur.Next {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}
// DisplayReverse Display the linkedlist in reverse order
func (ll *Doubly[T]) DisplayReverse() {
if ll.Head == nil {
return
}
var cur *Node[T]
for cur = ll.Head; cur.Next != nil; cur = cur.Next {
}
for ; cur != nil; cur = cur.Prev {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}