Skip to content

Commit 204a737

Browse files
authored
Add Stack and Queue Data Structures (#323)
* Add Queue Data Structure With Array, Linked-List, Container/List, and tests * Add Stack Data Structure With Array, Linked-List, Container/List, and tests * Update list of algorithms
1 parent e24747c commit 204a737

File tree

9 files changed

+819
-3
lines changed

9 files changed

+819
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
3333
* Linked-List
3434
* [Doubly Linked-list](./data_structures/linkedlist/doubly_linkedlist/)
3535
* [Singly Linked-list](./data_structures/linkedlist/singly_linkedlist/)
36+
* [Queue](./data_structures/queue/) | Array, Linked-List, STL(Container/List), Test
37+
* [Stack](./data_structures/stack/) | Array, Linked-List, STL(Container/List), Test
38+
* [Set](./data_structures/set/)
3639
* [Trie](./data_structures/trie/)
3740

3841
## Design Patterns
@@ -81,9 +84,6 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
8184
* [Binary Search](./searches/binary_search/)
8285
* [Linear Search](./searches/linear_search/)
8386

84-
## Set
85-
* [Set](./data_structures/set/set.go)
86-
8787
## Sorts
8888
* [Bubble Sort](./sorts/bubblesort.go)
8989
* [Heap Sort](./sorts/heapsort.go)

data_structures/queue/queue_test.go

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
// Queue Test
2+
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
3+
// The order is First In First Out (FIFO).
4+
// details:
5+
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
6+
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
7+
// author [Milad](https://github.com/miraddo)
8+
// see queuearray.go, queuelinkedlist.go, queuelinkedlistwithlist.go
9+
10+
package queue
11+
12+
import (
13+
"container/list"
14+
"testing"
15+
)
16+
17+
func TestQueue(t *testing.T) {
18+
19+
// Handle Queue Linked List
20+
t.Run("Test Queue Linked List", func(t *testing.T) {
21+
22+
t.Run("Test EnQueue", func(t *testing.T) {
23+
var newQueue Queue
24+
newQueue.enqueue(2)
25+
newQueue.enqueue(3)
26+
newQueue.enqueue(4)
27+
newQueue.enqueue(45)
28+
29+
if newQueue.frontQueue() != 2 && newQueue.backQueue() != 45 {
30+
t.Errorf("Test EnQueue is wrong the result must be %v and %v but got %v and %v", 2, 45, newQueue.frontQueue(), newQueue.backQueue())
31+
}
32+
33+
})
34+
35+
t.Run("Test DeQueue", func(t *testing.T) {
36+
var newQueue Queue
37+
newQueue.enqueue(2)
38+
newQueue.enqueue(3)
39+
newQueue.enqueue(4)
40+
41+
newQueue.dequeue()
42+
if newQueue.dequeue() != 3 {
43+
t.Errorf("Test DeQueue is wrong the result must be %v but got %v", 3, newQueue.dequeue())
44+
}
45+
46+
//fmt.Println(newQueue.show())
47+
})
48+
49+
t.Run("Test Queue isEmpty", func(t *testing.T) {
50+
var newQueue Queue
51+
if newQueue.isEmpty() != true {
52+
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", true, newQueue.isEmpty())
53+
}
54+
55+
newQueue.enqueue(3)
56+
newQueue.enqueue(4)
57+
58+
if newQueue.isEmpty() != false {
59+
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", false, newQueue.isEmpty())
60+
}
61+
})
62+
63+
t.Run("Test Queue Length", func(t *testing.T) {
64+
var newQueue Queue
65+
if newQueue.len() != 0 {
66+
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 0, newQueue.len())
67+
}
68+
69+
newQueue.enqueue(3)
70+
newQueue.enqueue(4)
71+
newQueue.dequeue()
72+
newQueue.enqueue(22)
73+
newQueue.enqueue(99)
74+
newQueue.dequeue()
75+
newQueue.dequeue()
76+
77+
if newQueue.len() != 1 {
78+
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 1, newQueue.len())
79+
}
80+
81+
})
82+
})
83+
84+
// Handle Queue Array
85+
t.Run("Test Queue Array", func(t *testing.T) {
86+
t.Run("Test EnQueue", func(t *testing.T) {
87+
EnQueue(2)
88+
EnQueue(23)
89+
EnQueue(45)
90+
EnQueue(66)
91+
92+
if FrontQueue() != 2 && BackQueue() != 66 {
93+
t.Errorf("Test EnQueue is wrong the result must be %v and %v but got %v and %v", 2, 66, FrontQueue(), BackQueue())
94+
}
95+
96+
})
97+
98+
t.Run("Test DeQueue", func(t *testing.T) {
99+
EnQueue(2)
100+
EnQueue(23)
101+
EnQueue(45)
102+
EnQueue(66)
103+
104+
DeQueue()
105+
DeQueue()
106+
107+
if DeQueue() != 45 {
108+
t.Errorf("Test DeQueue is wrong the result must be %v but got %v", 45, DeQueue())
109+
}
110+
})
111+
112+
ListQueue = []interface{}{}
113+
114+
t.Run("Test Queue isEmpty", func(t *testing.T) {
115+
116+
if IsEmptyQueue() != true {
117+
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", true, IsEmptyQueue())
118+
}
119+
120+
EnQueue(3)
121+
EnQueue(4)
122+
123+
if IsEmptyQueue() != false {
124+
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", false, IsEmptyQueue())
125+
}
126+
})
127+
128+
ListQueue = []interface{}{}
129+
t.Run("Test Queue Length", func(t *testing.T) {
130+
if LenQueue() != 0 {
131+
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 0, LenQueue())
132+
}
133+
134+
EnQueue(3)
135+
EnQueue(4)
136+
DeQueue()
137+
EnQueue(22)
138+
EnQueue(99)
139+
DeQueue()
140+
DeQueue()
141+
142+
if LenQueue() != 1 {
143+
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 1, LenQueue())
144+
}
145+
146+
})
147+
148+
149+
})
150+
151+
// Handle Queue Linked-List With Container/List (STL)
152+
t.Run("Test Container/List For Queue", func(t *testing.T) {
153+
listQueue := &LQueue{
154+
queue: list.New(),
155+
}
156+
157+
158+
t.Run("List Enqueue", func(t *testing.T) {
159+
listQueue.Enqueue("Snap")
160+
listQueue.Enqueue(123)
161+
listQueue.Enqueue(true)
162+
listQueue.Enqueue(212.545454)
163+
164+
if listQueue.Len() != 4{
165+
t.Errorf("List Enqueue is not correct expected %d but got %d",4, listQueue.Len())
166+
}
167+
})
168+
169+
170+
t.Run("List Dequeue", func(t *testing.T) {
171+
172+
173+
err := listQueue.Dequeue()
174+
175+
if err != nil{
176+
t.Error("got an unexpected error ", err)
177+
}
178+
if listQueue.Len() != 3{
179+
t.Errorf("List Dequeue is not correct expected %d but got %d",3, listQueue.Len())
180+
}
181+
})
182+
183+
t.Run("List Front", func(t *testing.T) {
184+
185+
186+
err := listQueue.Dequeue()
187+
188+
if err != nil{
189+
t.Error("got an unexpected error ", err)
190+
}
191+
192+
result , err := listQueue.Front()
193+
194+
if err != nil{
195+
t.Error("got an unexpected error ", err)
196+
}
197+
198+
if result != true{
199+
t.Errorf("List Front is not correct expected %v but got %v",true, result)
200+
}
201+
})
202+
203+
t.Run("List Back", func(t *testing.T) {
204+
205+
206+
207+
err := listQueue.Dequeue()
208+
209+
if err != nil{
210+
t.Error("got an unexpected error ", err)
211+
}
212+
213+
result , err := listQueue.Back()
214+
215+
if err != nil{
216+
t.Error("got an unexpected error ", err)
217+
}
218+
219+
if result != 212.545454{
220+
t.Errorf("List Back is not correct expected %v but got %v",212.545454, result)
221+
}
222+
})
223+
224+
225+
t.Run("List Length", func(t *testing.T) {
226+
227+
listQueue.Enqueue("Snap")
228+
229+
230+
err := listQueue.Dequeue()
231+
232+
if err != nil{
233+
t.Error("got an unexpected error ", err)
234+
}
235+
236+
if listQueue.Len() != 1{
237+
t.Errorf("List Length is not correct expected %v but got %v",1, listQueue.Len() )
238+
}
239+
})
240+
241+
242+
t.Run("List Empty", func(t *testing.T) {
243+
244+
err := listQueue.Dequeue()
245+
246+
if err != nil{
247+
t.Error("got an unexpected error ", err)
248+
}
249+
250+
if !listQueue.Empty(){
251+
t.Errorf("List Empty is not correct expected %v but got %v",true, listQueue.Empty() )
252+
}
253+
})
254+
255+
})
256+
257+
}

