|
|
@ -144,8 +144,7 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```js title="queue.js"
|
|
|
|
```js title="queue.js"
|
|
|
|
/* 初始化队列 */
|
|
|
|
/* 初始化队列 */
|
|
|
|
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
|
|
|
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
|
|
|
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
|
|
|
|
|
|
|
const queue = [];
|
|
|
|
const queue = [];
|
|
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
/* 元素入队 */
|
|
|
@ -159,7 +158,7 @@ comments: true
|
|
|
|
const peek = queue[0];
|
|
|
|
const peek = queue[0];
|
|
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
/* 元素出队 */
|
|
|
|
// O(n)
|
|
|
|
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
|
|
|
const poll = queue.shift();
|
|
|
|
const poll = queue.shift();
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
@ -174,7 +173,6 @@ comments: true
|
|
|
|
```typescript title="queue.ts"
|
|
|
|
```typescript title="queue.ts"
|
|
|
|
/* 初始化队列 */
|
|
|
|
/* 初始化队列 */
|
|
|
|
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
|
|
|
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
|
|
|
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
|
|
|
|
|
|
|
const queue: number[] = [];
|
|
|
|
const queue: number[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
/* 元素入队 */
|
|
|
@ -188,7 +186,7 @@ comments: true
|
|
|
|
const peek = queue[0];
|
|
|
|
const peek = queue[0];
|
|
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
/* 元素出队 */
|
|
|
|
// O(n)
|
|
|
|
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
|
|
|
const poll = queue.shift();
|
|
|
|
const poll = queue.shift();
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
@ -382,19 +380,16 @@ comments: true
|
|
|
|
// 使用内置包 list 来实现队列
|
|
|
|
// 使用内置包 list 来实现队列
|
|
|
|
data *list.List
|
|
|
|
data *list.List
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewLinkedListQueue 初始化链表
|
|
|
|
// NewLinkedListQueue 初始化链表
|
|
|
|
func NewLinkedListQueue() *LinkedListQueue {
|
|
|
|
func NewLinkedListQueue() *LinkedListQueue {
|
|
|
|
return &LinkedListQueue{
|
|
|
|
return &LinkedListQueue{
|
|
|
|
data: list.New(),
|
|
|
|
data: list.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Offer 入队
|
|
|
|
// Offer 入队
|
|
|
|
func (s *LinkedListQueue) Offer(value any) {
|
|
|
|
func (s *LinkedListQueue) Offer(value any) {
|
|
|
|
s.data.PushBack(value)
|
|
|
|
s.data.PushBack(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Poll 出队
|
|
|
|
// Poll 出队
|
|
|
|
func (s *LinkedListQueue) Poll() any {
|
|
|
|
func (s *LinkedListQueue) Poll() any {
|
|
|
|
if s.IsEmpty() {
|
|
|
|
if s.IsEmpty() {
|
|
|
@ -404,7 +399,6 @@ comments: true
|
|
|
|
s.data.Remove(e)
|
|
|
|
s.data.Remove(e)
|
|
|
|
return e.Value
|
|
|
|
return e.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Peek 访问队首元素
|
|
|
|
// Peek 访问队首元素
|
|
|
|
func (s *LinkedListQueue) Peek() any {
|
|
|
|
func (s *LinkedListQueue) Peek() any {
|
|
|
|
if s.IsEmpty() {
|
|
|
|
if s.IsEmpty() {
|
|
|
@ -413,12 +407,10 @@ comments: true
|
|
|
|
e := s.data.Front()
|
|
|
|
e := s.data.Front()
|
|
|
|
return e.Value
|
|
|
|
return e.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Size 获取队列的长度
|
|
|
|
// Size 获取队列的长度
|
|
|
|
func (s *LinkedListQueue) Size() int {
|
|
|
|
func (s *LinkedListQueue) Size() int {
|
|
|
|
return s.data.Len()
|
|
|
|
return s.data.Len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IsEmpty 判断队列是否为空
|
|
|
|
// IsEmpty 判断队列是否为空
|
|
|
|
func (s *LinkedListQueue) IsEmpty() bool {
|
|
|
|
func (s *LinkedListQueue) IsEmpty() bool {
|
|
|
|
return s.data.Len() == 0
|
|
|
|
return s.data.Len() == 0
|
|
|
@ -428,26 +420,23 @@ comments: true
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```js title="linkedlist_queue.js"
|
|
|
|
```js title="linkedlist_queue.js"
|
|
|
|
|
|
|
|
/* 基于链表实现的队列 */
|
|
|
|
class LinkedListQueue {
|
|
|
|
class LinkedListQueue {
|
|
|
|
#front;
|
|
|
|
#front; // 头结点 #front
|
|
|
|
#rear; // 头结点 #front ,尾结点 #rear
|
|
|
|
#rear; // 尾结点 #rear
|
|
|
|
#queSize = 0;
|
|
|
|
#queSize = 0;
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
constructor() {
|
|
|
|
this.#front = null;
|
|
|
|
this.#front = null;
|
|
|
|
this.#rear = null;
|
|
|
|
this.#rear = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
get size() {
|
|
|
|
get size() {
|
|
|
|
return this.#queSize;
|
|
|
|
return this.#queSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
isEmpty() {
|
|
|
|
isEmpty() {
|
|
|
|
return this.size === 0;
|
|
|
|
return this.size === 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
offer(num) {
|
|
|
|
offer(num) {
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
@ -463,36 +452,20 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.#queSize++;
|
|
|
|
this.#queSize++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
poll() {
|
|
|
|
poll() {
|
|
|
|
const num = this.peek();
|
|
|
|
const num = this.peek();
|
|
|
|
if (!this.#front) {
|
|
|
|
|
|
|
|
throw new Error("No element in queue!")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除头结点
|
|
|
|
// 删除头结点
|
|
|
|
this.#front = this.#front.next;
|
|
|
|
this.#front = this.#front.next;
|
|
|
|
this.#queSize--;
|
|
|
|
this.#queSize--;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek() {
|
|
|
|
peek() {
|
|
|
|
if (this.size === 0)
|
|
|
|
if (this.size === 0)
|
|
|
|
throw new Error("No element in queue!");
|
|
|
|
throw new Error("队列为空");
|
|
|
|
return this.#front.val;
|
|
|
|
return this.#front.val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 将链表转化为 Array 并返回 */
|
|
|
|
|
|
|
|
toArray() {
|
|
|
|
|
|
|
|
let node = this.#front;
|
|
|
|
|
|
|
|
const res = new Array(this.size);
|
|
|
|
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
|
|
|
|
res[i] = node.val;
|
|
|
|
|
|
|
|
node = node.next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -501,25 +474,21 @@ comments: true
|
|
|
|
```typescript title="linkedlist_queue.ts"
|
|
|
|
```typescript title="linkedlist_queue.ts"
|
|
|
|
/* 基于链表实现的队列 */
|
|
|
|
/* 基于链表实现的队列 */
|
|
|
|
class LinkedListQueue {
|
|
|
|
class LinkedListQueue {
|
|
|
|
private front: ListNode | null;
|
|
|
|
private front: ListNode | null; // 头结点 front
|
|
|
|
private rear: ListNode | null; // 头结点 front ,尾结点 rear
|
|
|
|
private rear: ListNode | null; // 尾结点 rear
|
|
|
|
private queSize: number = 0;
|
|
|
|
private queSize: number = 0;
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
constructor() {
|
|
|
|
this.front = null;
|
|
|
|
this.front = null;
|
|
|
|
this.rear = null;
|
|
|
|
this.rear = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
get size(): number {
|
|
|
|
get size(): number {
|
|
|
|
return this.queSize;
|
|
|
|
return this.queSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
isEmpty(): boolean {
|
|
|
|
isEmpty(): boolean {
|
|
|
|
return this.size === 0;
|
|
|
|
return this.size === 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
offer(num: number): void {
|
|
|
|
offer(num: number): void {
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
@ -535,36 +504,22 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.queSize++;
|
|
|
|
this.queSize++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
poll(): number {
|
|
|
|
poll(): number {
|
|
|
|
const num = this.peek();
|
|
|
|
const num = this.peek();
|
|
|
|
if (!this.front) {
|
|
|
|
if (!this.front)
|
|
|
|
throw new Error("No element in queue!")
|
|
|
|
throw new Error("队列为空")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除头结点
|
|
|
|
// 删除头结点
|
|
|
|
this.front = this.front.next;
|
|
|
|
this.front = this.front.next;
|
|
|
|
this.queSize--;
|
|
|
|
this.queSize--;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek(): number {
|
|
|
|
peek(): number {
|
|
|
|
if (this.size === 0)
|
|
|
|
if (this.size === 0)
|
|
|
|
throw new Error("No element in queue!");
|
|
|
|
throw new Error("队列为空");
|
|
|
|
return this.front!.val;
|
|
|
|
return this.front!.val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 将链表转化为 Array 并返回 */
|
|
|
|
|
|
|
|
toArray(): number[] {
|
|
|
|
|
|
|
|
let node = this.front;
|
|
|
|
|
|
|
|
const res = new Array<number>(this.size);
|
|
|
|
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
|
|
|
|
res[i] = node!.val;
|
|
|
|
|
|
|
|
node = node!.next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -637,7 +592,6 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
public int peek() {
|
|
|
|
public int peek() {
|
|
|
|
// 删除头结点
|
|
|
|
|
|
|
|
if (isEmpty())
|
|
|
|
if (isEmpty())
|
|
|
|
throw new EmptyStackException();
|
|
|
|
throw new EmptyStackException();
|
|
|
|
return nums[front];
|
|
|
|
return nums[front];
|
|
|
@ -701,7 +655,6 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
int peek() {
|
|
|
|
int peek() {
|
|
|
|
// 删除头结点
|
|
|
|
|
|
|
|
if (empty())
|
|
|
|
if (empty())
|
|
|
|
throw out_of_range("队列为空");
|
|
|
|
throw out_of_range("队列为空");
|
|
|
|
return nums[front];
|
|
|
|
return nums[front];
|
|
|
@ -750,7 +703,6 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
""" 出队 """
|
|
|
|
""" 出队 """
|
|
|
|
def poll(self):
|
|
|
|
def poll(self):
|
|
|
|
# 删除头结点
|
|
|
|
|
|
|
|
num = self.peek()
|
|
|
|
num = self.peek()
|
|
|
|
# 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
# 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
self.__front = (self.__front + 1) % self.capacity()
|
|
|
|
self.__front = (self.__front + 1) % self.capacity()
|
|
|
@ -758,7 +710,6 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
""" 访问队首元素 """
|
|
|
|
""" 访问队首元素 """
|
|
|
|
def peek(self):
|
|
|
|
def peek(self):
|
|
|
|
# 删除头结点
|
|
|
|
|
|
|
|
if self.is_empty():
|
|
|
|
if self.is_empty():
|
|
|
|
print("队列为空")
|
|
|
|
print("队列为空")
|
|
|
|
return False
|
|
|
|
return False
|
|
|
@ -791,7 +742,6 @@ comments: true
|
|
|
|
front int // 头指针,指向队首
|
|
|
|
front int // 头指针,指向队首
|
|
|
|
rear int // 尾指针,指向队尾 + 1
|
|
|
|
rear int // 尾指针,指向队尾 + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewArrayQueue 基于环形数组实现的队列
|
|
|
|
// NewArrayQueue 基于环形数组实现的队列
|
|
|
|
func NewArrayQueue(capacity int) *ArrayQueue {
|
|
|
|
func NewArrayQueue(capacity int) *ArrayQueue {
|
|
|
|
return &ArrayQueue{
|
|
|
|
return &ArrayQueue{
|
|
|
@ -801,18 +751,15 @@ comments: true
|
|
|
|
rear: 0,
|
|
|
|
rear: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Size 获取队列的长度
|
|
|
|
// Size 获取队列的长度
|
|
|
|
func (q *ArrayQueue) Size() int {
|
|
|
|
func (q *ArrayQueue) Size() int {
|
|
|
|
size := (q.capacity + q.rear - q.front) % q.capacity
|
|
|
|
size := (q.capacity + q.rear - q.front) % q.capacity
|
|
|
|
return size
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IsEmpty 判断队列是否为空
|
|
|
|
// IsEmpty 判断队列是否为空
|
|
|
|
func (q *ArrayQueue) IsEmpty() bool {
|
|
|
|
func (q *ArrayQueue) IsEmpty() bool {
|
|
|
|
return q.rear-q.front == 0
|
|
|
|
return q.rear-q.front == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Offer 入队
|
|
|
|
// Offer 入队
|
|
|
|
func (q *ArrayQueue) Offer(v int) {
|
|
|
|
func (q *ArrayQueue) Offer(v int) {
|
|
|
|
// 当 rear == capacity 表示队列已满
|
|
|
|
// 当 rear == capacity 表示队列已满
|
|
|
@ -824,7 +771,6 @@ comments: true
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
q.rear = (q.rear + 1) % q.capacity
|
|
|
|
q.rear = (q.rear + 1) % q.capacity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Poll 出队
|
|
|
|
// Poll 出队
|
|
|
|
func (q *ArrayQueue) Poll() any {
|
|
|
|
func (q *ArrayQueue) Poll() any {
|
|
|
|
if q.IsEmpty() {
|
|
|
|
if q.IsEmpty() {
|
|
|
@ -835,7 +781,6 @@ comments: true
|
|
|
|
q.front = (q.front + 1) % q.capacity
|
|
|
|
q.front = (q.front + 1) % q.capacity
|
|
|
|
return v
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Peek 访问队首元素
|
|
|
|
// Peek 访问队首元素
|
|
|
|
func (q *ArrayQueue) Peek() any {
|
|
|
|
func (q *ArrayQueue) Peek() any {
|
|
|
|
if q.IsEmpty() {
|
|
|
|
if q.IsEmpty() {
|
|
|
@ -851,76 +796,52 @@ comments: true
|
|
|
|
```js title="array_queue.js"
|
|
|
|
```js title="array_queue.js"
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
queue; // 用于存储队列元素的数组
|
|
|
|
#queue; // 用于存储队列元素的数组
|
|
|
|
front = 0; // 头指针,指向队首
|
|
|
|
#front = 0; // 头指针,指向队首
|
|
|
|
rear = 0; // 尾指针,指向队尾 + 1
|
|
|
|
#rear = 0; // 尾指针,指向队尾 + 1
|
|
|
|
CAPACITY = 1e5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(capacity) {
|
|
|
|
constructor(capacity) {
|
|
|
|
this.queue = new Array(capacity ?? this.CAPACITY);
|
|
|
|
this.#queue = new Array(capacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
get capacity() {
|
|
|
|
get capacity() {
|
|
|
|
return this.queue.length;
|
|
|
|
return this.#queue.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
get size() {
|
|
|
|
get size() {
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
return (this.capacity + this.rear - this.front) % this.capacity;
|
|
|
|
return (this.capacity + this.#rear - this.#front) % this.capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
empty() {
|
|
|
|
empty() {
|
|
|
|
return this.rear - this.front == 0;
|
|
|
|
return this.#rear - this.#front == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
offer(num) {
|
|
|
|
offer(num) {
|
|
|
|
if (this.size == this.capacity) {
|
|
|
|
if (this.size == this.capacity)
|
|
|
|
console.log("队列已满");
|
|
|
|
throw new Error("队列已满");
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
this.queue[this.rear] = num;
|
|
|
|
this.#queue[this.#rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
this.rear = (this.rear + 1) % this.capacity;
|
|
|
|
this.#rear = (this.#rear + 1) % this.capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
poll() {
|
|
|
|
poll() {
|
|
|
|
const num = this.peek();
|
|
|
|
const num = this.peek();
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
this.front = (this.front + 1) % this.capacity;
|
|
|
|
this.#front = (this.#front + 1) % this.capacity;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek() {
|
|
|
|
peek() {
|
|
|
|
// 删除头结点
|
|
|
|
|
|
|
|
if (this.empty())
|
|
|
|
if (this.empty())
|
|
|
|
throw new Error("The queue is empty!");
|
|
|
|
throw new Error("队列为空");
|
|
|
|
return this.queue[this.front];
|
|
|
|
return this.#queue[this.#front];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问指定索引元素 */
|
|
|
|
/* 访问指定索引元素 */
|
|
|
|
get(index) {
|
|
|
|
get(index) {
|
|
|
|
if (index >= this.size)
|
|
|
|
if (index >= this.size)
|
|
|
|
throw new Error("Index out of bounds!");
|
|
|
|
throw new Error("索引越界");
|
|
|
|
return this.queue[(this.front + index) % this.capacity];
|
|
|
|
return this.#queue[(this.#front + index) % this.capacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 返回 Array */
|
|
|
|
|
|
|
|
toArray() {
|
|
|
|
|
|
|
|
const siz = this.size;
|
|
|
|
|
|
|
|
const cap = this.capacity;
|
|
|
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
|
|
|
|
|
|
|
const arr = new Array(siz);
|
|
|
|
|
|
|
|
for (let i = 0, j = this.front; i < siz; i++, j++) {
|
|
|
|
|
|
|
|
arr[i] = this.queue[j % cap];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -928,46 +849,37 @@ comments: true
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="array_queue.ts"
|
|
|
|
```typescript title="array_queue.ts"
|
|
|
|
|
|
|
|
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
private queue: number[]; // 用于存储队列元素的数组
|
|
|
|
private queue: number[]; // 用于存储队列元素的数组
|
|
|
|
private front: number = 0; // 头指针,指向队首
|
|
|
|
private front: number = 0; // 头指针,指向队首
|
|
|
|
private rear: number = 0; // 尾指针,指向队尾 + 1
|
|
|
|
private rear: number = 0; // 尾指针,指向队尾 + 1
|
|
|
|
private CAPACITY: number = 1e5;
|
|
|
|
private CAPACITY: number = 1e5;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(capacity?: number) {
|
|
|
|
constructor(capacity?: number) {
|
|
|
|
this.queue = new Array<number>(capacity ?? this.CAPACITY);
|
|
|
|
this.queue = new Array<number>(capacity ?? this.CAPACITY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
get capacity(): number {
|
|
|
|
get capacity(): number {
|
|
|
|
return this.queue.length;
|
|
|
|
return this.queue.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
get size(): number {
|
|
|
|
get size(): number {
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
return (this.capacity + this.rear - this.front) % this.capacity;
|
|
|
|
return (this.capacity + this.rear - this.front) % this.capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
empty(): boolean {
|
|
|
|
empty(): boolean {
|
|
|
|
return this.rear - this.front == 0;
|
|
|
|
return this.rear - this.front == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
offer(num: number): void {
|
|
|
|
offer(num: number): void {
|
|
|
|
if (this.size == this.capacity) {
|
|
|
|
if (this.size == this.capacity)
|
|
|
|
console.log("队列已满");
|
|
|
|
throw new Error("队列已满");
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
this.queue[this.rear] = num;
|
|
|
|
this.queue[this.rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
this.rear = (this.rear + 1) % this.capacity;
|
|
|
|
this.rear = (this.rear + 1) % this.capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
poll(): number {
|
|
|
|
poll(): number {
|
|
|
|
const num = this.peek();
|
|
|
|
const num = this.peek();
|
|
|
@ -975,33 +887,18 @@ comments: true
|
|
|
|
this.front = (this.front + 1) % this.capacity;
|
|
|
|
this.front = (this.front + 1) % this.capacity;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek(): number {
|
|
|
|
peek(): number {
|
|
|
|
// 删除头结点
|
|
|
|
|
|
|
|
if (this.empty())
|
|
|
|
if (this.empty())
|
|
|
|
throw new Error("The queue is empty!");
|
|
|
|
throw new Error("队列为空");
|
|
|
|
return this.queue[this.front];
|
|
|
|
return this.queue[this.front];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问指定索引元素 */
|
|
|
|
/* 访问指定索引元素 */
|
|
|
|
get(index: number): number {
|
|
|
|
get(index: number): number {
|
|
|
|
if (index >= this.size)
|
|
|
|
if (index >= this.size)
|
|
|
|
throw new Error("Index out of bounds!");
|
|
|
|
throw new Error("索引越界");
|
|
|
|
return this.queue[(this.front + index) % this.capacity];
|
|
|
|
return this.queue[(this.front + index) % this.capacity];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 返回 Array */
|
|
|
|
|
|
|
|
toArray(): number[] {
|
|
|
|
|
|
|
|
const siz = this.size;
|
|
|
|
|
|
|
|
const cap = this.capacity;
|
|
|
|
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
|
|
|
|
|
|
|
const arr = new Array(siz);
|
|
|
|
|
|
|
|
for (let i = 0, j = this.front; i < siz; i++, j++) {
|
|
|
|
|
|
|
|
arr[i] = this.queue[j % cap];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|