栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++数据结构——链队列

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++数据结构——链队列

主页有其他数据结构内容(持续更新中)

代码:
#include
using namespace std;


template 
struct Node
{
    T data;        //数据域
    Node* next;      //指针域

    Node()
    {
        next = nullptr;
    }

    Node(T item, Node* ptr = nullptr)
    {
        data = item;
        next = ptr;
    }
};


template 
class LinkQueue
{
public:
    LinkQueue();                        //构造和析构
    ~LinkQueue();
    LinkQueue(const LinkQueue& item);  //拷贝构造函数
    LinkQueue& operator=(const LinkQueue& item);  //重载赋值运算符
    void push(const T& item);    //入队操作
    void pop();                     //出队操作
    bool empty()const;                     //判断队列是否为空
    T top();  //返回队列头部元素
    int size()const;    //  返回队列长度

private:
    Node* front;        //队头指针
    Node* rear;         //队尾指针
    int count;
};


template
LinkQueue::LinkQueue()  //构造函数
{
    rear = front = nullptr;
    this->count = 0;
}


template
LinkQueue::~LinkQueue()  //析构函数
{
    while (!empty())
    {
        pop();
    }
}


//入队操作
template
void LinkQueue::push(const T& item)
{
    Node* temp = new Node(item);   //手动开辟空间
    if (temp == nullptr)
    {
        cout << "Overflow!" << endl;
        return;
    }
    if (rear == nullptr)
    {
        front = rear = temp;
    }
    else
    {
        rear->next = temp;
        rear = temp;
    }
    this->count++;
}


//出队操作
template
void LinkQueue::pop()
{
    if (empty())
    {
        cout << "Underflow!" << endl;
        return;
    }
    Node* ptr = front;
    front = front->next;
    ptr = nullptr;
    delete ptr;
    if (this->count == 1){  //  这是最后一个结点出队
        rear = nullptr;
    }
    this->count--;
}


//判断队列是否为空
template
bool LinkQueue::empty()const
{
    return count == 0;
}


template
T LinkQueue::top() {
    if (empty()) {
        cout << "Underflow!" << endl;
    }
    else {
        return front->data;
    }
}


template
LinkQueue::LinkQueue(const LinkQueue& item)  //拷贝构造函数
{
    Node* original_front = item.front;
    Node* original_rear = item.rear;
    Node* new_copy;
    if (original_front == nullptr && original_rear == nullptr)
    {
        front = rear = nullptr;
    }
    else
    {
        front = new_copy = new Node(original_front->data);
        rear = new Node(original_rear->data);
        while (original_front->next != nullptr)
        {
            original_front = original_front->next;
            new_copy->next = new Node(original_front->data);
            new_copy = new_copy->next;
        }
    }
    this->count = item.count;
}


template
LinkQueue& LinkQueue::operator=(const LinkQueue& item)  //重载赋值运算符
{
    Node* original_front = item.front;
    Node* original_rear = item.rear;
    Node* new_copy;
    if (original_front == nullptr && original_rear == nullptr)
    {
        front = rear = nullptr;
    }
    else
    {
        front = new_copy = new Node(original_front->data);
        rear = new Node(original_rear->data);
        while (original_front->next != nullptr)
        {
            original_front = original_front->next;
            new_copy->next = new Node(original_front->data);
            new_copy = new_copy->next;
        }
    }
    this->count = item.count;
    return *this;
}

template
int LinkQueue::size() const {
    return this->count;
}


int main(){
    LinkQueue lq;
    for (int i = 0; i < 10; ++i) {
        lq.push(i);
    }
    cout << "队列的长度为:" << lq.size() << endl;

    LinkQueue copy_1 = lq;
    while (!copy_1.empty()){
        cout << copy_1.top() << "t";
        copy_1.pop();
    }
    cout << endl;

    LinkQueue copy_2 = lq;
    while (!copy_2.empty()){
        cout << copy_2.top() << "t";
        copy_2.pop();
    }


    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号