-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from Magro28/linkedlistgo
Add linked list for int32 in go
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |