-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-2.c
More file actions
88 lines (82 loc) · 1.34 KB
/
4-2.c
File metadata and controls
88 lines (82 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 10
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue *Q)
{
(*Q).base=(QElemType *)malloc(MAXSIZE*sizeof(QElemType));
if(!(*Q).base)
return ERROR;
(*Q).front=(*Q).rear=0;
return OK;
}
Status EnQueue(SqQueue *Q,QElemType e)
{
if((*Q).rear==MAXSIZE)
return ERROR;
(*Q).base[(*Q).rear]=e;
(*Q).rear=((*Q).rear+1)%MAXSIZE;
return OK;
}
Status DeQueue(SqQueue *Q,QElemType *e)
{
if((*Q).rear==(*Q).front)
return ERROR;
e=(*Q).base[(*Q).front];
(*Q).front=((*Q).front+1)%MAXSIZE;
return OK;
}
void OutputQueue(SqQueue Q)
{
while(Q.front!=Q.rear)
{
printf("%d ",Q.base[Q.front]);
Q.front=(Q.front+1)%MAXSIZE;
}
printf("\n");
}
void main()
{
SqQueue Q;
InitQueue(&Q);
int op,x;
while(1)
{
printf("请您选择: 1.进队 2.出队 0.退出==>");
scanf("%d",&op);
switch(op)
{
case 0:
return ;
case 1:
printf("请输入进队元素: ");
scanf("%d",&x);
if(!EnQueue(&Q,x))
printf("队列满!\n");
else
{
printf("进队成功,队内元素为:\n");
OutputQueue(Q);
}
break;
case 2:
if(DeQueue(&Q,&x))
{
printf("出队元素为: [%d],队内元素为: \n",x);
OutputQueue(Q);
}
else
printf("队空! \n");
break;
}
}
}