-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.go
67 lines (59 loc) · 1.07 KB
/
linkedlist.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
package linkedlist
import "github.com/gookit/color"
type Cell struct {
Row int
Col int
Vals []int
Prev *Cell
Next *Cell
}
type LinkedList struct {
Head *Cell
Last *Cell
CurrentCell *Cell
}
func createlinkedList() *LinkedList {
return &LinkedList{}
}
func (p *LinkedList) AddCell(row, col int, arr []int) error {
c := &Cell{
Row: row,
Col: col,
Vals: arr,
}
if p.Head == nil {
p.Head = c
c.Prev = nil
} else {
currentCell := p.Head
for currentCell.Next != nil {
currentCell = currentCell.Next
}
currentCell.Next = c
c.Prev = currentCell
p.Last = c
}
return nil
}
func (p *LinkedList) ShowAllEmptyCells() error {
currentCell := p.Head
if currentCell == nil {
color.Red.Println("EmptyCell list is empty.")
return nil
}
color.Green.Printf("%+v\n", *currentCell)
for currentCell.Next != nil {
currentCell = currentCell.Next
color.Green.Printf("%+v\n", *currentCell)
}
return nil
}
func (p *LinkedList) CountNodes() int {
count := 0
currN := p.Head
for currN != nil {
currN = currN.Next
count += 1
}
return count
}