循环队列的基本操作

实验题目循环队列的基本操作

• 【实验目的】

• 理解并掌握栈和队列的逻辑结构和存储结构;

• 理解栈和队列的相关基本运算;

• 编程对相关算法进行验证;

学会利用栈和队列解决实际问题

• 实验内容:编写一个程序,实现循环队列的各种基本运算,并在此基础上设计一个主程序完成如下功能:

• 1)初始化队列q

• 2)判断队列q是否为空;

• 3)依次进队列元素-1,2,10

• 4)出队一个元素,并输出该元素;

• 5)输出队列的长度(元素个数);

• 6)依次进队元素-3,12,10

• 7)输出出队序列;

• 8)判断队列q是否为空。


头文件Queue.h

#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
#include <bits/stdc++.h>
using namespace std; //cout,cin
//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define QElemType int
//OVERFLOW 在 math.h 中已定义为3

typedef int Status;
typedef int Boolean; // 布尔类型

//为了操作的方便,建议本书每一个程序都把c1.h包含进去
#define MAXQSIZE 100
typedef struct{
    QElemType *base;
    int front;
    int rear;
}SqQueue;
Status InitQueue(SqQueue &Q);
int QueueLength(SqQueue Q);
Status EnQueue(SqQueue &Q,QElemType e);
Status DEQueue(SqQueue &Q,QElemType &e);
Status QueueEmpty(SqQueue &Q);
#endif // QUEUE_H_INCLUDED

Queue.cpp

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

Status InitQueue(SqQueue &Q)
{
    //构造一个空队列Q
    Q.base = (QElemType * )malloc(MAXQSIZE *sizeof(QElemType));
    if(!Q.base) exit (OVERFLOW);//存储空间分配失败
    Q.front = Q.rear = 0;
    return OK;
}
int QueueLength(SqQueue Q)
{
    //返回Q的元素个数,即队列的长度
    return(Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
Status EnQueue(SqQueue &Q,QElemType e)
{
    //插入元素e为Q的新的对为元素
    if((Q.rear + 1) % MAXQSIZE == Q.front)
        return ERROR;//队列满
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXQSIZE;
    return OK;
}
Status DEQueue(SqQueue &Q,QElemType &e)
{
    //若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
    //否则返回ERRPR
    if(Q.front == Q.rear)
        return ERROR;
    e = Q.base[Q.front];
    Q.front = (Q.front + 1) % MAXQSIZE;
    return OK;
}
Status QueueEmpty(SqQueue &Q)
 { //若队列Q为空队列,则返回TRUE,否则返回FALSE
   if(Q.front==Q.rear) //队列空的标志
     return TRUE;
   else
     return FALSE;
 }
main.cpp

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

int main()
{
    SqQueue Q;
    QElemType e;
    cout << "①初始化队列Q:"<< endl;
    InitQueue(Q);
    cout << "②判断队列Q是否为空:"<< endl;
    if(QueueEmpty(Q)) cout << "空"<< endl;
    else cout << "不空"<< endl;
    cout << "③依次进队列元素-1,2,10:";
    EnQueue(Q,-1);
    EnQueue(Q,2);
    EnQueue(Q,10);
    cout << endl;
    cout << "④出队一个元素,并输出该元素:";
    DEQueue(Q,e);cout << e <<endl;
    cout << "⑤输出队列的长度:"<< QueueLength(Q)<< endl;
    cout << "⑥依次进队元素-3,12,10:";
     EnQueue(Q,-3);
     EnQueue(Q,12);
     EnQueue(Q,10);
     cout << endl;
     cout << "输出出队序列:";
     for(int i = 1;i <= 5;i++)
        {
            DEQueue(Q,e);
            cout << e << " ";
        }
        cout << endl;
    cout << "判断队列是否为空:"<< endl;
    if(QueueEmpty(Q)) cout << "空"<< endl;
    else cout << "不空"<< endl;
    return 0;
}


循环队列基本操作包括初始化、入队、出队、求队列长度、求队头元素、判空、清空和销毁等。以下是循环队列基本操作的实现方法: 1. 初始化:创建一个空的循环队列,需要指定队列的最大长度。 2. 入队:将元素插入到队列的队尾,如果队列已满则无法插入。 3. 出队:将队列的队头元素删除并返回,如果队列为空则无法删除。 4. 求队列长度:返回队列中元素的个数。 5. 求队头元素:返回队列的队头元素,但不删除。 6. 判空:判断队列是否为空。 7. 清空:清空队列中的所有元素。 8. 销毁:销毁队列,释放内存空间。 以下是一个基于顺序表实现的循环队列的代码示例: ``` #define MAXSIZE 100 // 队列的最大长度 typedef struct { int data[MAXSIZE]; // 存储队列元素的数组 int front; // 队头指针 int rear; // 队尾指针 } CircularQueue; // 初始化循环队列 void InitQueue(CircularQueue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int IsEmpty(CircularQueue *q) { return q->front == q->rear; } // 判断队列是否已满 int IsFull(CircularQueue *q) { return (q->rear + 1) % MAXSIZE == q->front; } // 入队 int EnQueue(CircularQueue *q, int x) { if (IsFull(q)) { return 0; // 队列已满,无法插入 } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; return 1; } // 出队 int DeQueue(CircularQueue *q, int *x) { if (IsEmpty(q)) { return 0; // 队列为空,无法删除 } *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } // 求队列长度 int QueueLength(CircularQueue *q) { return (q->rear - q->front + MAXSIZE) % MAXSIZE; } // 求队头元素 int GetHead(CircularQueue *q, int *x) { if (IsEmpty(q)) { return 0; // 队列为空,无法获取队头元素 } *x = q->data[q->front]; return 1; } // 清空队列 void ClearQueue(CircularQueue *q) { q->front = q->rear = 0; } // 销毁队列 void DestroyQueue(CircularQueue *q) { free(q); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值