Skip to content

Commit cd4bff8

Browse files
committed
make the linked list generic over the Node value
1 parent 316865c commit cd4bff8

File tree

8 files changed

+39
-52
lines changed

8 files changed

+39
-52
lines changed

linkedlist/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ A linked list is a linear collection of data elements whose order is not given b
99
The principal benefit of a linked list over a conventional array is that the list elements can be easily inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk, while restructuring an array at run-time is a much more expensive operation. Linked lists allow insertion and removal of nodes at any point in the list, and allow doing so with a constant number of operations by keeping the link previous to the link being added or removed in memory during list traversal.
1010

1111
This module serves as the base for all executable. So we don't have to redefine the Node, Linkedlist and the basic methods while solving the problems.
12-

linkedlist/compare_strings/compare.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package compare
22

3-
func compareStrings(list1 *Linkedlist, list2 *Linkedlist) int {
3+
func compareStrings(list1 *llist, list2 *llist) int {
44
// remember that the list is defines as:
55
// type Linkedlist struct {
66
// Length int
@@ -17,14 +17,15 @@ func compareStrings(list1 *Linkedlist, list2 *Linkedlist) int {
1717

1818
cur1 := list1.Head
1919
cur2 := list2.Head
20-
for cur1.Val == cur2.Val && cur1.Next != nil {
20+
// type assertion is required as the Node value = interface{}
21+
for cur1.Val.(string) == cur2.Val.(string) && cur1.Next != nil {
2122
cur1 = cur1.Next
2223
cur2 = cur2.Next
2324
}
2425

25-
if cur1.Val > cur2.Val {
26+
if cur1.Val.(string) > cur2.Val.(string) {
2627
return 1
27-
} else if cur1.Val < cur2.Val {
28+
} else if cur1.Val.(string) < cur2.Val.(string) {
2829
return -1
2930
}
3031
//asumes the list are equal

linkedlist/compare_strings/compare_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ package compare
22

33
import (
44
"testing"
5+
6+
"github.com/danrusei/algorithms_with_go/linkedlist"
57
)
68

9+
type llist struct {
10+
linkedlist.Linkedlist
11+
}
12+
713
// this function creates the lists,
814
// the way the linkedlist is defined it allows us to check the length
9-
func createLinkedList(vals []string) *Linkedlist {
10-
list := NewLinkedlist()
11-
node := NewNode(vals[0])
15+
func createLinkedList(vals []string) *llist {
16+
list := llist{}
17+
node := linkedlist.NewNode(vals[0])
1218
list.Head = node
1319
list.Length = 1
1420
cur := list.Head
1521
for i, val := range vals {
1622
if i == 0 {
1723
continue
1824
}
19-
node := NewNode(val)
25+
node := linkedlist.NewNode(val)
2026
cur.Next = node
2127
list.Length++
2228
}
23-
return list
29+
return &list
2430
}
2531

2632
func TestComparedStrings(t *testing.T) {

linkedlist/compare_strings/linked_list.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

linkedlist/insert_node/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type llist struct {
1414
func (ll *llist) sortedInsert(n *linkedlist.Node) {
1515

1616
//special case for the head end
17-
if ll.Head == nil || ll.Head.Val >= n.Val {
17+
// type assertion is required as the Linked List value = interface{}
18+
if ll.Head == nil || ll.Head.Val.(int) >= n.Val.(int) {
1819
n.Next = ll.Head
1920
ll.Head = n
2021
ll.Length++
@@ -23,7 +24,7 @@ func (ll *llist) sortedInsert(n *linkedlist.Node) {
2324

2425
//locate the node before the point of insertion
2526
cur := ll.Head
26-
for ; cur.Next != nil && cur.Next.Val < n.Val; cur = cur.Next {
27+
for ; cur.Next != nil && cur.Next.Val.(int) < n.Val.(int); cur = cur.Next {
2728
}
2829
n.Next = cur.Next
2930
cur.Next = n

linkedlist/linkedlist.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ package linkedlist
1212
import "fmt"
1313

1414
// Node struct holds a node
15+
// the value of the Node could be anything (int, string or a struncture)
16+
// interface{} - an empty interface may hold values of any type.
17+
// if using interface{} confuse you, change with any type you would like
18+
// initially I started with int value, but some problems require strings as values
19+
// another example: implementing Hash-Table I need even to define a Key-Value struct
1520
type Node struct {
16-
Val int
21+
Val interface{}
1722
Next *Node
1823
}
1924

@@ -29,7 +34,7 @@ func NewLinkedlist() *Linkedlist {
2934
}
3035

3136
// NewNode creates a new node
32-
func NewNode(val int) *Node {
37+
func NewNode(val interface{}) *Node {
3338
return &Node{val, nil}
3439
}
3540

@@ -38,14 +43,14 @@ func (ll *Linkedlist) String() string {
3843
cur := ll.Head
3944
var printedlist string
4045
for ; cur.Next != nil; cur = cur.Next {
41-
printedlist = printedlist + fmt.Sprintf("%d \t", cur.Val)
46+
printedlist = printedlist + fmt.Sprintf("%v \t", cur.Val)
4247
}
43-
printedlist = printedlist + fmt.Sprintf("%d", cur.Val)
48+
printedlist = printedlist + fmt.Sprintf("%v", cur.Val)
4449
return printedlist
4550
}
4651

4752
//AddAtBeg adds a Node at the beginning of the list
48-
func (ll *Linkedlist) AddAtBeg(val int) {
53+
func (ll *Linkedlist) AddAtBeg(val interface{}) {
4954
n := NewNode(val)
5055
n.Next = ll.Head
5156
ll.Head = n
@@ -54,7 +59,7 @@ func (ll *Linkedlist) AddAtBeg(val int) {
5459
}
5560

5661
//AddAtEnd adds a Node at the end of the list
57-
func (ll *Linkedlist) AddAtEnd(val int) {
62+
func (ll *Linkedlist) AddAtEnd(val interface{}) {
5863
n := NewNode(val)
5964

6065
if ll.Head == nil {
@@ -72,7 +77,7 @@ func (ll *Linkedlist) AddAtEnd(val int) {
7277
}
7378

7479
//DelAtBeg deletes a node from the beginning of the list
75-
func (ll *Linkedlist) DelAtBeg() int {
80+
func (ll *Linkedlist) DelAtBeg() interface{} {
7681
if ll.Head == nil {
7782
return -1
7883
}
@@ -84,7 +89,7 @@ func (ll *Linkedlist) DelAtBeg() int {
8489
}
8590

8691
//DelAtEnd deletes a node from the end of the list
87-
func (ll *Linkedlist) DelAtEnd() int {
92+
func (ll *Linkedlist) DelAtEnd() interface{} {
8893
if ll.Head == nil {
8994
return -1
9095
}
@@ -101,7 +106,7 @@ func (ll *Linkedlist) DelAtEnd() int {
101106
}
102107

103108
//DeleteWithValute deletes a node which value is equal to the function parameter
104-
func (ll *Linkedlist) DeleteWithValute(val int) int {
109+
func (ll *Linkedlist) DeleteWithValute(val interface{}) interface{} {
105110
if ll.Head == nil {
106111
return -1
107112
}

linkedlist/linkedlist_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ func TestLinkedList(t *testing.T) {
1515
want := []int{3, 2, 1}
1616
got := []int{}
1717
current := list.Head
18-
got = append(got, current.Val)
18+
got = append(got, current.Val.(int))
1919
for current.Next != nil {
2020
current = current.Next
21-
got = append(got, current.Val)
21+
got = append(got, current.Val.(int))
2222
}
2323
if !reflect.DeepEqual(got, want) {
2424
t.Errorf("got: %v, want: %v", got, want)
@@ -31,10 +31,10 @@ func TestLinkedList(t *testing.T) {
3131
want := []int{3, 2, 1, 4}
3232
got := []int{}
3333
current := list.Head
34-
got = append(got, current.Val)
34+
got = append(got, current.Val.(int))
3535
for current.Next != nil {
3636
current = current.Next
37-
got = append(got, current.Val)
37+
got = append(got, current.Val.(int))
3838
}
3939
if !reflect.DeepEqual(got, want) {
4040
t.Errorf("got: %v, want: %v", got, want)

linkedlist/random_node/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func (ll *llist) randomNode() int {
3737
n++
3838
}
3939

40-
return result
40+
// type assertion is required because Linked List value = interface{}
41+
return result.(int)
4142
}
4243

4344
func main() {

0 commit comments

Comments
 (0)