数据结构 - C/C++ - 队列

结构特性

  • 队列是一种特殊的线性表,限制在表的一端进行插入、在表的另一端进行删除。

    • 表中允许插入的一端称为队尾(rear) - 进队 | 入队

    • 表中允许删除的一端称为队头(front) - 退队 | 出队

  • 先进先出(first in first out - FIFO) - 队列中先进入的元素最先出队。

结构实现

  • 静态队列 - 基于数组 - 顺序存储

  • 动态队列 - 基于链表 - 链式存储

结构容器

  • queue

  • deque

结构设计

  • 顺序存储

  • 链式存储

cpp 复制代码
class Node
{
public:
    int value;
    Node* Next;
    Node(int Num) : value(Num), Next(nullptr) {}
};

class Queue
{
public:
    Node* front;
    Node* rear;
    int size;

public:
    Queue() : front(nullptr) , rear(nullptr), size(0) {}
    ~Queue()
    {
        Clear();
    }


public:
    int GetSize()
    {
        return size;
    }
    bool IsEmpty()
    {
        return size == 0;
    }
    void Clear()
    {
        Node* node = front;
        while (node)
        {
            Node* temp = node;
            node = node->Next;
            delete temp;
        }
    }

public:
    void Push(int value)
    {
        Node* node = new Node(value);
        if (front == nullptr)
        {
            front = node;
            rear = node;
        }
        else
        {
            rear->Next = node;
            rear = node;
        }

        size++;
    }
    int Pop()
    {
        if (IsEmpty()) return 0;

        int RetValue = GetFront();

        Node* node = front;
        front = front->Next;
        delete node;

        size--;

        return RetValue;
    }
    int GetFront()
    {
        if (this->front)
        {
            return this->front->value;
        }

        return -1;
    }
    int GetRear()
    {
        if (this->rear)
        {
            return this->rear->value;
        }
        return - 1;
    }
};
  • 双端队列
cpp 复制代码
#include <iostream>

class Node
{
public:
    int value;
    Node* Prev;
    Node* Next;
    Node(int value) : value(value), Prev(nullptr), Next(nullptr) {}
};

class Deque
{
public:
    Node* front;
    Node* rear;
    int size;

public:
    Deque(): front(nullptr), rear(nullptr), size(0) 
    {

    }
    ~Deque()
    {
        Node* node = front;
        while (node)
        {
            Node* temp = node;
            node = node->Next;
            delete temp;
        }
    }

public:
    int GetSize()
    {
        return this->size;
    }
    bool IsEmpty()
    {
        return this->size == 0;
    }

public:
    void PushFornt(int value)
    {
        Node* node = new Node(value);

        if (IsEmpty())
        {
            front = rear = node;
        }
        else
        {
            node->Prev = nullptr;
            node->Next = front;
            front->Prev = node;
            front = node;
        }

        size++;
    }
    void PushRear(int value)
    {
        Node* node = new Node(value);

        if (IsEmpty())
        {
            front = rear = node;
        }
        else
        {
            node->Next = nullptr;
            node->Prev = rear;
            rear->Next = node;          
            rear = node;
        }
        size++;
    }

    int PopFront()
    {
        int Ret = 0;

        if (IsEmpty())
        {
            return -1;
        }
        else
        {
            Ret = this->front->value;
            Node* node = this->front->Next;
            if (node != nullptr)
            {
                node->Prev = nullptr;
            }
            delete front;
            front = node;
        }

        size--;
        return Ret;
    }
    int PopRear()
    {
        int Ret = 0;

        if (IsEmpty())
        {
            return -1;
        }
        else
        {
            Ret = this->rear->value;
            Node* node = this->rear->Prev;
            if (node != nullptr)
            {
                node->Next = nullptr;
            }

            delete rear;
            rear = node;
        }

        size--;
        return Ret;
    }

    int GetFront()
    {
        if (IsEmpty())
        {
            return -1;
        }
        return front->value;
    }
    int GetRear()
    {
        if (IsEmpty())
        {
            return -1;
        }
        return rear->value;
    }

};
相关推荐
Meme Buoy2 小时前
18.补充数学1:生成树-最短路径-最大流量-线性规划
数据结构·算法
汀、人工智能2 小时前
[特殊字符] 第89课:岛屿数量
数据结构·算法·数据库架构·图论·bfs·岛屿数量
九英里路2 小时前
cpp容器——string模拟实现
java·前端·数据结构·c++·算法·容器·字符串
2401_892070983 小时前
顺序栈(动态数组实现) 超详细解析(C++ 语言 + 可直接运行)
数据结构·c++·顺序栈
漫霂3 小时前
二叉树的翻转
java·数据结构·算法
3秒一个大3 小时前
深入理解 JS 中的栈与堆:从内存模型到数据结构,再谈内存泄漏
前端·javascript·数据结构
旖-旎4 小时前
哈希表(存在重复元素)(3)
数据结构·c++·学习·算法·leetcode·散列表
计算机安禾4 小时前
【数据结构与算法】第39篇:图论(三):最小生成树——Prim算法与Kruskal算法
开发语言·数据结构·c++·算法·排序算法·图论·visual studio code
汀、人工智能4 小时前
[特殊字符] 第77课:最长递增子序列
数据结构·算法·数据库架构·图论·bfs·最长递增子序列
澈2074 小时前
堆排序:高效构建大顶堆实战
数据结构·算法·排序算法