Skip to content

Commit

Permalink
refactor: Replace poll with pop in Queue and Deque (krahets#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets authored Mar 13, 2023
1 parent 2d17ee8 commit 8aebbaa
Show file tree
Hide file tree
Showing 77 changed files with 261 additions and 261 deletions.
10 changes: 5 additions & 5 deletions codes/c/chapter_stack_and_queue/array_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void push(ArrayQueue *queue, int num) {
queue->queSize++;
}

/* 出队 */
void poll(ArrayQueue *queue) {
/* 出队 */
void pop(ArrayQueue *queue) {
int num = peek(queue);
// 队首指针向后移动一位,若越过尾部则返回到数组头部
queue->front = (queue->front + 1) % queue->queCapacity;
Expand Down Expand Up @@ -106,8 +106,8 @@ int main() {
printf("队首元素 peek = %d\r\n", peekNum);

/* 元素出队 */
poll(queue);
printf("出队元素 poll = %d,出队后 queue = ", peekNum);
pop(queue);
printf("出队元素 pop = %d,出队后 queue = ", peekNum);
printArrayQueue(queue);

/* 获取队列的长度 */
Expand All @@ -121,7 +121,7 @@ int main() {
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
push(queue, i);
poll(queue);
pop(queue);
printf("第 %d 轮入队 + 出队后 queue = ", i);
printArrayQueue(queue);
}
Expand Down
12 changes: 6 additions & 6 deletions codes/cpp/chapter_stack_and_queue/array_deque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ArrayDeque {
}

/* 队首出队 */
int pollFirst() {
int popFirst() {
int num = peekFirst();
// 队首指针向后移动一位
front = index(front + 1);
Expand All @@ -81,7 +81,7 @@ class ArrayDeque {
}

/* 队尾出队 */
int pollLast() {
int popLast() {
int num = peekLast();
queSize--;
return num;
Expand Down Expand Up @@ -139,11 +139,11 @@ int main() {
PrintUtil::printVector(deque->toVector());

/* 元素出队 */
int pollLast = deque->pollLast();
cout << "队尾出队元素 = " << pollLast << ",队尾出队后 deque = ";
int popLast = deque->popLast();
cout << "队尾出队元素 = " << popLast << ",队尾出队后 deque = ";
PrintUtil::printVector(deque->toVector());
int pollFirst = deque->pollFirst();
cout << "队首出队元素 = " << pollFirst << ",队首出队后 deque = ";
int popFirst = deque->popFirst();
cout << "队首出队元素 = " << popFirst << ",队首出队后 deque = ";
PrintUtil::printVector(deque->toVector());

/* 获取双向队列的长度 */
Expand Down
8 changes: 4 additions & 4 deletions codes/cpp/chapter_stack_and_queue/array_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ArrayQueue {
}

/* 出队 */
void poll() {
void pop() {
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % queCapacity;
Expand Down Expand Up @@ -102,8 +102,8 @@ int main() {
cout << "队首元素 peek = " << peek << endl;

/* 元素出队 */
queue->poll();
cout << "出队元素 poll = " << peek << ",出队后 queue = ";
queue->pop();
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
PrintUtil::printVector(queue->toVector());

/* 获取队列的长度 */
Expand All @@ -117,7 +117,7 @@ int main() {
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
queue->push(i);
queue->poll();
queue->pop();
cout << "" << i << " 轮入队 + 出队后 queue = ";
PrintUtil::printVector(queue->toVector());
}
Expand Down
18 changes: 9 additions & 9 deletions codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LinkedListDeque {
}

/* 出队操作 */
int poll(bool isFront) {
int pop(bool isFront) {
// 若队列为空,直接返回 -1
if (isEmpty())
return -1;
Expand Down Expand Up @@ -110,13 +110,13 @@ class LinkedListDeque {
}

/* 队首出队 */
int pollFirst() {
return poll(true);
int popFirst() {
return pop(true);
}

/* 队尾出队 */
int pollLast() {
return poll(false);
int popLast() {
return pop(false);
}

/* 访问队首元素 */
Expand Down Expand Up @@ -166,11 +166,11 @@ int main() {
PrintUtil::printVector(deque->toVector());

/* 元素出队 */
int pollLast = deque->pollLast();
cout << "队尾出队元素 = " << pollLast << ",队尾出队后 deque = ";
int popLast = deque->popLast();
cout << "队尾出队元素 = " << popLast << ",队尾出队后 deque = ";
PrintUtil::printVector(deque->toVector());
int pollFirst = deque->pollFirst();
cout << "队首出队元素 = " << pollFirst << ",队首出队后 deque = ";
int popFirst = deque->popFirst();
cout << "队首出队元素 = " << popFirst << ",队首出队后 deque = ";
PrintUtil::printVector(deque->toVector());

/* 获取双向队列的长度 */
Expand Down
6 changes: 3 additions & 3 deletions codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LinkedListQueue {
}

/* 出队 */
void poll() {
void pop() {
int num = peek();
// 删除头结点
ListNode *tmp = front;
Expand Down Expand Up @@ -101,8 +101,8 @@ int main() {
cout << "队首元素 peek = " << peek << endl;

/* 元素出队 */
queue->poll();
cout << "出队元素 poll = " << peek << ",出队后 queue = ";
queue->pop();
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
PrintUtil::printVector(queue->toVector());

/* 获取队列的长度 */
Expand Down
12 changes: 6 additions & 6 deletions codes/csharp/chapter_stack_and_queue/array_deque.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void pushLast(int num)
}

/* 队首出队 */
public int pollFirst()
public int popFirst()
{
int num = peekFirst();
// 队首指针向后移动一位
Expand All @@ -91,7 +91,7 @@ public int pollFirst()
}

/* 队尾出队 */
public int pollLast()
public int popLast()
{
int num = peekLast();
queSize--;
Expand Down Expand Up @@ -158,10 +158,10 @@ public void Test()
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));

/* 元素出队 */
int pollLast = deque.pollLast();
Console.WriteLine("队尾出队元素 = " + pollLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int pollFirst = deque.pollFirst();
Console.WriteLine("队首出队元素 = " + pollFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
int popLast = deque.popLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int popFirst = deque.popFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));

/* 获取双向队列的长度 */
int size = deque.size();
Expand Down
8 changes: 4 additions & 4 deletions codes/csharp/chapter_stack_and_queue/array_queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void push(int num)
}

/* 出队 */
public int poll()
public int pop()
{
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
Expand Down Expand Up @@ -108,8 +108,8 @@ public void Test()
Console.WriteLine("队首元素 peek = " + peek);

/* 元素出队 */
int poll = queue.poll();
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + string.Join(",", queue.toArray()));
int pop = queue.pop();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.toArray()));

/* 获取队列的长度 */
int size = queue.size();
Expand All @@ -123,7 +123,7 @@ public void Test()
for (int i = 0; i < 10; i++)
{
queue.push(i);
queue.poll();
queue.pop();
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
}
}
Expand Down
18 changes: 9 additions & 9 deletions codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void pushLast(int num)
}

/* 出队操作 */
private int? poll(bool isFront)
private int? pop(bool isFront)
{
// 若队列为空,直接返回 null
if (isEmpty())
Expand Down Expand Up @@ -133,15 +133,15 @@ public void pushLast(int num)
}

/* 队首出队 */
public int? pollFirst()
public int? popFirst()
{
return poll(true);
return pop(true);
}

/* 队尾出队 */
public int? pollLast()
public int? popLast()
{
return poll(false);
return pop(false);
}

/* 访问队首元素 */
Expand Down Expand Up @@ -196,10 +196,10 @@ public void Test()
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));

/* 元素出队 */
int? pollLast = deque.pollLast();
Console.WriteLine("队尾出队元素 = " + pollLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int? pollFirst = deque.pollFirst();
Console.WriteLine("队首出队元素 = " + pollFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
int? popLast = deque.popLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int? popFirst = deque.popFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));

/* 获取双向队列的长度 */
int size = deque.size();
Expand Down
6 changes: 3 additions & 3 deletions codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void push(int num)
}

/* 出队 */
public int poll()
public int pop()
{
int num = peek();
// 删除头结点
Expand Down Expand Up @@ -109,8 +109,8 @@ public void Test()
Console.WriteLine("队首元素 peek = " + peek);

/* 元素出队 */
int poll = queue.poll();
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.toArray()));
int pop = queue.pop();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + String.Join(",", queue.toArray()));

/* 获取队列的长度 */
int size = queue.size();
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_stack_and_queue/queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void Test()
Console.WriteLine("队首元素 peek = " + peek);

/* 元素出队 */
int poll = queue.Dequeue();
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.ToArray()));
int pop = queue.Dequeue();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + String.Join(",", queue.ToArray()));

/* 获取队列的长度 */
int size = queue.Count();
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_stack_and_queue/array_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (q *arrayQueue) push(num int) {
}

/* 出队 */
func (q *arrayQueue) poll() any {
func (q *arrayQueue) pop() any {
num := q.peek()
// 队首指针向后移动一位,若越过尾部则返回到数组头部
q.front = (q.front + 1) % q.queCapacity
Expand Down
10 changes: 5 additions & 5 deletions codes/go/chapter_stack_and_queue/deque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func TestLinkedListDeque(t *testing.T) {
fmt.Println("队尾元素 rear =", rear)

// 元素出队
pollFirst := deque.pollFirst()
fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ")
popFirst := deque.popFirst()
fmt.Print("队首出队元素 popFirst = ", popFirst, ",队首出队后 deque = ")
PrintList(deque.toList())
pollLast := deque.pollLast()
fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ")
popLast := deque.popLast()
fmt.Print("队尾出队元素 popLast = ", popLast, ",队尾出队后 deque = ")
PrintList(deque.toList())

// 获取队的长度
Expand All @@ -136,6 +136,6 @@ func BenchmarkLinkedListDeque(b *testing.B) {
deque.pushLast(777)
}
for i := 0; i < b.N; i++ {
deque.pollFirst()
deque.popFirst()
}
}
4 changes: 2 additions & 2 deletions codes/go/chapter_stack_and_queue/linkedlist_deque.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *linkedListDeque) pushLast(value any) {
}

/* 队首元素出队 */
func (s *linkedListDeque) pollFirst() any {
func (s *linkedListDeque) popFirst() any {
if s.isEmpty() {
return nil
}
Expand All @@ -42,7 +42,7 @@ func (s *linkedListDeque) pollFirst() any {
}

/* 队尾元素出队 */
func (s *linkedListDeque) pollLast() any {
func (s *linkedListDeque) popLast() any {
if s.isEmpty() {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_stack_and_queue/linkedlist_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *linkedListQueue) push(value any) {
}

/* 出队 */
func (s *linkedListQueue) poll() any {
func (s *linkedListQueue) pop() any {
if s.isEmpty() {
return nil
}
Expand Down
Loading

0 comments on commit 8aebbaa

Please sign in to comment.