-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllist-stack-queue-pop.go
73 lines (57 loc) · 962 Bytes
/
llist-stack-queue-pop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import "fmt"
type List struct {
head *Node
tail *Node
}
type Node struct {
value string
prev *Node
next *Node
}
var x int
func (l *List) StackInsertBack(value string) {
n := &Node{
value: value,
prev: l.tail,
}
if l.tail != nil {
l.tail.next = n
}
l.tail = n
if l.head == nil {
l.head = n
}
}
func (l *List) PopStack(x int) {
if l.head == nil && l.tail == nil {
fmt.Println("Null")
}
for l.head != nil && x > 0 {
fmt.Println(l.head.value)
l.head = l.head.next
x = x - 1
}
}
func (l *List) PopQueue(x int) {
fmt.Println("x:", x)
if l.head == nil && l.tail == nil {
fmt.Println("Null")
}
for l.tail != nil && x > 0 {
fmt.Println(l.tail.value)
l.tail = l.tail.prev
x = x - 1
}
}
func main() {
l := List{}
l.StackInsertBack("15")
l.StackInsertBack("15")
l.StackInsertBack("10")
l.StackInsertBack("10")
l.StackInsertBack("22")
l.StackInsertBack("10")
l.PopStack(3)
l.PopQueue(3)
}