Skip to content

Commit

Permalink
Merge pull request #291 from Magro28/linkedlistgo
Browse files Browse the repository at this point in the history
Add linked list for int32 in go
  • Loading branch information
i-vishi authored Oct 20, 2018
2 parents 1c2367f + c79eb6f commit 0ea26dd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
37 changes: 37 additions & 0 deletions data structures/linked list/go/linkedlistint32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package linkedlistint32

import "fmt"

type Element struct {
Value int32
Next *Element
}

type LinkedListInt32 struct {
First *Element
Last *Element
}

func (ll *LinkedListInt32) push(value int32) {
newElement := Element{Value: value, Next: nil}

if ll.First == nil {
ll.First = &newElement
}
if ll.Last == nil {
ll.Last = &newElement
} else {
ll.Last.Next = &newElement
ll.Last = &newElement
}
}

func print(ll LinkedListInt32) {
var element = ll.First
for element.Next != nil {
fmt.Printf("%v ",element.Value);
element = element.Next
}
fmt.Printf("%v ",element.Value);
}

34 changes: 34 additions & 0 deletions data structures/linked list/go/linkedlistint32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package linkedlistint32

import (
_ "testing"
"testing"
)

func Test(t *testing.T) {
var ll LinkedListInt32
ll.push(11)
ll.push(12)
ll.push(55)
ll.push(67)
print(ll)

if ll.First == nil {
t.Error("first element is empty")
}
if ll.First.Value != 11 {
t.Error("first element is not 11")
}

if ll.First.Next.Value != 12 {
t.Error("second element is not 12")
}

if ll.First.Next.Next.Value != 55 {
t.Error("third element is not 55")
}

if (ll.Last.Value != 67){
t.Error("last element is not 67")
}
}

0 comments on commit 0ea26dd

Please sign in to comment.