-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularDoublyLinkedList.go
172 lines (151 loc) · 3.99 KB
/
circularDoublyLinkedList.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import "fmt"
type Node struct {
value int
prev *Node
next *Node
}
type CircularDoublyLinkedList struct {
head *Node
tail *Node
length int
}
// Add a value to end of the list
func (cicularDoublyLinkedList *CircularDoublyLinkedList) append(value int) {
newNode := Node{}
newNode.value = value
if cicularDoublyLinkedList.head == nil {
cicularDoublyLinkedList.head = &newNode
cicularDoublyLinkedList.tail = &newNode
cicularDoublyLinkedList.length++
return
}
newNode.next = cicularDoublyLinkedList.head
newNode.prev = cicularDoublyLinkedList.tail
cicularDoublyLinkedList.head.prev = &newNode
cicularDoublyLinkedList.tail.next = &newNode
cicularDoublyLinkedList.tail = &newNode
cicularDoublyLinkedList.length++
return
}
// Add a value to beginning of the list
func (cicularDoublyLinkedList *CircularDoublyLinkedList) prepend(value int) {
newNode := Node{}
newNode.value = value
if cicularDoublyLinkedList.head == nil {
cicularDoublyLinkedList.head = &newNode
cicularDoublyLinkedList.tail = &newNode
cicularDoublyLinkedList.length++
return
}
newNode.next = cicularDoublyLinkedList.head
newNode.prev = cicularDoublyLinkedList.tail
cicularDoublyLinkedList.head.prev = &newNode
cicularDoublyLinkedList.tail.next = &newNode
cicularDoublyLinkedList.head = &newNode
cicularDoublyLinkedList.length++
return
}
// Add a value to a specific position of the list
func (cicularDoublyLinkedList *CircularDoublyLinkedList) insert(position, value int) {
if position < 0 {
fmt.Println("Error: Invalid position")
return
}
if position == 0 {
cicularDoublyLinkedList.prepend(value)
return
}
if position > cicularDoublyLinkedList.length {
cicularDoublyLinkedList.append(value)
return
}
ptr := cicularDoublyLinkedList.head
for i := 0; i < position; i++ {
if i == position-1 {
newNode := Node{}
newNode.value = value
newNode.next = ptr
newNode.prev = ptr.prev
ptr.prev.next = &newNode
ptr.prev = &newNode
cicularDoublyLinkedList.length++
return
}
ptr = ptr.next
}
}
// Remove a value from beginning of the list
func (cicularDoublyLinkedList *CircularDoublyLinkedList) removeFromBeginning() {
if cicularDoublyLinkedList.head == nil {
fmt.Println("Linked List is empty")
return
}
prevNode := cicularDoublyLinkedList.head.prev
nextNode := cicularDoublyLinkedList.head.next
prevNode.next = nextNode
nextNode.prev = prevNode
cicularDoublyLinkedList.head = nextNode
cicularDoublyLinkedList.length--
return
}
// Remove a value from end of the list
func (cicularDoublyLinkedList *CircularDoublyLinkedList) removeFromEnd() {
if cicularDoublyLinkedList.head == nil {
fmt.Println("Linked List is empty")
return
}
prevNode := cicularDoublyLinkedList.tail.prev
nextNode := cicularDoublyLinkedList.tail.next
prevNode.next = nextNode
nextNode.prev = prevNode
cicularDoublyLinkedList.tail = prevNode
cicularDoublyLinkedList.length--
return
}
// Remove a value from a specific position
func (cicularDoublyLinkedList *CircularDoublyLinkedList) remove(position int) {
if position < 0 || position > cicularDoublyLinkedList.length {
fmt.Println("Error: Invalid position")
return
}
ptr := cicularDoublyLinkedList.head
for i := 0; i < position; i++ {
if i == position-1 {
prevNode := ptr.prev
nextNode := ptr.next
prevNode.next = nextNode
nextNode.prev = prevNode
cicularDoublyLinkedList.length--
return
}
ptr = ptr.next
}
}
func (cicularDoublyLinkedList *CircularDoublyLinkedList) display() {
if cicularDoublyLinkedList.head == nil {
fmt.Println("Linked list is empty")
return
}
ptr := cicularDoublyLinkedList.head
for ptr.next != cicularDoublyLinkedList.head {
fmt.Print(ptr.value, " ")
ptr = ptr.next
}
fmt.Print(ptr.value)
fmt.Printf("\nLength: %d", cicularDoublyLinkedList.length)
return
}
func main() {
cdll := CircularDoublyLinkedList{}
cdll.append(10)
cdll.append(1)
cdll.append(0)
cdll.prepend(4)
cdll.prepend(3)
cdll.insert(10, 7)
cdll.removeFromBeginning()
cdll.removeFromEnd()
cdll.remove(4)
cdll.display()
}