javascript 之顺序队列(链表实现)

大家好,我是 17。

优先队列 不同,顺序队列没有特权,所有元素都是平等的,先进的先出。

代码

class Node {
  constructor(val) {
    this.val = val
    this.next = null
  }
}

class Queen {
  constructor() {
    this.length = 0
    const node = new Node()
    this.head = node
    this.tail = node
  }
  push(val) {
    const node = new Node(val)
    this.tail.next = node
    this.tail = node
    this.length++
  }
  pop() { 
    if (this.length <= 0) return undefined
    let val = this.head.next.val
    this.head = this.head.next
    this.length--
    return val
  }
  peekHead() { 
    if (this.length <= 0) return undefined
    else return this.head.next.val
  }
  peekTail(){
    if (this.length <= 0) return undefined
    else return this.tail.val
  }
}

方法命名参照数组的命名。push 是在队尾入队元素,pop 是在队首出队元素。length 是队列的长度。我在所有相关 线性表 的方法命名上都会参考数组命名,这样的好处是不用记。

入队和出队一个元素的时间复杂度都是 O(1),这是用链表的好处。如果用数组来实现的话,出队或入队将为 O(n)。还有一种实现方式是用 hash 表的方式,用连续的数字做 key,好处是可以查询到任意位置的元素,坏处是数字可能遇到上限。

所以如果只是从队尾入,从队首出的话,链表是最好的选择。

应用

顺序队列的一个应用是缓存数据。在生产者和消费者之间建立一个缓冲,以缓解生产和消费速度不一致的矛盾。

在实际应用中,可能生产者在某段时间的能力特别强,会导致队列过大,所以还得加个限制。

class Queen {
  constructor(max = Infinity) {
    this.max = max
    this.length = 0
    const node = new Node()
    this.head = node
    this.tail = node
  }
  push(val) {
    const node = new Node(val)
    this.tail.next = node
    this.tail = node
    this.length++
    if (this.length > this.max) {
      this.pop()
    }
  }
  pop() {
    if (this.length <= 0) return undefined
    let val = this.head.next.val
    this.head = this.head.next
    this.length--
    return val
  }
  peek() {
    if (this.length <= 0) return undefined
    else return this.head.next.val
  }
}

加上 max 参数,最大长度限度,相当于是一个保险。如果超过长度,新元素入队,老元素出队,操持最大长度。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是C语言链表实现循环队列的代码实现和详细步骤: 1.定义循环队列的结构体和初始化函数 ```c #define MAXSIZE 100 // 循环队列的最大长度 typedef struct SLnode { int data; struct SLnode* next; }SLnode; typedef struct CQueue { SLnode* front; // 队头指针 SLnode* tail; // 队尾指针 int length; // 队列长度 }CQueue; // 初始化循环队列 void InitCQueue(CQueue* pq) { pq->front = pq->tail = (SLnode*)malloc(sizeof(SLnode)); pq->front->next = NULL; pq->length = 0; } ``` 2.实现入队操作 ```c // 入队操作 void EnCQueue(CQueue* pq, int x) { SLnode* s = (SLnode*)malloc(sizeof(SLnode)); s->data = x; s->next = NULL; pq->tail->next = s; pq->tail = s; pq->length++; } ``` 3.实现出队操作 ```c // 出队操作 int DeCQueue(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法出队!\n"); return -1; } SLnode* p = pq->front->next; int x = p->data; pq->front->next = p->next; if (pq->tail == p) { pq->tail = pq->front; } free(p); pq->length--; return x; } ``` 4.实现获取队列长度的函数 ```c // 获取队列长度 int CQueueLength(CQueue* pq) { SLnode* cur = pq->front->next; int size = 0; while (cur != NULL) { cur = cur->next; ++size; } return size; } ``` 5.实现判断队列是否为空的函数 ```c // 判断队列是否为空 int IsCQueueEmpty(CQueue* pq) { if (pq->front == pq->tail) { return 1; } else { return 0; } } ``` 6.实现判断队列是否已满的函数 ```c // 判断队列是否已满 int IsCQueueFull(CQueue* pq) { if (pq->length == MAXSIZE) { return 1; } else { return 0; } } ``` 7.实现获取队头元素的函数 ```c // 获取队头元素 int GetCQueueFront(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法获取队头元素!\n"); return -1; } return pq->front->next->data; } ``` 8.实现获取队尾元素的函数 ```c // 获取队尾元素 int GetCQueueRear(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法获取队尾元素!\n"); return -1; } return pq->tail->data; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IAM17前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值