-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly.go
122 lines (93 loc) · 2.2 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
package doubly
import "fmt"
// define the Node for doubly linkedlist
type Node struct {
prev *Node
value int
next *Node
}
// now define the linked list
type LinkedList struct {
head *Node
}
// insert the value at the start of the list
func (list *LinkedList) InsertAtStart(value int) {
// create a new node with value
newNode := &Node{value: value}
// check if the head is none
if list.head == nil {
list.head = newNode
return
}
// store new node to the temp
tmp := newNode
// save head to the temp next
tmp.next = list.head
// save tmp prev in head
list.head.prev = tmp
// now change the head with the start node
list.head = tmp
}
// display the linked list
func (list *LinkedList) Display() {
// check there is data or linked list exists
if list.IsEmpty() {
fmt.Println("Ooops! There is no data to display. please insert the data.")
return
}
// save list head in temp var
current := list.head
// loop through the list
for current != nil {
// display the value with the link arrow
fmt.Printf("%d ⇆ ", current.value)
// save the next node to the current for keep forwading the loop
current = current.next
}
fmt.Println("nil")
}
// a function check list is empty of not
func (list *LinkedList) IsEmpty() bool {
return list.head == nil
}
// count the list
func (list *LinkedList) Count() int {
// check there is any data in linked list
if list.IsEmpty() {
return 0
}
// initialize the count
count := 0
// temp node to store head
temp := list.head
// loop through the list
for temp.next != nil {
count++
temp = temp.next
}
// return the counter
return count
}
// find the node in list
func (list *LinkedList) Find(index int) {
// check there is any data in linked list
if list.IsEmpty() {
fmt.Println("Ooops! There is no data to display. please insert the data.")
return
}
// get the count of the linked list
count := list.Count()
// check the count
if index > count {
fmt.Println("Ooops! index is not found.")
return
}
// current node to store head
current := list.head
// loop through the count
for range index {
current = current.next
}
// print the current value
fmt.Printf("So %d was found at index %d\n", current.value, index)
}