c++队列

目录

 

  c++队列基础了解

  c++队列代码分步详解   全部代码在最后面

 队列的入队和出队具体实现(图形方式)

 队列的基本操作c++ 

 全部代码


 

c++队列基础了解

  队列:
    队列也是操作受限的线性表 限定在只能在表的一端进行插入 在表的另一端进行删除
 
    栈是先进后出  队列是先进先出
    允许插入数据的位置叫队尾 插入数据叫入队
    允许删除数据的位置叫队头 删除数据叫出队

    队列的物理实现 顺序队列(数组实现) 链式队列(链表实现)
    我们只讨论链式队列 顺序队列除了考试没什么人用的

  c++队列代码分步详解   全部代码在最后面

   以下8个队列函数就全部结束了 现在就有了队列的基础功能了 

   1 队列的数据结构 需要两个结构体

struct lnode
{
    elemtype data;//存储队列中的元素
    struct lnode* next;//next指针
};
struct linkqueue
{
    lnode* head, * tail;//队列的头指针和尾指针
};

2 初始化队列函数

bool initqueue(linkqueue& qq)//结构体别名
{
    qq.head = new(std::nothrow)lnode;//分配头节点
    if (qq.head == nullptr) return false;//内存不足 返回失败
    qq.head->next = nullptr;//头结点的下一结点暂时不存在 置空
    qq.tail = qq.head; //尾指针目前指向头结点
    return true;
}

3 销毁队列函数

void destroyqueue(linkqueue& qq)
{
    lnode* tmp;
    while (qq.head != nullptr)
    {
        tmp = qq.head->next; //tmp先保存下一结点的地址
        delete qq.head;      //释放当前节点
        qq.head = tmp;       //头指针移动到下一节点
    }
}

 队列的入队和出队具体实现(图形方式)

1:队列的入队规则  大白话来说就是从尾部next指针进行添加节点

2: 队列的出队规则  大白话来说就是从头部next指针进行删除结点

了解了入队和出队的实现方式 就能理解入队出队代码了

4 元素入队函数

bool inqueue(linkqueue& qq,const elemtype ee)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return false; }
    lnode* tmp = new(std::nothrow)lnode;//分配新节点
    if (tmp == nullptr) return false;

    tmp->data = ee;         //把元素的值存入data中
    tmp->next = nullptr;    //tmp因为是最后一个元素所以next是nullptr
    qq.tail->next = tmp;    //把tmp追加到qq.tail后
    qq.tail = tmp;          // 重新设置tail指针。
    return true;
}

5 元素出队函数

bool OutQueue(linkqueue& QQ, elemtype& ee)
{
    if (QQ.head == nullptr) { cout << "队列未初始化。\n"; return false; }
    if (QQ.head->next == nullptr) { cout << "队列为空。\n"; return false; }
    lnode* tmp = QQ.head->next;     // tmp指向第一个结点。
    ee = tmp->data;     // 把第一个结点的数据保存到ee中。
    QQ.head->next = tmp->next;  // 头结点的next指针指向第二个结点。
    // 如果出队的是最后一个结点。
    if (tmp == QQ.tail)  QQ.tail = QQ.head;
    delete tmp;     // 释放已出队的结点。
    return true;
}

6 显示队列中全部的元素函数。

void printqueue(linkqueue& qq)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return; }
    lnode* tmp = qq.head->next;
    while (tmp != nullptr)
    {
        cout << tmp->data<<" ";
        tmp = tmp->next;
    }
    cout << endl;
}

7 求队列长度函数

int Length(linkqueue& qq)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return false; }
    lnode* tmp = qq.head->next;
    int length = 0;
    while (tmp != nullptr)
    {
        length++;
        tmp = tmp->next;
    }
    return length;

}

8 清空队列函数

void clear(linkqueue& qq)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return; }
    // 清空队列是指释放链表全部的数据结点,但保留头结点。
    lnode* tmp = qq.head->next;
    lnode* tmpnext;
    while (tmp != nullptr)
    {
        tmpnext = tmp->next;       // tmpnext保存下一结点的地址。
        delete[]tmp;               // 释放当前结点。
        tmp = tmpnext;             // tmp指针移动到下一结点。
    }
    qq.head->next = nullptr;
    qq.tail = qq.head;             // 尾指针指向头结点。
}

队列的基本操作c++ 

main 函数内容

int main()
{
    linkqueue qq; //创建队列
    memset(&qq, 0, sizeof(qq));
    initqueue(qq);//初始化队列

    //入队使用for循环
    cout << "使用for循环入队0-9" << endl;
    for (int i = 0; i < 10; i++)
    {
        inqueue(qq, i);
    }
    cout << "队列中的元素是:";
    printqueue(qq);
    cout << "队列的长度是:" << Length(qq) << "。\n";
    elemtype ee;     // 创建一个数据元素变量ee 用于接收出队的数据。
    
    //出队一个元素
    cout << "首先将首元素出队" << endl;
    OutQueue(qq, ee);
    cout << "出队的元素值为" << ee << endl;
    cout << "队列中的元素是:";
    printqueue(qq);
    //全部出队
    cout << "使用while 将剩余元素全部出队" << endl;
    while (OutQueue(qq, ee))
        cout << "出队的元素值为" << ee << endl;
    cout << "队列中的元素是:";
    printqueue(qq);


    destroyqueue(qq); // 销毁队列QQ。
}

