Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 12ccda3

Browse files
committed
232 利用 container/list.List 重新实现了代码。close #37
1 parent 19832ea commit 12ccda3

File tree

3 files changed

+36
-97
lines changed

3 files changed

+36
-97
lines changed

Algorithms/0232.implement-queue-using-stacks/README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44

55
Implement the following operations of a queue using stacks.
66

7+
- push(x) -- Push element x to the back of queue.
8+
- pop() -- Removes the element from in front of queue.
9+
- peek() -- Get the front element.
10+
- empty() -- Return whether the queue is empty.
711

8-
push(x) -- Push element x to the back of queue.
12+
Example:
913

14+
```text
15+
MyQueue queue = new MyQueue();
1016
11-
pop() -- Removes the element from in front of queue.
12-
13-
14-
peek() -- Get the front element.
15-
16-
17-
empty() -- Return whether the queue is empty.
18-
17+
queue.push(1);
18+
queue.push(2);
19+
queue.peek(); // returns 1
20+
queue.pop(); // returns 1
21+
queue.empty(); // returns false
22+
```
1923

2024
Notes:
2125

22-
You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
23-
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
24-
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
25-
26-
## 解题思路
27-
28-
见程序注释
26+
- You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
27+
- Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
28+
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,40 @@
11
package problem0232
22

3-
// MyQueue 是利用 栈 实现的队列
3+
import "container/list"
4+
5+
// MyQueue 是利用 list 实现的队列
46
type MyQueue struct {
5-
a, b *Stack
7+
list *list.List
68
}
79

810
// Constructor Initialize your data structure here.
911
func Constructor() MyQueue {
10-
return MyQueue{a: NewStack(), b: NewStack()}
12+
return MyQueue{
13+
list: list.New(),
14+
}
1115
}
1216

1317
// Push element x to the back of queue.
14-
func (mq *MyQueue) Push(x int) {
15-
mq.a.Push(x)
18+
func (q *MyQueue) Push(x int) {
19+
q.list.PushBack(x)
1620
}
1721

1822
// Pop Removes the element from in front of queue and returns that element.
19-
func (mq *MyQueue) Pop() int {
20-
if mq.b.Len() == 0 {
21-
for mq.a.Len() > 0 {
22-
mq.b.Push(mq.a.Pop())
23-
}
24-
}
25-
26-
return mq.b.Pop()
23+
func (q *MyQueue) Pop() int {
24+
front := q.list.Front()
25+
res := front.Value.(int)
26+
q.list.Remove(front)
27+
return res
2728
}
2829

2930
// Peek Get the front element.
30-
func (mq *MyQueue) Peek() int {
31-
res := mq.Pop()
32-
mq.b.Push(res)
31+
func (q *MyQueue) Peek() int {
32+
front := q.list.Front()
33+
res := front.Value.(int)
3334
return res
3435
}
3536

3637
// Empty returns whether the queue is empty.
37-
func (mq *MyQueue) Empty() bool {
38-
return mq.a.Len() == 0 && mq.b.Len() == 0
39-
}
40-
41-
/**
42-
* Your MyQueue object will be instantiated and called as such:
43-
* obj := Constructor();
44-
* obj.Push(x);
45-
* param_2 := obj.Pop();
46-
* param_3 := obj.Peek();
47-
* param_4 := obj.Empty();
48-
*/
49-
50-
// Stack 是用于存放 int 的 栈
51-
type Stack struct {
52-
nums []int
53-
}
54-
55-
// NewStack 返回 *kit.Stack
56-
func NewStack() *Stack {
57-
return &Stack{nums: []int{}}
58-
}
59-
60-
// Push 把 n 放入 栈
61-
func (s *Stack) Push(n int) {
62-
s.nums = append(s.nums, n)
63-
}
64-
65-
// Pop 从 s 中取出最后放入 栈 的值
66-
func (s *Stack) Pop() int {
67-
res := s.nums[len(s.nums)-1]
68-
s.nums = s.nums[:len(s.nums)-1]
69-
return res
70-
}
71-
72-
// Len 返回 s 的长度
73-
func (s *Stack) Len() int {
74-
return len(s.nums)
75-
}
76-
77-
// IsEmpty 反馈 s 是否为空
78-
func (s *Stack) IsEmpty() bool {
79-
return s.Len() == 0
38+
func (q *MyQueue) Empty() bool {
39+
return q.list.Len() == 0
8040
}

Algorithms/0232.implement-queue-using-stacks/implement-queue-using-stacks_test.go

+1-22
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,10 @@ func Test_MyQueue(t *testing.T) {
1616

1717
for i := start; i < end; i++ {
1818
q.Push(i)
19-
ast.Equal(start , q.Peek(), "查看 q.Peek()")
19+
ast.Equal(start, q.Peek(), "查看 q.Peek()")
2020
}
2121

2222
for i := start; i < end; i++ {
2323
ast.Equal(i, q.Pop(), "从 q 中 pop 出数来。")
2424
}
25-
26-
ast.True(q.Empty(), "检查 Pop 完毕后的 q 是否为空")
27-
}
28-
func Test_Stack(t *testing.T) {
29-
ast := assert.New(t)
30-
31-
s := NewStack()
32-
ast.True(s.IsEmpty(), "检查新建的 s 是否为空")
33-
34-
start, end := 0, 100
35-
36-
for i := start; i < end; i++ {
37-
s.Push(i)
38-
ast.Equal(i-start+1, s.Len(), "Push 后检查 q 的长度。")
39-
}
40-
41-
for i := end - 1; i >= start; i-- {
42-
ast.Equal(i, s.Pop(), "从 s 中 pop 出数来。")
43-
}
44-
45-
ast.True(s.IsEmpty(), "检查 Pop 完毕后的 s 是否为空")
4625
}

0 commit comments

Comments
 (0)