数据结构——循环队列

#include <stdio.h>
#include<stdlib.h>
#include <iostream> 
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
   
typedef int QElemType;
//队列的顺序存储结构 
#define MAXQSIZE 100
typedef struct {
    QElemType *base;
    int front;
    int rear;
}SqQueue;

//初始化空队列
Status InitQueue(SqQueue &Q)
{
    //构造空队列
    Q.base=new QElemType[MAXQSIZE]; //为队列分配一个最大容量为MAXQSIZE的数组空间 
    if(!Q.base) exit(OVERFLOW);
    Q.front=Q.rear=0;  //头指针和尾指针置空,队列为空 
    return OK;
 } 
//打印队列
void OutputQueue(SqQueue Q,int n)
{
    if(Q.front==Q.rear) cout<<"队列为空"; 
    while(Q.front!=Q.rear)
    {
        cout<<Q.base[Q.front]<<"  ";
        Q.front=(Q.front+1)%(n+1); 
    }
    

//判断队空
Status QueueEmpty(SqQueue Q)
{
    return (Q.front==Q.rear);
}

//求队列的长度
int QueueLength(SqQueue Q)
{
    return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

//取队首
Status GetHead(SqQueue Q,QElemType &e)
{
    if(Q.front==Q.rear) return ERROR;
    e=Q.base[Q.front]; //返回队头元素的值,队头元素不变 
    return OK;
}

//出队列
Status DeQueue(SqQueue &Q,QElemType &e)
{
    if(Q.front==Q.rear) return ERROR;
    e=Q.base[Q.front];  //保存队头元素 
    Q.front=(Q.front+1)%MAXQSIZE; //队头指针加一 
    return OK;
}

//入队列
Status EnQueue(SqQueue &Q,QElemType e)
{
    //插入e为新的队尾元素 
    if((Q.rear+1)%MAXQSIZE==Q.front)
    //尾指针在循环意义上加一,表明存储空间已满 
    return ERROR;
    Q.base[Q.rear]=e; //新元素为队尾 
    Q.rear=(Q.rear+1)%MAXQSIZE; //队尾指针加一 
    return OK;
}

//队列的销毁
Status DestoryQueue(SqQueue &Q)
{
    free(Q.base);
    Q.base=NULL;
    Q.front=Q.rear=0;
    
    return OK;
}
 
 
int main(){
    SqQueue Q;
    QElemType e;
    int choice,n,i;    
    cout<<"\n建立循环队列:";
    if (InitQueue(Q)==OVERFLOW){
          cout<<"队列空间分配失败,程序退出!";
         return 0;
         }
    else{
        cout<<"\n元素个数=";
          cin>>n;
          cout<<"\n输入"<<n<<"个队列元素: "; 
          for(int j=0;j<n;j++)
          {
              cin>>i;
              EnQueue(Q,i);    
        }
          cout<<"顺序队列建立成功,队列为:";
        OutputQueue(Q,n);    
    } 
    do { 
        cout<<"\n\n===================================";
        cout<<"\n        循环队列的基本操作           ";
        cout<<"\n===================================";
        cout<<"\n           1:判断队列空" ;
        cout<<"\n           2:求队列的长度" ;
        cout<<"\n           3:取队首" ;
        cout<<"\n           4:出队列" ;         
        cout<<"\n           5:入队列" ;    
        cout<<"\n           6:打印队列" ;    
        cout<<"\n           0:操作结束" ;
        cout<<"\n===================================";
        cout<<"\n请输入你的选择:"; 
        cin>>choice;
        switch (choice){
            case 1:    if(!QueueEmpty)
                    cout<<"此队列为空队列!";
                    else
                    cout<<"此队列非空!";
                       break;
            case 2:    cout<<"此队列的长度为"<<QueueLength(Q);
                       break;
            case 3:    GetHead(Q,e);
                    cout<<"队首为:"<<e; 
                       break;           
            case 4:    DeQueue(Q,e);
                    cout<<"出队列的元素为:"<<e<<"\n当前队列为:";
                    OutputQueue(Q,n); 
                    break;
            case 5:    cout<<"需要入队列的元素为:";
                    cin>>e; 
                    EnQueue(Q,e);                
                    cout<<"\n当前队列为:"; 
                    OutputQueue(Q,n+1); 
                    break;
            case 6:    
                    cout<<"\n队列为:";
                    OutputQueue(Q,n);
                    break;
            case 0:    break; 
            default:cout<<"\n输入错误,重新输入!";            
        }
    } while (choice) ;    
    DestoryQueue(Q);    
    return 0; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值