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 }