-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkQueue.go
More file actions
77 lines (66 loc) · 1.02 KB
/
LinkQueue.go
File metadata and controls
77 lines (66 loc) · 1.02 KB
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
74
75
76
77
package LinkQueue
type Qnode struct {
Data interface{}
Next *Qnode
}
type LinkQueue struct {
Length int
Front *Qnode
Rear *Qnode
}
func (lq *LinkQueue) InitSqQueue() *LinkQueue {
node := &Qnode{}
return &LinkQueue{
Length: 0,
Front: node,
Rear: node,
}
}
func (lq *LinkQueue) enQueue(data interface{}) *LinkQueue {
e := &Qnode{
Data: data,
Next: nil,
}
if lq.Length == 0 {
lq.Front = e
lq.Rear = e
} else {
lq.Rear.Next = e
lq.Rear = e
}
lq.Length++
return lq
}
func (lq *LinkQueue) Dequeue() interface{} {
if lq.Length == 0 {
return nil
}
e := lq.Front
data := e.Data
if lq.Length == 1 {
node := &Qnode{}
lq.Front = node
lq.Rear = node
} else {
lq.Front = lq.Front.Next
}
lq.Length--
e = nil
return data
}
func (lq *LinkQueue) LinkQueue2Slice() []interface{} {
slice := []interface{}{}
if lq.Length == 0 {
return nil
}
cur := lq.Front
for cur != nil {
slice = append(slice, cur.Data)
if cur.Next != nil {
cur = cur.Next
} else {
break
}
}
return slice
}