运行后截图

 全部代码

#include <iostream>
using namespace std;
typedef int elemtype;   // 自定义队列的数据元素为整数。
//队列的数据结构 两个结构体
struct lnode
{
    elemtype data;//存储队列中的元素
    struct lnode* next;//next指针
};
struct linkqueue
{
    lnode* head, * tail;//队列的头指针和尾指针
};
// 初始化队列。
bool initqueue(linkqueue& qq)//结构体别名
{
    qq.head = new(std::nothrow)lnode;//分配头节点
    if (qq.head == nullptr) return false;//内存不足 返回失败
    qq.head->next = nullptr;//头结点的下一结点暂时不存在 置空
    qq.tail = qq.head; //尾指针目前指向头结点
    return true;
}
// 销毁队列QQ。
void destroyqueue(linkqueue& qq)
{
    lnode* tmp;
    while (qq.head != nullptr)
    {
        tmp = qq.head->next; //tmp先保存下一结点的地址
        delete qq.head;      //释放当前节点
        qq.head = tmp;       //头指针移动到下一节点
    }
}
// 元素入队。
bool inqueue(linkqueue& qq,const elemtype ee)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return false; }
    lnode* tmp = new(std::nothrow)lnode;//分配新节点
    if (tmp == nullptr) return false;

    tmp->data = ee;         //把元素的值存入data中
    tmp->next = nullptr;    //tmp因为是最后一个元素所以next是nullptr
    qq.tail->next = tmp;    //把tmp追加到qq.tail后
    qq.tail = tmp;          // 重新设置tail指针。
    return true;
}
// 元素出队。
// // 元素出队。
bool OutQueue(linkqueue& QQ, elemtype& ee)
{
    if (QQ.head == nullptr) { cout << "队列未初始化。\n"; return false; }
    if (QQ.head->next == nullptr) {  return false; }
    lnode* tmp = QQ.head->next;     // tmp指向第一个结点。
    ee = tmp->data;     // 把第一个结点的数据保存到ee中。
    QQ.head->next = tmp->next;  // 头结点的next指针指向第二个结点。
    // 如果出队的是最后一个结点。
    if (tmp == QQ.tail)  QQ.tail = QQ.head;
    delete tmp;     // 释放已出队的结点。
    return true;
}
// 显示队列中全部的元素。
void printqueue(linkqueue& qq)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return; }
    lnode* tmp = qq.head->next;
    while (tmp != nullptr)
    {
        cout << tmp->data<<" ";
        tmp = tmp->next;
    }
    cout << endl;
}

// 求队列的长度。
int Length(linkqueue& qq)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return false; }
    lnode* tmp = qq.head->next;
    int length = 0;
    while (tmp != nullptr)
    {
        length++;
        tmp = tmp->next;
    }
    return length;

}
// 清空队列。
void clear(linkqueue& qq)
{
    if (qq.head == nullptr) { cout << "队列未初始化。\n"; return; }
    // 清空队列是指释放链表全部的数据结点,但保留头结点。
    lnode* tmp = qq.head->next;
    lnode* tmpnext;
    while (tmp != nullptr)
    {
        tmpnext = tmp->next;       // tmpnext保存下一结点的地址。
        delete[]tmp;               // 释放当前结点。
        tmp = tmpnext;             // tmp指针移动到下一结点。
    }
    qq.head->next = nullptr;
    qq.tail = qq.head;             // 尾指针指向头结点。
}
int main()
{
    linkqueue qq; //创建队列
    memset(&qq, 0, sizeof(qq));
    initqueue(qq);//初始化队列

    //入队使用for循环
    cout << "使用for循环入队0-9" << endl;
    for (int i = 0; i < 10; i++)
    {
        inqueue(qq, i);
    }
    cout << "队列中的元素是:";
    printqueue(qq);
    cout << "队列的长度是:" << Length(qq) << "。\n";
    elemtype ee;     // 创建一个数据元素变量ee 用于接收出队的数据。
    
    //出队一个元素
    cout << "首先将首元素出队" << endl;
    OutQueue(qq, ee);
    cout << "出队的元素值为" << ee << endl;
    cout << "队列中的元素是:";
    printqueue(qq);
    //全部出队
    cout << "使用while 将剩余元素全部出队" << endl;
    while (OutQueue(qq, ee))
        cout << "出队的元素值为" << ee << endl;
    cout << "队列中的元素是:";
    printqueue(qq);


    destroyqueue(qq); // 销毁队列QQ。
}

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello-smile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值