手写 deque

· · 个人记录

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

核心代码

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;
  }
};

(码风极丑,多多包涵)

正文

内容概要

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

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

对,说完了。

Part 1.Stack

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

(1).stack & top & size()

$top$ 记录栈顶的下一位与栈大小,范围为 $1$ 至 $SIZE$。$stack_{top-1}$ 即为栈顶。 $size()$ 是 $top$ 在 $Stack$ 在外部的公有成员,返回 $top$ 的数值(即栈的大小)。 $stack$ 与 $top$ 都是 $Stack$ 的私有成员,仅在内部使用。 ### (2).push(val) 将一个链表中空闲的节点 $val$ 压入栈顶,等待使用。 ### (3).query() 返回位于栈顶的空闲的节点,以供链表抓取。 ### (4).pop() 与 $push()$ 相对,弹出栈顶已被链表抓取的空闲节点。 ## Part 2.List 一个用指针实现的双向链表,也是整个 $Queue$ 的核心。 ### (1).Union & list & *head & *tail $Union$ 是链表节点结构体,由 $*prev,*next,val$ 组成,分别代表链表节点的上一个链表节点、下一个链表节点与储存的数值。$*prev$ 和 $*next$ 初始值为 $nullptr$。 $list$ 为链表数组本身,储存空间从 $list_0$ 至 $list_{SIZE-1}$,容量为 $SIZE$。 $*head$ 和 $*tail$ 是链表的头指针和尾指针,链表为空时值均为 $nullptr$。