数据结构——队列

一、实验目的

  1. 理解和掌握队列的类型定义方法。
  2. 掌握栈中的基本运算,包括创建、判空及判满、出队/入队等基本操作。
  3. 学习利用队解决实际问题

二、实验要求

【项目1 – 验证性实验:建立顺序环形队列算法库】

定义顺序环形队列存储结构,实现其基本运算,并完成测试。
【要求】:
1、头文件sqqueue.h中定义数据结构并声明用于完成基本运算的函数。
2、在sqqueue.cpp中实现这些函数
3、在main函数中完成测试,包括如下内容:
(1)初始化队列q
(2)依次进队列元素a,b,c
(3)判断队列是否为空
(4)出队一个元素
(5)输出队列中元素个数
(6)依次进队列元素d,e,f
(7)输出队列中元素个数
(8)将队列中所有元素删除,并输出序列
(9)释放队列

main.cpp程序代码:

#include <iostream>
#include "sqqueue.h"
#include "liqueue.h"
using namespace std;

int main()
{
    SqQueue *p;
    ElemType e;
    InitQueue(p);
    enQueue(p,'a');
    enQueue(p,'b');
    enQueue(p,'c');
    if(QueueEmpty(p))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    deQueue(p,e);
    printf("%c\n",e);
    printf("进队d,e,f后,队列元素个数为:%d\n",QueueLength(p));
    enQueue(p,'d');
    enQueue(p,'e');
    enQueue(p,'f');
    printf("队列元素个数为:%d\n",QueueLength(p));
    while(QueueLength(p)>0)
    {
        deQueue(p,e);
        printf("删除元素%c\n",e);
    }
    DestroyQueue(p);
    printf("已释放队列");
    return 0;
}

运行结果图:
在这里插入图片描述

1.头文件:liqueue.h,包含定义链队数据结构的代码、宏定义、要实现算法的函数的声明;
2.源文件:liqueue.cpp,包含实现各种算法的函数的定义
3.在同一项目(project)中建立一个源文件(如main.cpp),编制main函数,完成相关的测试工作。 包括如下内容:

(1)初始化队列q
(2)依次进队列元素a,b,c
(3)判断队列是否为空
(4)出队一个元素
(5)输出队列中元素个数
(6)依次进队列元素d,e,f
(7)输出队列中元素个数
(8)将队列中所有元素删除,并输出序列
(9)释放队列

main.cpp程序代码:

#include <iostream>
#include "sqqueue.h"
#include "liqueue.h"
using namespace std;

int main()
{
    LiQueue *p;
    ElemType e;
    InitQueue(p);
    enQueue(p,'a');
    enQueue(p,'b');
    enQueue(p,'c');
    if(QueueEmpty(p))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    deQueue(p,e);
    printf("%c\n",e);
    printf("进队d,e,f后,队列元素个数为:%d\n",QueueLength(p));
    enQueue(p,'d');
    enQueue(p,'e');
    enQueue(p,'f');
    printf("队列元素个数为:%d\n",QueueLength(p));
    while(QueueLength(p)>0)
    {
        deQueue(p,e);
        printf("删除元素%c\n",e);
    }
    DestroyQueue(p);
    printf("已释放队列");
    return 0;
}

运行结果图:
在这里插入图片描述
【项目2 – 队列应用】

【题目1】设从键盘输入一整数序列a1,a2,…,an,试编程实现:当ai>0时,ai进队,当ai<0时,将队首元素出队,当ai=0时,表示输入结束。要求将队列处理成环形队列,使用环形队列算法库中定义的数据类型及算法,程序中只包括一个函数(main函数),入队和出队等操作直接在main函数中调用即可。当进队出队异常(如队满)时,要打印出错信息。

程序代码:

#include "malloc.h"
#include"stdio.h"
#include <iostream>
#include<iomanip>
using namespace std;

#define QueueSize 5
typedef int QElemType;
typedef struct QNode {
    QElemType  data[QueueSize];
    int front;
    int rear;
}SqQueue;

int EnQueue(SqQueue& q, QElemType i);
int DeQueue(SqQueue& p, QElemType& j);

int main()
{
        int a;
        QElemType b;
        SqQueue qu;
        qu.rear = qu.front = 0;
        while (1)
        {
            cout << "请输入一整数(输入正数进队,负数出队,0终止输入):" << setw(12);
            cin>>a;
            if (a>0)
            {
                if (EnQueue(qu, a))
                {
                    cout << " 队列已满,不能入队" << endl;
                }
            }
            else if (a<0)
            {
                if (DeQueue(qu, b))
                {
                    cout << "队列为空,不能出队" << endl;
                }
            }
            else
                break;
        }
        cout << "队列中的所有元素为:" << endl;
        int  maxqueue;
        maxqueue = (qu.rear - qu.front + QueueSize) % QueueSize;
        if (maxqueue ==0)
        {
            cout<<"队列为空"<<endl;
        }
        else
        {
                for (int i = 0; i < maxqueue; i++)
                {
                    cout <<qu.data[(qu.front + i) % QueueSize] << setw(6);
                }
        }
        system("pause");
        return 0;
}
int EnQueue(SqQueue &q, QElemType i)
{
    if ((q.rear + 1)% QueueSize == q.front)
    {
        return 1;
    }
    else
    {
        q.data[q.rear] = i;
        q.rear =( q.rear + 1)% QueueSize;
        return 0;
    }
}
int DeQueue(SqQueue &p, QElemType &j)
{
    if (p.rear == p.front)
    {
        return 1;
    }
    else
    {
        j = p.data[p.front];
        p.front = (p.front + 1)% QueueSize;
        cout << "出队元素是:" << j << endl;
        return 0;
    }
}

运行结果图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值