Skip to content

Commit

Permalink
linked-list: removed panic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddri committed Jan 29, 2015
1 parent c98bda1 commit 54448a7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
38 changes: 21 additions & 17 deletions data-structures/linked-list/linked_list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package list

import (
//"fmt"
"errors"
)

type List struct {
Expand Down Expand Up @@ -67,24 +67,24 @@ func (l *List) Append(value interface{}) {
l.Length++
}

func (l *List) Add(value interface{}, index int) {
func (l *List) Add(value interface{}, index int) error {
if index > l.Len() {
panic("Index out of range")
return errors.New("Index out of range")
}

node := NewNode(value)

if l.Len() == 0 || index == 0 {
l.Prepend(value)
return
return nil
}

if l.Len()-1 == index {
l.Append(value)
return
return nil
}

nextNode := l.Get(index)
nextNode, _ := l.Get(index)
prevNode := nextNode.Prev

prevNode.Next = node
Expand All @@ -94,17 +94,19 @@ func (l *List) Add(value interface{}, index int) {
node.Next = nextNode

l.Length++

return nil
}

func (l *List) Remove(value interface{}) {
func (l *List) Remove(value interface{}) error {
if l.Len() == 0 {
panic("Empty list")
return errors.New("Empty list")
}

if l.Head.Value == value {
l.Head = l.Head.Next
l.Length--
return
return nil
}

found := 0
Expand All @@ -118,26 +120,28 @@ func (l *List) Remove(value interface{}) {
}

if found == 0 {
panic("Node not found")
return errors.New("Node not found")
}

return nil
}

func (l *List) Get(index int) *Node {
func (l *List) Get(index int) (*Node, error) {
if index > l.Len() {
panic("Index out of range")
return nil, errors.New("Index out of range")
}

node := l.Head
for i := 0; i < index; i++ {
node = node.Next
}

return node
return node, nil
}

func (l *List) Find(node *Node) int {
func (l *List) Find(node *Node) (int, error) {
if l.Len() == 0 {
panic("Empty list")
return 0, errors.New("Empty list")
}

index := 0
Expand All @@ -150,10 +154,10 @@ func (l *List) Find(node *Node) int {
})

if found == -1 {
panic("Item not found")
return 0, errors.New("Item not found")
}

return found
return found, nil
}

func (l *List) Clear() {
Expand Down
59 changes: 38 additions & 21 deletions data-structures/linked-list/linked_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ func TestLinkedList(t *testing.T) {
l.Prepend(NewNode(2))
l.Prepend(NewNode(3))

if *l.Get(0).Value.(*Node) != *NewNode(3) ||
*l.Get(1).Value.(*Node) != *NewNode(2) ||
*l.Get(2).Value.(*Node) != *NewNode(1) {

fmt.Println(l.Get(0).Value)
fmt.Println(l.Get(1).Value)
fmt.Println(l.Get(2).Value)
zero := *slice(l.Get(0))[0].(*Node).Value.(*Node)
one := *slice(l.Get(1))[0].(*Node).Value.(*Node)
two := *slice(l.Get(2))[0].(*Node).Value.(*Node)

if zero != *NewNode(3) ||
one != *NewNode(2) ||
two != *NewNode(1) {

fmt.Println(*one.Value.(*Node), *NewNode(2))
fmt.Println(zero.Value)
fmt.Println(one.Value)
fmt.Println(two.Value)
t.Error()
}

Expand All @@ -30,26 +35,35 @@ func TestLinkedList(t *testing.T) {
k.Append(NewNode(2))
k.Append(NewNode(3))

if *k.Get(0).Value.(*Node) != *NewNode(1) ||
*k.Get(1).Value.(*Node) != *NewNode(2) ||
*k.Get(2).Value.(*Node) != *NewNode(3) {
zero = *slice(k.Get(0))[0].(*Node).Value.(*Node)
one = *slice(k.Get(1))[0].(*Node).Value.(*Node)
two = *slice(k.Get(2))[0].(*Node).Value.(*Node)

if zero != *NewNode(1) ||
one != *NewNode(2) ||
two != *NewNode(3) {

fmt.Println(k.Get(0).Value)
fmt.Println(k.Get(1).Value)
fmt.Println(k.Get(2).Value)
fmt.Println(zero.Value)
fmt.Println(one.Value)
fmt.Println(two.Value)
t.Error()
}

// Test Add
k.Add(NewNode(8), 1)

if *k.Get(0).Value.(*Node) != *NewNode(1) ||
*k.Get(1).Value.(*Node) != *NewNode(8) ||
*k.Get(2).Value.(*Node) != *NewNode(2) {
zero = *slice(k.Get(0))[0].(*Node).Value.(*Node)
one = *slice(k.Get(1))[0].(*Node).Value.(*Node)
two = *slice(k.Get(2))[0].(*Node).Value.(*Node)

if zero != *NewNode(1) ||
one != *NewNode(8) ||
two != *NewNode(2) {

fmt.Println(zero.Value)
fmt.Println(one.Value)
fmt.Println(two.Value)

fmt.Println(k.Get(0).Value)
fmt.Println(k.Get(1).Value)
fmt.Println(k.Get(2).Value)
t.Error()
}

Expand All @@ -62,7 +76,6 @@ func TestLinkedList(t *testing.T) {
// Test Each
counter := 0
f := func(node *Node) {
//fmt.Println(node.Value)
counter += node.Value.(int)
}

Expand All @@ -72,7 +85,7 @@ func TestLinkedList(t *testing.T) {
}

// Test Find
index := l.Find(NewNode(1))
index, _ := l.Find(NewNode(1))
if index != 3 {
fmt.Println(index)
t.Error()
Expand All @@ -97,3 +110,7 @@ func TestLinkedList(t *testing.T) {
}

}

func slice(args ...interface{}) []interface{} {
return args
}

0 comments on commit 54448a7

Please sign in to comment.