-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueLinkedList.go
More file actions
187 lines (157 loc) · 3.29 KB
/
QueueLinkedList.go
File metadata and controls
187 lines (157 loc) · 3.29 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
*
* @Name: Go - Data Structure and Algorithm (Queue Linked-List)
* @Author: Max Base
* @Date: 2022/10/12
* @Repository: https://github.com/BaseMax/QueueLinkedListGo
* @License: GPL-3.0
*
*/
package main
import "fmt"
type Node struct {
data interface{}
next *Node
}
type QueueLinkedList struct {
head *Node
tail *Node
}
/**
* @summary: Create a new queue
* @return: A new queue
*/
func NewQueueLinkedList() *QueueLinkedList {
return &QueueLinkedList{}
}
/**
* @summary: Add a new element to the queue (to the tail)
* @param: data - the data to add
*/
func (q *QueueLinkedList) Enqueue(data interface{}) {
node := &Node{data: data}
if q.head == nil {
q.head = node
q.tail = node
} else {
q.tail.next = node
q.tail = node
}
}
/**
* @summary: Add a new element to the queue (to the head)
* @param: data - the data to add
*/
func (q *QueueLinkedList) EnqueueHead(data interface{}) {
node := &Node{data: data}
if q.head == nil {
q.head = node
q.tail = node
} else {
node.next = q.head
q.head = node
}
}
/**
* @summary: Remove the first element from the queue (from the head)
* @return: The data of the first element
*/
func (q *QueueLinkedList) Dequeue() interface{} {
if q.head == nil {
return nil
}
node := q.head
q.head = q.head.next
return node.data
}
/**
* @summary: Remove the first element from the queue (from the tail)
* @return: The data of the first element
*/
func (q *QueueLinkedList) DequeueTail() interface{} {
if q.head == nil {
return nil
}
node := q.head
q.head = q.head.next
return node.data
}
/**
* @summary: Get the first element from the queue (from the head)
* @return: The data of the first element
*/
func (q *QueueLinkedList) Peek() interface{} {
if q.head == nil {
return nil
}
return q.head.data
}
/**
* @summary: Get the first element from the queue (from the tail)
* @return: The data of the first element
*/
func (q *QueueLinkedList) PeekTail() interface{} {
if q.head == nil {
return nil
}
return q.tail.data
}
/**
* @summary: Check if the queue is empty
* @return: True if the queue is empty, false otherwise
*/
func (q *QueueLinkedList) IsEmpty() bool {
return q.head == nil
}
/**
* @summary: Get the size of the queue
* @return: The size of the queue
*/
func (q *QueueLinkedList) Size() int {
size := 0
for node := q.head; node != nil; node = node.next {
size++
}
return size
}
/**
* @summary: Clear the queue
*/
func (q *QueueLinkedList) Clear() {
q.head = nil
q.tail = nil
}
/**
* @summary: Print the queue
*/
func (q *QueueLinkedList) Print() {
for node := q.head; node != nil; node = node.next {
fmt.Println(node.data)
}
}
/**
* @summary: Reverse the queue
*/
func (q *QueueLinkedList) Reverse() {
var prev *Node
curr := q.head
for curr != nil {
next := curr.next
curr.next = prev
prev = curr
curr = next
}
q.head = prev
}
/**
* @summary: Reverse the queue (recursive)
*/
func (q *QueueLinkedList) ReverseRecursive(Node *Node) *Node {
if Node == nil || Node.next == nil {
return Node
}
newHead := q.ReverseRecursive(Node.next)
Node.next.next = Node
Node.next = nil
return newHead
}