@@ -23,13 +23,15 @@ func newNode(val interface{}) *node {
23
23
return & node {val , nil }
24
24
}
25
25
26
+ // AddAtBeg adds a new node with given value at the beginning of the list.
26
27
func (ll * singlelinkedlist ) AddAtBeg (val interface {}) {
27
28
n := newNode (val )
28
29
n .Next = ll .Head
29
30
ll .Head = n
30
31
ll .length ++
31
32
}
32
33
34
+ // AddAtEnd adds a new node with given value at the end of the list.
33
35
func (ll * singlelinkedlist ) AddAtEnd (val int ) {
34
36
n := newNode (val )
35
37
@@ -46,6 +48,7 @@ func (ll *singlelinkedlist) AddAtEnd(val int) {
46
48
ll .length ++
47
49
}
48
50
51
+ // DelAtBeg deletes the node at the head(beginning) of the list and returns its value. Returns -1 if the list is empty.
49
52
func (ll * singlelinkedlist ) DelAtBeg () interface {} {
50
53
if ll .Head == nil {
51
54
return - 1
@@ -58,6 +61,7 @@ func (ll *singlelinkedlist) DelAtBeg() interface{} {
58
61
return cur .Val
59
62
}
60
63
64
+ // DelAtEnd deletes the node at the tail(end) of the list and returns its value. Returns -1 if the list is empty.
61
65
func (ll * singlelinkedlist ) DelAtEnd () interface {} {
62
66
if ll .Head == nil {
63
67
return - 1
@@ -79,10 +83,12 @@ func (ll *singlelinkedlist) DelAtEnd() interface{} {
79
83
80
84
}
81
85
86
+ // Count returns the current size of the list.
82
87
func (ll * singlelinkedlist ) Count () int {
83
88
return ll .length
84
89
}
85
90
91
+ // Reverse reverses the list.
86
92
func (ll * singlelinkedlist ) Reverse () {
87
93
var prev , Next * node
88
94
cur := ll .Head
@@ -97,29 +103,11 @@ func (ll *singlelinkedlist) Reverse() {
97
103
ll .Head = prev
98
104
}
99
105
106
+ // Display prints out the elements of the list.
100
107
func (ll * singlelinkedlist ) Display () {
101
108
for cur := ll .Head ; cur != nil ; cur = cur .Next {
102
109
fmt .Print (cur .Val , " " )
103
110
}
104
111
105
112
fmt .Print ("\n " )
106
113
}
107
-
108
- // func main() {
109
- // ll := singlelinkedlist{}
110
-
111
- // ll.addAtBeg(10)
112
- // ll.addAtEnd(20)
113
- // ll.display()
114
- // ll.addAtBeg(30)
115
- // ll.display()
116
- // ll.reverse()
117
- // ll.display()
118
-
119
- // fmt.Print(ll.delAtBeg(), "\n")
120
- // ll.display()
121
-
122
- // fmt.Print(ll.delAtEnd(), "\n")
123
- // ll.display()
124
-
125
- // }
0 commit comments