Skip to content

Commit

Permalink
create double linked list and read it
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishi Davidson authored and Nishi Davidson committed May 17, 2020
1 parent e9be859 commit 5e42310
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions dllist-new-create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"fmt"
"strings"
)

type List struct {
head *Node
tail *Node
}

type Node struct {
val string
next *Node
prev *Node
}

func main() {
s := "seattle"
sa := strings.Split(s, "")
l := &List{}
n := &Node{}

for i, _ := range sa {
n = l.createList(&Node{
val: sa[i],
next: nil,
prev: nil,
})
}

fmt.Println(n)

l.showList()
}

func (l *List) createList(newnode *Node) *Node {
if l.head == nil {
l.head = newnode
l.tail = newnode
return l.head
}

currnode := l.head
for currnode.next != nil {
currnode = currnode.next
}
currnode.next = newnode
newnode.prev = currnode
l.tail = newnode

return l.head
}

func (l *List) showList() {
currnode := l.head
for currnode != nil {
fmt.Printf("currnode: %v\n", currnode)
currnode = currnode.next
}
}

0 comments on commit 5e42310

Please sign in to comment.