队列结构实现

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
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>


typedef struct Queue
{
int * pBase;
int front;
int rear;
}QUEUE;

/*
队列的实现



*/
//初始化
void init_queue(QUEUE *);
void init_queue(QUEUE *phead){
phead->pBase = (int*)malloc(sizeof(int) * 6);
phead->front = 0;
phead->rear = 0;
}
//入队
bool en_queue(QUEUE *,int);
bool full_queue(QUEUE *);
bool is_timy(QUEUE *);
bool de_queue(QUEUE *, int*);
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
bool en_queue(QUEUE *phead,int val){
if (full_queue(phead))
return false;
phead->pBase[phead->rear] = val;
phead->rear = (phead->rear + 1) % 6;
return true;
}
//出队
bool de_queue(QUEUE *phead, int *val){
if (is_timy(phead))
return false;
*val = phead->pBase[phead->front];
phead->front = (phead->front + 1) % 6;
return true;
}
//是否为空
bool is_timy(QUEUE *phead){
if (phead->front == phead->rear)
return true;
return false;
}
//是否饱和
bool full_queue(QUEUE * phead){
if (phead->front==(phead->rear+1)%6)
return true;
return false;
}
//遍历
void taverse_queue(QUEUE *);
void taverse_queue(QUEUE * phead){
int f = phead->front;
int r = phead->rear;
if (is_timy(phead))
return;
while (f!=r)
{
printf("%d\n", phead->pBase[f]);
f = (f + 1) % 6;
}
}

void main(){
QUEUE queue;
int val;
init_queue(&queue); //初始化
if (is_timy(&queue)) //是否为空
printf("空队列");
en_queue(&queue, 1); //入队
en_queue(&queue, 2);
en_queue(&queue, 3);
en_queue(&queue, 4);
en_queue(&queue, 5);
taverse_queue(&queue); //遍历
if (full_queue(&queue)) //是否饱和
printf("饱和");
en_queue(&queue, 6);
if (full_queue(&queue)) //是否饱和
printf("饱和");
de_queue(&queue, &val);
de_queue(&queue, &val);
de_queue(&queue, &val);
de_queue(&queue, &val);
de_queue(&queue, &val);
de_queue(&queue, &val);
taverse_queue(&queue);
de_queue(&queue, &val); //出队
printf("%d", val);


}

版权声明:本文为博主原创,如若转载请标明出处https://dword.top/队列实现.html

-------------end-------------
0%