复制代码
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
typedef int ElementType; typedef int Status; #define OK 1 #define TRUE 1 #define FALSE 0 #define ERROR 0 #define MAXSIZE 100 typedef struct { ElementType data[MAXSIZE]; int front;/*头指针*/ int rear;/*尾结点,如果不为空的话,指向队尾元素的下一个位置*/ }SqQueue; /*初始化一个空队列*/ Status initQueue(SqQueue *Q){ Q->front = 0; Q->rear = 0; return OK; } /*求循环队列的长度*/ int QueueLength(SqQueue Q) { return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; } Status isFull(SqQueue *Q){ if ((Q->rear + 1) % MAXSIZE == Q->front) { return TRUE; } return FALSE; } /*如果队列未满,则插入元素e为Q新的队尾元素*/ Status EnQueue(SqQueue *Q,ElementType e) { if (isFull(Q)) { return ERROR; } Q->data[Q->rear] = e; Q->rear = (Q->rear + 1) % MAXSIZE;/*防止出现到了数组的末端*,如果到了末端就转到数组头部*/ return OK; } /*循环出队列操作*/ Status DeQueue(SqQueue *Q,ElementType *e) { if (Q->front == Q->rear){/*空栈的判断*/ return ERROR; } *e = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE;/*将头指针向后移位*/ return OK; }
转载于:https://my.oschina.net/ccqy66/blog/484456
最后
以上就是糊涂胡萝卜最近收集整理的关于数据结构-队列顺序结构的实现和操作的全部内容,更多相关数据结构-队列顺序结构内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复