Skip to content

Instantly share code, notes, and snippets.

@rezamt
Created February 5, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezamt/a64958465ac73b64184841ae2172ac94 to your computer and use it in GitHub Desktop.
Save rezamt/a64958465ac73b64184841ae2172ac94 to your computer and use it in GitHub Desktop.
package main
import (
"strconv"
"fmt"
)
type Node struct {
value interface{}
next *Node
prev *Node
}
type IList interface {
append(node *Node)
remove(node *Node)
create(value interface{}) *Node
search(value interface{}) (interface{}, bool)
length() int
Head() *Node
Tail() *Node
}
type LinkList struct {
head *Node
tail *Node
size int
}
func (l *LinkList) append(node *Node) {
if l.head == nil {
l.head = node
l.tail = l.head
return
}
l.tail.next = node
l.tail = node
l.size++
}
func (l *LinkList) remove(node *Node) {
l.size--
}
func (l *LinkList) create(value interface{}) *Node {
if value == nil {
return nil
}
return &Node{value: value}
}
func (l *LinkList) search(value interface{}) (interface{}, bool) {
return nil, false // for now
}
func (l *LinkList) length() int {
return l.size
}
func (l *LinkList) Head() *Node {
return l.head
}
func (l *LinkList) Tail() *Node {
return l.tail
}
func traverse(l *IList) {
if l == nil {
return
}
_traverse((*l).Head())
}
func _traverse(n *Node) {
if n == nil {
return
}
if n.next != nil {
}
fmt.Println("Node: ", n.value)
_traverse(n.next)
}
func createLinkList() *LinkList {
return &LinkList{}
}
func main() {
var sl IList
sl = createLinkList()
for i, _ := range [...]int{1, 2, 3, 4, 5, 6, 6} {
n := sl.create("Hello --> " + strconv.Itoa(i))
sl.append(n)
}
traverse(&sl)
}
@rezamt
Copy link
Author

rezamt commented Feb 5, 2018

I need to add

  • Error handling and logging
  • Test cases (for testing the routines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment