Replace poll with pop

This commit is contained in:
krahets 2023-03-13 22:39:45 +08:00
parent 28aacccf44
commit 516cb17775
6 changed files with 27 additions and 27 deletions

View file

@ -97,7 +97,7 @@ int peekLast(ArrayDeque *deque) {
} }
/* 队首出队 */ /* 队首出队 */
int pollFirst(ArrayDeque *deque) { int popFirst(ArrayDeque *deque) {
int num = peekFirst(deque); int num = peekFirst(deque);
// 队首指针向后移动一位 // 队首指针向后移动一位
deque->front = dequeIndex(deque, deque->front + 1); deque->front = dequeIndex(deque, deque->front + 1);
@ -106,7 +106,7 @@ int pollFirst(ArrayDeque *deque) {
} }
/* 队尾出队 */ /* 队尾出队 */
int pollLast(ArrayDeque *deque) { int popLast(ArrayDeque *deque) {
int num = peekLast(deque); int num = peekLast(deque);
deque->queSize--; deque->queSize--;
return num; return num;
@ -149,11 +149,11 @@ int main() {
printArrayDeque(deque); printArrayDeque(deque);
/* 元素出队 */ /* 元素出队 */
int pollLastNum = pollLast(deque); int popLastNum = popLast(deque);
printf("队尾出队元素 = %d队尾出队后 deque= ", pollLastNum); printf("队尾出队元素 = %d队尾出队后 deque= ", popLastNum);
printArrayDeque(deque); printArrayDeque(deque);
int pollFirstNum = pollFirst(deque); int popFirstNum = popFirst(deque);
printf("队首出队元素 = %d队首出队后 deque= ", pollFirstNum); printf("队首出队元素 = %d队首出队后 deque= ", popFirstNum);
printArrayDeque(deque); printArrayDeque(deque);
/* 获取队列的长度 */ /* 获取队列的长度 */

View file

@ -68,7 +68,7 @@ int peek(LinkedListQueue *queue) {
} }
/* 出队 */ /* 出队 */
void poll(LinkedListQueue *queue) { void pop(LinkedListQueue *queue) {
int num = peek(queue); int num = peek(queue);
ListNode *tmp = queue->front; ListNode *tmp = queue->front;
queue->front = queue->front->next; queue->front = queue->front->next;
@ -108,8 +108,8 @@ int main() {
printf("队首元素 peek = %d\r\n", peekNum); printf("队首元素 peek = %d\r\n", peekNum);
/* 元素出队 */ /* 元素出队 */
poll(queue); pop(queue);
printf("出队元素 poll = %d出队后 queue = ", peekNum); printf("出队元素 pop = %d出队后 queue = ", peekNum);
printLinkedListQueue(queue); printLinkedListQueue(queue);
/* 获取队列的长度 */ /* 获取队列的长度 */

View file

@ -70,7 +70,7 @@ func (q *arrayDeque) pushLast(num int) {
} }
/* 队首出队 */ /* 队首出队 */
func (q *arrayDeque) pollFirst() any { func (q *arrayDeque) popFirst() any {
num := q.peekFirst() num := q.peekFirst()
// 队首指针向后移动一位 // 队首指针向后移动一位
q.front = q.index(q.front + 1) q.front = q.index(q.front + 1)
@ -79,7 +79,7 @@ func (q *arrayDeque) pollFirst() any {
} }
/* 队尾出队 */ /* 队尾出队 */
func (q *arrayDeque) pollLast() any { func (q *arrayDeque) popLast() any {
num := q.peekLast() num := q.peekLast()
q.queSize-- q.queSize--
return num return num

View file

@ -76,11 +76,11 @@ func TestArrayDeque(t *testing.T) {
PrintSlice(deque.toSlice()) PrintSlice(deque.toSlice())
/* 元素出队 */ /* 元素出队 */
pollFirst := deque.pollFirst() popFirst := deque.popFirst()
fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ") fmt.Print("队首出队元素 popFirst = ", popFirst, ",队首出队后 deque = ")
PrintSlice(deque.toSlice()) PrintSlice(deque.toSlice())
pollLast := deque.pollLast() popLast := deque.popLast()
fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ") fmt.Print("队尾出队元素 popLast = ", popLast, ",队尾出队后 deque = ")
PrintSlice(deque.toSlice()) PrintSlice(deque.toSlice())
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */

View file

@ -99,7 +99,7 @@ impl<T: Copy> LinkedListDeque<T> {
} }
/* 出队操作 */ /* 出队操作 */
pub fn poll(&mut self, is_front: bool) -> Option<T> { pub fn pop(&mut self, is_front: bool) -> Option<T> {
if self.is_empty() {return None}; if self.is_empty() {return None};
// 队首出队操作 // 队首出队操作
if is_front { if is_front {
@ -135,13 +135,13 @@ impl<T: Copy> LinkedListDeque<T> {
} }
/* 队首出队 */ /* 队首出队 */
pub fn poll_first(&mut self) -> Option<T> { pub fn pop_first(&mut self) -> Option<T> {
return self.poll(true); return self.pop(true);
} }
/* 队尾出队 */ /* 队尾出队 */
pub fn poll_last(&mut self) -> Option<T> { pub fn pop_last(&mut self) -> Option<T> {
return self.poll(false); return self.pop(false);
} }
/* 访问队首元素 */ /* 访问队首元素 */
@ -190,11 +190,11 @@ fn main() {
print_util::print_array(&deque.to_array(deque.peek_first())); print_util::print_array(&deque.to_array(deque.peek_first()));
/* 元素出队 */ /* 元素出队 */
let poll_last = deque.poll_last().unwrap(); let pop_last = deque.pop_last().unwrap();
print!("\n队尾出队元素 = {},队尾出队后 deque = ", poll_last); print!("\n队尾出队元素 = {},队尾出队后 deque = ", pop_last);
print_util::print_array(&deque.to_array(deque.peek_first())); print_util::print_array(&deque.to_array(deque.peek_first()));
let poll_first = deque.poll_first().unwrap(); let pop_first = deque.pop_first().unwrap();
print!("\n队首出队元素 = {},队首出队后 deque = ", poll_first); print!("\n队首出队元素 = {},队首出队后 deque = ", pop_first);
print_util::print_array(&deque.to_array(deque.peek_first())); print_util::print_array(&deque.to_array(deque.peek_first()));
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */

View file

@ -57,7 +57,7 @@ impl<T: Copy> LinkedListQueue<T> {
} }
/* 出队 */ /* 出队 */
pub fn poll(&mut self) -> Option<T> { pub fn pop(&mut self) -> Option<T> {
self.front.take().map(|old_front| { self.front.take().map(|old_front| {
match old_front.borrow_mut().next.take() { match old_front.borrow_mut().next.take() {
Some(new_front) => { Some(new_front) => {
@ -107,8 +107,8 @@ fn main() {
print!("\n队首元素 peek = {}", peek); print!("\n队首元素 peek = {}", peek);
/* 元素出队 */ /* 元素出队 */
let poll = queue.poll().unwrap(); let pop = queue.pop().unwrap();
print!("\n出队元素 poll = {},出队后 queue = ", poll); print!("\n出队元素 pop = {},出队后 queue = ", pop);
print_util::print_array(&queue.to_array(queue.peek())); print_util::print_array(&queue.to_array(queue.peek()));
/* 获取队列的长度 */ /* 获取队列的长度 */