data_structures/queue/queuearray.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Queue Array
2+
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
3+
// The order is First In First Out (FIFO).
4+
// details:
5+
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
6+
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
7+
// author [Milad](https://github.com/miraddo)
8+
// see queuelinkedlist.go, queuelinkedlistwithlist.go, queue_test.go
9+
10+
package queue
11+
12+
13+
var ListQueue []interface{}
14+
15+
16+
// EnQueue it will be added new value into our list
17+
func EnQueue(n interface{}) {
18+
ListQueue = append(ListQueue, n)
19+
}
20+
21+
// DeQueue it will be removed the first value that added into the list
22+
func DeQueue() interface{} {
23+
data := ListQueue[0]
24+
ListQueue = ListQueue[1:]
25+
return data
26+
}
27+
28+
// FrontQueue return the Front value
29+
func FrontQueue() interface{} {
30+
return ListQueue[0]
31+
}
32+
33+
// BackQueue return the Back value
34+
func BackQueue() interface{} {
35+
return ListQueue[len(ListQueue)-1]
36+
}
37+
38+
// LenQueue will return the length of the queue list
39+
func LenQueue() int {
40+
return len(ListQueue)
41+
}
42+
43+
// IsEmptyQueue check our list is empty or not
44+
func IsEmptyQueue() bool {
45+
return len(ListQueue) == 0
46+
}

0 commit comments

Comments
 (0)