数据结构--实验2--队列的操作

  1 #include"stdio.h"
  2 #include"malloc.h"
  3 typedef int datatype;
  4 
  5 #define MAXSIZE 50 //队列的最大容量
  6 //定义队列结构体
  7 typedef struct
  8 {
  9     datatype data[MAXSIZE];
 10     int front,real;
 11     int num; //队列中元素的个数 
 12 } c_SeQueue;
 13 //置空队
 14 c_SeQueue* Init_SeQueue(){
 15     c_SeQueue *q;
 16     q=new c_SeQueue;
 17     q->front=q->real=MAXSIZE-1;
 18     q->num=0;
 19     return q; 
 20 }
 21 //入队
 22 int In_SeQueue(c_SeQueue *q,datatype x){
 23     if(q->num==MAXSIZE){
 24         printf("队满");
 25         return -1;
 26     }
 27     else{
 28         q->real=(q->real+1) % MAXSIZE;
 29         q->data[q->real]=x;
 30         q->num++;
 31         return 1;
 32         
 33     }
 34 } 
 35 //出队
 36 int Out_SeQueue(c_SeQueue *q,datatype *x){
 37     if(q->num==0){
 38         printf("队空");
 39         return -1;
 40     }
 41     else{
 42         *x=q->data[q->front];
 43         q->front=(q->front+1) % MAXSIZE; 
 44         *x=q->data[q->front];
 45         q->num--;
 46         return 1;
 47     }
 48     
 49 } 
 50 //判断空
 51 int isEmpty(c_SeQueue *q)
 52 {
 53     if(q->num==0)
 54         return 1;
 55         else
 56         return 0;
 57     
 58 } 
 59 //显示队列
 60 void displayQueue(c_SeQueue q){
 61 
 62     while((q.front)!=q.real)
 63     {
 64         q.front=(q.front+1)%MAXSIZE;
 65         printf("%-3d",q.data[q.front]);
 66         
 67     }
 68 
 69 }
 70 void main()
 71 {
 72     c_SeQueue  *sq;
 73 //    LQueue  *Lq;
 74     int j,k;
 75     datatype x;
 76     sq=Init_SeQueue();
 77    // Lq=Init_Queue();
 78     do
 79     {   
 80     printf("\n\n\n\n");
 81     printf("\t\t\t 队列子系统\n");
 82     printf("\t\t*******************************\n");
 83     printf("\t\t*        1----进 队 列     *\n");
 84     printf("\t\t*        2----出 队 列    *\n");
 85     printf("\t\t*        3----队列空否    *\n");
 86     printf("\t\t*        4----显示队列    *\n");
 87     printf("\t\t*        0----返  回    *\n");
 88     printf("\t\t*******************************\n");
 89     printf("\t\t 请选择菜单项(0-4):");
 90     scanf("%d",&k);getchar();
 91     switch(k){
 92         case 1:
 93         printf("\n   请输入要进队列的数据:");
 94         scanf("%d",&x);
 95         j=In_SeQueue(sq,x);
 96         break;
 97         case 2:
 98             j=Out_SeQueue(sq,&x);
 99         break;
100         case 3:
101             j=isEmpty(sq);
102             if(j==1)
103                 printf("此队列为空");
104                 else
105                 printf("队列不为空");
106         break;
107         case 4:
108             printf("\n   队列元素有:  ");
109             displayQueue(*sq);
110         break;
111     
112         
113     }
114     }while(k!=0);
115 }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

5210丫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值