1

1
/**//*==============循环队列--队列的顺序存储表示形式====================*/
2
#include <stdio.h>
3
#define MAXQSIZE 20
4
5
typedef struct
{/**//*====循环队列的类型定义===*/
6
char *base;
7
int front;
8
int rear;
9
}sqque;
10
/**//*==========常用的被调用函数定义=================*/
11
int quelength(sqque q);
12
13
void print(sqque q)
{
14
int a,i;
15
i=quelength(q);
16
a=q.front;
17
printf("The queue:");
18
while(i--)
{
19
printf("%c ",q.base[a]);
20
a=(a+1)%MAXQSIZE;
21
}
22
}
23
24
/**//*=============对循环队列进行操作的函数定义==============*/
25
26
int initque(sqque *q)
{/**//*初始化一个循环队列*/
27
q->base=(char *)malloc(MAXQSIZE*sizeof(char));
28
if(!q->base) exit(0);
29
q->front=q->rear=0;
30
return 1;
31
}
32
33
34
int quelength(sqque q)
{/**//*返回循环队列的元素个数*/
35
return (q.rear-q.front+MAXQSIZE)%MAXQSIZE;/**//*===*/
36
}
37
38
39
int enque(sqque *q,char e)
{/**//*入队函数*/
40
if((q->rear+1)%MAXQSIZE == q->front) return 0;
41
q->base[q->rear]=e;/**//*===指针的运算。。。===*/
42
q->rear=(q->rear+1) % MAXQSIZE;
43
return 1;
44
}
45
46
47
int deque(sqque *q,char *e)
{/**//*删除对头元素*/
48
if(q->front == q->rear) return 0;
49
*e=q->base[q->front];
50
q->front=(q->front+1) % MAXQSIZE;
51
return 1;
52
}
53
54
55
/**//*===================主函数部分==================*/
56
main()
{
57
int i=0;
58
char tem='A',a,*x;
59
sqque *squ,sque;
60
squ=&sque;
61
initque(squ);
62
for(i=1;i<=16;i++)
{
63
enque(squ,tem++);
64
}
65
66
print(sque);
67
printf("\nThere are %d elements in this queue\n",quelength(sque));
68
x=&a;
69
deque(squ,x);
70
printf("\nAfter delque,");
71
print(sque);
72
printf("\nThe deleted element:%c",a);
73
74
75
76
getch();
77
78
}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101


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

89

90

91

92

93

94

95

96

97

98

99

100

101
