-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create double linked list and read it
- 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.
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,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 | ||
} | ||
} |