mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:46:29 +08:00
Replace poll with pop
This commit is contained in:
parent
28aacccf44
commit
516cb17775
6 changed files with 27 additions and 27 deletions
|
@ -97,7 +97,7 @@ int peekLast(ArrayDeque *deque) {
|
|||
}
|
||||
|
||||
/* 队首出队 */
|
||||
int pollFirst(ArrayDeque *deque) {
|
||||
int popFirst(ArrayDeque *deque) {
|
||||
int num = peekFirst(deque);
|
||||
// 队首指针向后移动一位
|
||||
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);
|
||||
deque->queSize--;
|
||||
return num;
|
||||
|
@ -149,11 +149,11 @@ int main() {
|
|||
printArrayDeque(deque);
|
||||
|
||||
/* 元素出队 */
|
||||
int pollLastNum = pollLast(deque);
|
||||
printf("队尾出队元素 = %d,队尾出队后 deque= ", pollLastNum);
|
||||
int popLastNum = popLast(deque);
|
||||
printf("队尾出队元素 = %d,队尾出队后 deque= ", popLastNum);
|
||||
printArrayDeque(deque);
|
||||
int pollFirstNum = pollFirst(deque);
|
||||
printf("队首出队元素 = %d,队首出队后 deque= ", pollFirstNum);
|
||||
int popFirstNum = popFirst(deque);
|
||||
printf("队首出队元素 = %d,队首出队后 deque= ", popFirstNum);
|
||||
printArrayDeque(deque);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
|
|
@ -68,7 +68,7 @@ int peek(LinkedListQueue *queue) {
|
|||
}
|
||||
|
||||
/* 出队 */
|
||||
void poll(LinkedListQueue *queue) {
|
||||
void pop(LinkedListQueue *queue) {
|
||||
int num = peek(queue);
|
||||
ListNode *tmp = queue->front;
|
||||
queue->front = queue->front->next;
|
||||
|
@ -108,8 +108,8 @@ int main() {
|
|||
printf("队首元素 peek = %d\r\n", peekNum);
|
||||
|
||||
/* 元素出队 */
|
||||
poll(queue);
|
||||
printf("出队元素 poll = %d,出队后 queue = ", peekNum);
|
||||
pop(queue);
|
||||
printf("出队元素 pop = %d,出队后 queue = ", peekNum);
|
||||
printLinkedListQueue(queue);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
|
|
@ -70,7 +70,7 @@ func (q *arrayDeque) pushLast(num int) {
|
|||
}
|
||||
|
||||
/* 队首出队 */
|
||||
func (q *arrayDeque) pollFirst() any {
|
||||
func (q *arrayDeque) popFirst() any {
|
||||
num := q.peekFirst()
|
||||
// 队首指针向后移动一位
|
||||
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()
|
||||
q.queSize--
|
||||
return num
|
||||
|
|
|
@ -76,11 +76,11 @@ func TestArrayDeque(t *testing.T) {
|
|||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* 元素出队 */
|
||||
pollFirst := deque.pollFirst()
|
||||
fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ")
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("队首出队元素 popFirst = ", popFirst, ",队首出队后 deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
pollLast := deque.pollLast()
|
||||
fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ")
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("队尾出队元素 popLast = ", popLast, ",队尾出队后 deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
|
|
|
@ -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 is_front {
|
||||
|
@ -135,13 +135,13 @@ impl<T: Copy> LinkedListDeque<T> {
|
|||
}
|
||||
|
||||
/* 队首出队 */
|
||||
pub fn poll_first(&mut self) -> Option<T> {
|
||||
return self.poll(true);
|
||||
pub fn pop_first(&mut self) -> Option<T> {
|
||||
return self.pop(true);
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
pub fn poll_last(&mut self) -> Option<T> {
|
||||
return self.poll(false);
|
||||
pub fn pop_last(&mut self) -> Option<T> {
|
||||
return self.pop(false);
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -190,11 +190,11 @@ fn main() {
|
|||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
|
||||
/* 元素出队 */
|
||||
let poll_last = deque.poll_last().unwrap();
|
||||
print!("\n队尾出队元素 = {},队尾出队后 deque = ", poll_last);
|
||||
let pop_last = deque.pop_last().unwrap();
|
||||
print!("\n队尾出队元素 = {},队尾出队后 deque = ", pop_last);
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
let poll_first = deque.poll_first().unwrap();
|
||||
print!("\n队首出队元素 = {},队首出队后 deque = ", poll_first);
|
||||
let pop_first = deque.pop_first().unwrap();
|
||||
print!("\n队首出队元素 = {},队首出队后 deque = ", pop_first);
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
|
|
|
@ -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| {
|
||||
match old_front.borrow_mut().next.take() {
|
||||
Some(new_front) => {
|
||||
|
@ -107,8 +107,8 @@ fn main() {
|
|||
print!("\n队首元素 peek = {}", peek);
|
||||
|
||||
/* 元素出队 */
|
||||
let poll = queue.poll().unwrap();
|
||||
print!("\n出队元素 poll = {},出队后 queue = ", poll);
|
||||
let pop = queue.pop().unwrap();
|
||||
print!("\n出队元素 pop = {},出队后 queue = ", pop);
|
||||
print_util::print_array(&queue.to_array(queue.peek()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
|
Loading…
Reference in a new issue