首页 > 编程语言 > C++数据结构(队列)

C++数据结构(队列)

时间:2023-04-24 18:00:11浏览次数:33  
标签:队列 SeqQueue LinkQueue C++ bool template front 数据结构 rear

队列是先进先出的线性表

顺序队列

顺序存储,使用一段连续的内存空间去依次存储队列中的数据

代码实现:

#include <iostream>

#define MaxSize 10

template <typename T>
class SeqQueue {
public:
    SeqQueue();
    ~SeqQueue();

public:
    bool EnQueue(const T& e);
    bool DeQueue(T &e);
    bool GetHead(T &e);
    void ClearQueue();
    
    void DispList();
    int ListLength();
    bool IsEmpty();
    bool IsFull();

private:
    T *m_data;
    int m_front;
    int m_rear;
};

template <typename T>
SeqQueue<T>::SeqQueue() {
    m_data = new T[MaxSize];
    m_front = 0;
    m_rear = 0;
}

template <typename T>
SeqQueue<T>::~SeqQueue() {
    delete[] m_data;
}

template <typename T>
bool SeqQueue<T>::EnQueue(const T &e) {
    if (IsFull() == true) {
        std::cout << "SeqQueue Full" << std::endl;
        return false;
    }
    m_data[m_rear] = e;
    m_rear++;
    return true;
}

template <typename T>
bool SeqQueue<T>::DeQueue(T &e) {
    if (IsEmpty() == true) {
        std::cout << "SeqQueue Empty" << std::endl;
        return false;
    }
    e = m_data[m_front];
    m_front++;
    return true;
}

template <typename T>
bool SeqQueue<T>::GetHead(T &e) {
    if (IsEmpty() == true) {
        std::cout << "SeqQueue Empty" << std::endl;
        return false;
    }
    e = m_data[m_front];
    return true;
}

template <class T>
void SeqQueue<T>::DispList() {
    for (int i = m_front; i < m_rear; i++) {
        std::cout << m_data[i] << " ";
    }
    std::cout << std::endl;
}

template <class T>
int SeqQueue<T>::ListLength() {
    return m_rear - m_front;
}

template <class T>
bool SeqQueue<T>::IsEmpty() {
    if (m_front == m_rear) {
        return true;
    }
    return false;
}

template <class T>
bool SeqQueue<T>::IsFull() {
    if (m_rear >= MaxSize) {
        return true;
    }
    return false;
}

template <class T>
void SeqQueue<T>::ClearQueue() {
    m_front = m_rear = 0;
}

int main(void) {
    SeqQueue<int> seqobj;
    seqobj.EnQueue(150);
    seqobj.EnQueue(200);
    seqobj.EnQueue(300);
    seqobj.EnQueue(400);
    seqobj.DispList();

    return 0;
}

链式队列

如果长度不确定,那么可以使用链式队列

#include <iostream>

template <typename T>
struct QueueNode {
    T data;
    QueueNode<T> *next;
};

template <typename T>
class LinkQueue {
public:
    LinkQueue();
    ~LinkQueue();
public:
    bool EnQueue(const T &e);
    bool DeQueue(T &e);
    bool GetHead(T &e);

    void DispList();
    int ListLength();
    bool IsEmpty();
private:
    QueueNode<T> *m_front;
    QueueNode<T> *m_rear;
    int m_length;
};

template <typename T>
LinkQueue<T>::LinkQueue() {
    m_front = new QueueNode<T>;
    m_front->next = nullptr;
    m_rear = m_front;
    m_length = 0;
}

template <typename T>
LinkQueue<T>::~LinkQueue() {
    QueueNode<T> *pnode = m_front->next;
    QueueNode<T> *ptmp;
    while (pnode != nullptr) {
        ptmp = pnode;
        pnode = pnode->next;
        delete ptmp;
    }
    delete m_front;
    m_front = m_rear = nullptr;
    m_length = 0;
}

template <typename T>
bool LinkQueue<T>::EnQueue(const T &e) {
    QueueNode<T> *node = new QueueNode<T>;
    node->data = e;
    node->next = nullptr;

    m_rear->next = node;
    m_rear = node;
    m_length++;

    return true;
}

template <typename T>
bool LinkQueue<T>::DeQueue(T &e) {
    if (IsEmpty() == true) {
        std::cout << "LinkQueue Empty" << std::endl;
        return false;
    }
    QueueNode<T> *p_willdel = m_front->next;
    e = p_willdel->data;

    m_front->next = p_willdel->next;
    if (m_rear == p_willdel) {
        m_rear = m_front;
    }

    delete p_willdel;
    m_length--;
    return true;
}

