Created
February 5, 2018 06:03
-
-
Save rezamt/a64958465ac73b64184841ae2172ac94 to your computer and use it in GitHub Desktop.
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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need to add