Skip to content

Commit 0308430

Browse files
authored
Merge pull request #202 from Mystigan/145-linked_list-tests
Add tests for Singly Linked List library
2 parents d45c4b0 + e1302bd commit 0308430

File tree

3 files changed

+180
-123
lines changed

3 files changed

+180
-123
lines changed

data-structures/linked-list/single-linkedlist.go

Lines changed: 0 additions & 123 deletions
This file was deleted.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package singlelinkedlist
2+
3+
// demonstration of singly linked list in golang
4+
import "fmt"
5+
6+
type node struct {
7+
Val interface{}
8+
Next *node
9+
}
10+
11+
type singlelinkedlist struct {
12+
length int
13+
Head *node
14+
}
15+
16+
// CreateList returns a new instance of a linked list
17+
func CreateList() *singlelinkedlist {
18+
return &singlelinkedlist{}
19+
}
20+
21+
// to avoid mistakes when using pointer vs struct for new node creation
22+
func newNode(val interface{}) *node {
23+
return &node{val, nil}
24+
}
25+
26+
// AddAtBeg adds a new node with given value at the beginning of the list.
27+
func (ll *singlelinkedlist) AddAtBeg(val interface{}) {
28+
n := newNode(val)
29+
n.Next = ll.Head
30+
ll.Head = n
31+
ll.length++
32+
}
33+
34+
// AddAtEnd adds a new node with given value at the end of the list.
35+
func (ll *singlelinkedlist) AddAtEnd(val int) {
36+
n := newNode(val)
37+
38+
if ll.Head == nil {
39+
ll.Head = n
40+
ll.length++
41+
return
42+
}
43+
44+
cur := ll.Head
45+
for ; cur.Next != nil; cur = cur.Next {
46+
}
47+
cur.Next = n
48+
ll.length++
49+
}
50+
51+
// DelAtBeg deletes the node at the head(beginning) of the list and returns its value. Returns -1 if the list is empty.
52+
func (ll *singlelinkedlist) DelAtBeg() interface{} {
53+
if ll.Head == nil {
54+
return -1
55+
}
56+
57+
cur := ll.Head
58+
ll.Head = cur.Next
59+
ll.length--
60+
61+
return cur.Val
62+
}
63+
64+
// DelAtEnd deletes the node at the tail(end) of the list and returns its value. Returns -1 if the list is empty.
65+
func (ll *singlelinkedlist) DelAtEnd() interface{} {
66+
if ll.Head == nil {
67+
return -1
68+
}
69+
70+
if ll.Head.Next == nil {
71+
return ll.DelAtBeg()
72+
}
73+
74+
cur := ll.Head
75+
76+
for ; cur.Next.Next != nil; cur = cur.Next {
77+
}
78+
79+
retval := cur.Next.Val
80+
cur.Next = nil
81+
ll.length--
82+
return retval
83+
84+
}
85+
86+
// Count returns the current size of the list.
87+
func (ll *singlelinkedlist) Count() int {
88+
return ll.length
89+
}
90+
91+
// Reverse reverses the list.
92+
func (ll *singlelinkedlist) Reverse() {
93+
var prev, Next *node
94+
cur := ll.Head
95+
96+
for cur != nil {
97+
Next = cur.Next
98+
cur.Next = prev
99+
prev = cur
100+
cur = Next
101+
}
102+
103+
ll.Head = prev
104+
}
105+
106+
// Display prints out the elements of the list.
107+
func (ll *singlelinkedlist) Display() {
108+
for cur := ll.Head; cur != nil; cur = cur.Next {
109+
fmt.Print(cur.Val, " ")
110+
}
111+
112+
fmt.Print("\n")
113+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package singlelinkedlist
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestLinkedList(t *testing.T) {
9+
list := CreateList()
10+
list.AddAtBeg(1)
11+
list.AddAtBeg(2)
12+
list.AddAtBeg(3)
13+
14+
t.Run("Test AddAtBeg()", func(t *testing.T) {
15+
want := []interface{}{3, 2, 1}
16+
got := []interface{}{}
17+
current := list.Head
18+
got = append(got, current.Val)
19+
for current.Next != nil {
20+
current = current.Next
21+
got = append(got, current.Val)
22+
}
23+
if !reflect.DeepEqual(got, want) {
24+
t.Errorf("got: %v, want: %v", got, want)
25+
}
26+
})
27+
28+
list.AddAtEnd(4)
29+
30+
t.Run("Test AddAtEnd()", func(t *testing.T) {
31+
want := []interface{}{3, 2, 1, 4}
32+
got := []interface{}{}
33+
current := list.Head
34+
got = append(got, current.Val)
35+
for current.Next != nil {
36+
current = current.Next
37+
got = append(got, current.Val)
38+
}
39+
if !reflect.DeepEqual(got, want) {
40+
t.Errorf("got: %v, want: %v", got, want)
41+
}
42+
})
43+
44+
t.Run("Test DelAtBeg()", func(t *testing.T) {
45+
want := interface{}(3)
46+
got := list.DelAtBeg()
47+
if got != want {
48+
t.Errorf("got: %v, want: %v", got, want)
49+
}
50+
})
51+
52+
t.Run("Test DelAtEnd()", func(t *testing.T) {
53+
want := interface{}(4)
54+
got := list.DelAtEnd()
55+
if got != want {
56+
t.Errorf("got: %v, want: %v", got, want)
57+
}
58+
})
59+
60+
t.Run("Test Count()", func(t *testing.T) {
61+
want := 2
62+
got := list.Count()
63+
if got != want {
64+
t.Errorf("got: %v, want: %v", got, want)
65+
}
66+
})
67+
}

0 commit comments

Comments
 (0)