-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack-to-queue.go
75 lines (61 loc) · 1.27 KB
/
stack-to-queue.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
74
75
package stack
import "fmt"
type Queue struct {
queue []interface{}
}
/** Initialize your data structure here. */
func ConstructorQueue() Queue {
return Queue{
queue: []interface{}{},
}
}
/** Push element x to the back of queue. */
func (this *Queue) Push(x interface{}) {
this.queue = append(this.queue, x)
}
/** Removes the element from in front of queue and returns that element. */
func (this *Queue) Pop() interface{} {
size := len(this.queue)
if size == 0 {
return nil
} else {
res := this.queue[0]
this.queue = append([]interface{}{}, this.queue[1:]...)
return res
}
}
/** Get the front element. */
func (this *Queue) Peek() interface{} {
size := len(this.queue)
if size == 0 {
return nil
} else {
return this.queue[0]
}
}
/** Returns whether the queue is empty. */
func (this *Queue) Empty() bool {
if len(this.queue) == 0 {
return true
} else {
return false
}
}
/**
* Your Queue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/
func testQueue() {
obj := ConstructorQueue()
obj.Push("12")
param_2 := obj.Pop()
param_3 := obj.Peek()
param_4 := obj.Empty()
fmt.Println(param_2)
fmt.Println(param_3)
fmt.Println(param_4)
}