抵扣说明:
1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。
Original: https://blog.csdn.net/qq_34465338/article/details/128333290
Author: 若博豆
Title: 【华为OD机试真题23 JAVA】单词倒序
相关阅读
Title: 数据结构——队列和栈
1. 简介
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除(出队)操作,而在表的后端(rear)进行插入(入队)操作。

堆栈,也称为堆栈,是一个具有有限操作(如队列)的线性表。限定仅在表格末尾插入和删除的线性表格。这一端被称为堆栈的顶部,相反,另一端被称为堆栈的底部。与先进先出队列相比,堆栈的运行模式是后进先出。
[En]
Stack, also known as stack, is a linear table with limited operations like queues. Qualifies linear tables that insert and delete only at the end of the table. This end is called the top of the stack, on the contrary, the other end is called the bottom of the stack. Compared with the first-in-first-out queue, the mode of operation of the stack is LIFO.

2.具体实现
栈和链表属于线性表,意味着它可以基于数组和链表来实现,它们和普通线性表相比只是在增加和删除元素时只能在首尾操作,我们只用提供push和pop接口,实现起来十分简单。
在这里,我们使用本机列表来实现,事实上,使用我们之前实现的动态数组和链表没有问题。
[En]
Here we use native lists to implement, in fact, there is no problem with using the dynamic arrays and linked lists we implemented earlier.
Python:
class Stack:
def __init__(self,*args):
self.data = []
def push(self,item):
self.data.append(item)
def pop(self):
item = self.data.pop()
return item
class Queue:
def __init__(self,*args):
self.data = []
def push(self,item):
self.data.append(item)
def pop(self):
item = self.data.pop(0)
return item
C#:
class Queue
{
private List data;
public Queue()
{
data = new List();
}
public void push(T item)
{
data.Add(item);
}
public T pop()
{
T res = data[0];
data.RemoveAt(0);
return res;
}
}
class Stack
{
private List data;
public Stack()
{
data = new List();
}
public void push(T item)
{
data.Add(item);
}
public T pop()
{
T res = data[data.Count - 1];
data.RemoveAt(data.Count - 1);
return res;
}
}
3. 队列和栈的更多形式
队列除了先进先出外,也有循环队列、后进先出队列和优先队列。
环形队列一般基于环形数组,在添加或删除元素时可以节省不必要的空间和时间浪费。
[En]
Circular queues are generally based on circular arrays, which can save unnecessary space and time waste when adding or deleting elements.
实际上,后进先出队列与堆栈实现是一样的,从这个角度来看,堆栈无疑是一种队列。
[En]
In fact, the LIFO queue is the same as the stack implementation, from this point of view, the stack is undoubtedly a kind of queue.
实现了优先级队列,根据加入队列的权重,离开队列始终是队列中权重最大的元素,优先级队列可以基于最大堆来实现。我还将在数据结构中继这一系列博客帖子之后,再写一篇堆最大和最小的博客帖子。
[En]
Priority queue is realized that according to the weight of joining the queue, leaving the queue is always the most weighted element in the queue, and the priority queue can be implemented based on the maximum heap. I will also write another blog post with the largest and smallest pile after the follow-up of this series of blog posts in the data structure.
Original: https://www.cnblogs.com/lazyfish007/p/11835817.html
Author: 秋叶红了
Title: 数据结构——队列和栈
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/356125/
转载文章受原作者版权保护。转载请注明原作者出处!