Skip to content

Commit ef2a845

Browse files
author
Santo
authored
Added linkedlist
1 parent c9be35e commit ef2a845

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Linkedlist.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type node struct {
6+
v int
7+
next *node
8+
}
9+
10+
var head *node = nil
11+
12+
func (l *node) pushFront(val int) *node {
13+
if head == nil {
14+
l.v = val
15+
l.next = nil
16+
head = l
17+
return l
18+
} else {
19+
nnode := new(node)
20+
nnode = head
21+
22+
nnode2 := &node {
23+
v: val,
24+
next: nnode,
25+
}
26+
head = nnode2
27+
return head
28+
}
29+
}
30+
31+
func (l *node) pushBack(val int) *node {
32+
if head == nil {
33+
l.v = val
34+
l.next = nil
35+
head = l
36+
return l
37+
} else {
38+
for l.next != nil {
39+
l = l.next
40+
}
41+
42+
l.next = new(node)
43+
l.next.v = val
44+
l.next.next = nil
45+
return l
46+
}
47+
}
48+
49+
func (l *node) popFront() *node {
50+
if head == nil {
51+
return head
52+
}
53+
54+
cpnode := new(node)
55+
cpnode = head.next
56+
head = cpnode
57+
58+
return head
59+
}
60+
61+
func (l *node) popBack() *node {
62+
if head == nil {
63+
return head
64+
}
65+
66+
cpnode := new(node)
67+
cpnode = head
68+
69+
for cpnode.next.next != nil {
70+
cpnode = cpnode.next
71+
}
72+
73+
cpnode.next = nil
74+
return head
75+
}
76+
77+
func main() {
78+
lista := new(node)
79+
lista.pushBack(25).pushBack(24).pushBack(32)
80+
lista.pushBack(56)
81+
lista.pushFront(36)
82+
lista.popFront()
83+
lista.popBack()
84+
for head != nil {
85+
fmt.Printf("%d ",head.v)
86+
head = head.next
87+
}
88+
}

0 commit comments

Comments
 (0)