-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstacknqueue.go
More file actions
165 lines (136 loc) · 2.56 KB
/
stacknqueue.go
File metadata and controls
165 lines (136 loc) · 2.56 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package stacknqueue
import "sync"
type node struct {
data interface{}
next *node
prev *node
}
type StackNQueue struct {
head *node
tail *node
count uint64
threadSafe bool
lock *sync.Mutex
}
// NewStackNQueue creates a new LinkedList object that can be used as a
// Stack and/or a Queue and returns a pointer
func NewStackNQueue(threadSafe bool) *StackNQueue {
q := &StackNQueue{threadSafe:threadSafe}
if threadSafe {
q.lock = &sync.Mutex{}
}
return q
}
// Len returns the number of elements in the List
func (q *StackNQueue) Len() uint64 {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
return q.count
}
// Push inserts the value to the front of the List
func (q *StackNQueue) Push(item interface{}) {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
n := &node{data: item}
if q.head == nil {
q.head = n
q.tail = n
} else {
q.head.prev = n
n.next = q.head
q.head = n
}
q.count++
}
// Pop removes and returns the value at the front of the List.
func (q *StackNQueue) Pop() interface{} {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
if q.head == nil {
return nil
}
n := q.head
q.head = n.next
if q.head == nil {
q.tail = nil
}
q.count--
return n.data
}
// Queue adds an item to the end of the List.
// This is a FIFO action.
func (q *StackNQueue) Queue(item interface{}) {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
n := &node{data: item}
if q.tail == nil {
q.tail = n
q.head = n
} else {
n.prev = q.tail
q.tail.next = n
q.tail = n
}
q.count++
}
// Dequeue removes and returns the last item in the List.
// This is a crazy action.
func (q *StackNQueue) Dequeue() interface{} {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
//No items in the list
if q.tail == nil {
return nil
}
//Only one item in this List
if q.head == q.tail {
response := q.head
q.head = nil
q.tail = nil
q.count--
return response.data
}
response := q.tail
q.tail = response.prev
q.count--
return response.data
}
// Peek returns the value at the front of the List
// without mutation. This means the value is not removed.
func (q *StackNQueue) Peek() interface{} {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
n := q.head
if n == nil {
return nil
}
return n.data
}
func (q *StackNQueue) Empty() {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
q.count = 0
q.head = nil
q.tail = nil
}
func (q *StackNQueue) IsEmpty() bool {
if q.threadSafe {
q.lock.Lock()
defer q.lock.Unlock()
}
return q.count == 0
}