跳到正文

个人记录

手写 deque

__Ganlai__约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年10月27日
收录
2026年11月11日
更新
2026年11月18日
篇幅
约 7 分钟

版本

  1. v1当前

闲话:这是笔者在颓废时胡乱写的。


核心代码

C++
class Queue
{
  static const int SIZE=1<<21;
  class Stack
  {
    int stack[SIZE],top=0;
  public:
    inline void push(const int &val)
    {
      stack[top++]=val;
    }
    inline void pop(void)
    {
      --top;
    }
    inline int query(void)
    {
      return stack[top-1];
    }
    inline int size(void)
    {
      return top;
    }
  }stack;
  class List
  {
    struct Union
    {
      Union *prev=nullptr,*next=nullptr;
      int val;
    }list[SIZE],*head=nullptr,*tail=nullptr;
  public:
    inline bool empty(void)
    {
      return head==nullptr&&tail==nullptr;
    }
    inline int front(void)
    {
      return head->val;
    }
    inline int back(void)
    {
      return tail->val;
    }
    inline void pushfront(const int &siz,const int &val)
    {
      Union *p=list+siz;
      p->val=val,
      p->prev=nullptr,
      p->next=head;
      if(head==nullptr&&tail==nullptr)
      {
        head=tail=p;
        return;
      }
      head->prev=p,
      head=p;
    }
    inline void pushback(const int &siz,const int &val)
    {
      Union *p=list+siz;
      p->val=val,
      p->next=nullptr,
      p->prev=tail;
      if(head==nullptr&&tail==nullptr)
      {
        head=tail=p;
        return;
      }
      tail->next=p,
      tail=p;
    }
    inline int popfront(void)
    {
      Union *p=head;
      if(p->next==nullptr)
      {
        head=tail=nullptr;
        return p-list;
      }
      p->next->prev=nullptr,
      head=p->next;
      return p-list;
    }
    inline int popback(void)
    {
      Union *p=tail;
      if(p->prev==nullptr)
      {
        head=tail=nullptr;
        return p-list;
      }
      p->prev->next=nullptr,
      tail=p->prev;
      return p-list;
    }
  }list;
public:
  Queue()
  {
    for(int i=0;i<SIZE;++i)
      stack.push(i);
  }
  inline bool empty(void)
  {
    return list.empty();
  }
  inline int size(void)
  {
    return SIZE-stack.size();
  }
  inline int front(void)
  {
    if(list.empty())
      return -1;
    return list.front();
  }
  inline int back(void)
  {
    if(list.empty())
      return -1;
    return list.back();
  }
  inline bool pushfront(const int &val)
  {
    if(!stack.size())
      return false;
    list.pushfront(stack.query(),val),stack.pop();
    return true;
  }
  inline bool pushback(const int &val)
  {
    if(!stack.size())
      return false;
    list.pushback(stack.query(),val),stack.pop();
    return true;
  }
  inline bool popfront(void)
  {
    if(list.empty())
      return false;
    stack.push(list.popfront());
    return true;
  }
  inline bool popback(void)
  {
    if(list.empty())
      return false;
    stack.push(list.popback());
    return true;
  }
};

(码风极丑,多多包涵)

正文

内容概要

本质是预分配大数组的静态内存池 + 双向链表 + 空闲栈(即接下来会讲到的 ListList 和 StackStack),实现双端队列 deque。

空间复杂度 O(SIZE)O(SIZE),定义时间复杂度 O(SIZE)O(SIZE),操作时间复杂度 O(1)O(1)。SIZESIZE 默认为 221=2,097,1522^{21} = 2,097,152。

对,说完了。

Part 1.Stack

一个表里如一的栈,用来记录链表中空闲的节点。

(1).stack & top & size()

stackstack 是栈数组本身,储存空间从 stack0stack_0 至 stackSIZE−1stack_{SIZE-1},容量为 SIZESIZE。

toptop 记录栈顶的下一位与栈大小,范围为 11 至 SIZESIZE。stacktop−1stack_{top-1} 即为栈顶。

size()size() 是 toptop 在 StackStack 在外部的公有成员,返回 toptop 的数值(即栈的大小)。

stackstack 与 toptop 都是 StackStack 的私有成员,仅在内部使用。

(2).push(val)

将一个链表中空闲的节点 valval 压入栈顶,等待使用。

(3).query()

返回位于栈顶的空闲的节点,以供链表抓取。

(4).pop()

与 push()push() 相对,弹出栈顶已被链表抓取的空闲节点。

Part 2.List

一个用指针实现的双向链表,也是整个 QueueQueue 的核心。

(1).Union & list & *head & *tail

UnionUnion 是链表节点结构体,由 ∗prev,∗next,val*prev,*next,val 组成,分别代表链表节点的上一个链表节点、下一个链表节点与储存的数值。∗prev*prev 和 ∗next*next 初始值为 nullptrnullptr。

listlist 为链表数组本身,储存空间从 list0list_0 至 listSIZE−1list_{SIZE-1},容量为 SIZESIZE。

∗head*head 和 ∗tail*tail 是链表的头指针和尾指针,链表为空时值均为 nullptrnullptr。

原文