template <typename T>
bool LinkQueue<T>::GetHead(T &e) {
    if (IsEmpty() == true) {
        std::cout << "Link Queue Empty" <<std::endl;
        return false;
    }
    e = m_front->next->data;
    return true;
}

template <class T>
void LinkQueue<T>::DispList() {
    QueueNode<T> *p = m_front->next;
    while (p != nullptr) {
        std::cout << p->data << " ";
        p = p->next;
    }
    std::cout << std::endl;
}

template <class T>
int LinkQueue<T>::ListLength() {
    return m_length;
}

template <class T>
bool LinkQueue<T>::IsEmpty() {
    if (m_front == m_rear) {
        return true;
    }
    return false;
}

int main(void) {
    LinkQueue<int> lnobj;
    lnobj.EnQueue(150);

    int eval2 = 0;
    lnobj.DeQueue(eval2);
    lnobj.EnQueue(200);
    lnobj.EnQueue(700);
    lnobj.DispList();

    return 0;
}

标签:队列,SeqQueue,LinkQueue,C++,bool,template,front,数据结构,rear
From: https://www.cnblogs.com/N3ptune/p/17350368.html

相关文章

  • 【C/C++】 可变参数函数
    #include<stdio.h>#include<stdarg.h>/***按自定义格式符解析数据*/voidprocess(constchar*fmt,va_listargs){for(;*fmt;fmt++){if(*fmt=='%'){continue;}switch(*fmt){ca......
  • 【介绍】C++五种迭代器
    目录1. 输入迭代器(Input Iterator):2. 输出迭代器(Output Iterator):3. 前向迭代器(Forward Iterator):4. 双向迭代器(Bidirectional Iterator):5. 随机访问迭代器(Random Access Iterator): 1. 输入迭代器(Input Iterator):支持单次读取和前进;即只能遍历一遍集合,并且只能向......
  • C++中struct和class的区别 || C++中const和static的作用
    struct和class不同点两者中如果不对成员不指定公私有,struct默认是公有的,class则默认是私有的class默认是private继承,而struct默认是public继承  static不考虑类的情况隐藏。所有不加static的全局变量和函数具有全局可见性,可以在其他文件中使用,加了之后只能在该......
  • C++ 学习 第八天
    今日内容:匿名函数 动态数组 匿名函数:lambda表达式:捕获列表:[捕获列表]{cout<<endl;}捕获列表捕获的是父作用域下的属性,如果[]为空,默认不补货 值捕获父作用域下所有的属性,只捕获值,不捕获属性本身(只读不写)值捕获父作用域下所有的函数,但是引用捕获父作用域下......
  • 13、c++使用单例模式实现命名空间函数
    本案例实现一个test命名空间,此命名空间内有两个函数,分别为getName()和getNameSpace();声明命名空间及函数namespacetest{conststd::string&getName()和();conststd::string&getNameSpace();}命名空间内实现单例类实现一个单例类,构造函数要为private,自身对......
  • 基于C++研究高并发内存池
    访问【WRITE-BUG数字空间】_[内附完整源码和文档]内存池:程序预先向系统申请一大块足够的内存,此后,当系统需要申请内存的时候,不是直接向操作习题申请,而是向内存池中申请,当释放的时候,不返回给操作系统,而是返回给内存池,当程序退出时,内存池才将申请的内存真正释放高并发内存池借鉴tcmall......
  • C++并发之fence
    intx=0;inty=0;intr0,r1;//cpu1voidf1(){x=1;std::atomic_thread_fence(std::memory_order_acquire);r0=y;}//cpu2voidf2(){y=1;std::atomic_thread_fence(std::memory_order_acquire);r1=x;} fence......
  • C++第四章课后习题4-8
    定义一个dog类,包含的age,weight等属性,以及对这些属性的操作方法,实现并测试这个类。1#include<iostream>2usingnamespacestd;3classDog{4private:5intage,weight;6public:7voidsetdog(inta,intb)8{9......
  • c++打卡十三天
    一、问题描述。 二、设计思路①、首先我们是用二分法解决这个问题。二分法是指在一个有序数组中,我们通过目标数与数组中间值的比较,对半缩小数组范围,比如一个升序数组中间值是4,当我们寻找一个比四小的数字时,只需要从首位和中间值中寻找,然后继续确定新的中间值,长此以往,就可以有......
  • 双端队列的定位
    1:可用迭代器2:地址访问#include<iostream>#include<string>#include<deque>//头文件不能少usingnamespacestd;deque<string>deq;//这里用一个string类型的deque来做演示,初始为空deque<string>::iteratorit;//提前准备一个迭代器,写法一如既往intmain(){......