-
Notifications
You must be signed in to change notification settings - Fork 0
/
队列(顺序表).c
97 lines (90 loc) · 1.5 KB
/
队列(顺序表).c
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
89
90
91
92
93
94
95
96
#include<stdio.h>
#include<stdlib.h>
#define Status int
#define MaxSize 10
#define Datetype int
typedef struct queue{
int front;
int rear;
Datetype *base;
}Queue;
//初始化队列
Status InitQueue(Queue * queue)
{
queue->base=(Datetype *)malloc(sizeof(Datetype)*MaxSize);
if(!queue->base) exit(-1);
queue->front=queue->rear=0;
return 1;
}
//求队列长度
int QueueLength(Queue *q)
{
return (q->rear-q->front+MaxSize)%MaxSize;
}
//判断队列是否为满
Status isFull(Queue *q)
{
if((q->rear+1)%MaxSize==q->front) return 1;
else return 0;
}
//判断队列是为空
Status isEmpty(Queue *q)
{
if(q->front==q->rear) return 1;
else return 0;
}
//入队
Status Enqueue(Queue *q,Datetype e)
{
if(isFull(q)) return 0;//如果队列为满,返回false
q->base[q->rear]=e;
q->rear=(q->rear+1)%MaxSize;
return 1;
}
//出队
Status Dequeue(Queue *q)
{
if(isEmpty(q)) return 0;
q->front=(q->front+1)%MaxSize;
return 1;
}
//得到对头元素
Datetype Gethead(Queue *q)
{
if(isEmpty(q)) exit(-1);
return q->base[q->front];
}
//销毁队列
void Destoty(Queue *q)
{
free(q->base);
}
//遍历
void Queuedisplay(Queue *q)
{
int i;
printf("队列元素为:");
for(int i=0;i<QueueLength(q);i++)
{
printf("%d ",q->base[q->front+i]);
}
printf("\n");
}
int main()
{
Queue q;
InitQueue(&q);
Enqueue(&q,1);
Enqueue(&q,2);
Enqueue(&q,3);
Enqueue(&q,4);
printf("队列长度为:%d\n",QueueLength(&q));
printf("队头元素为: %d\n",Gethead(&q));
Queuedisplay(&q);
Dequeue(&q);
Queuedisplay(&q);
Dequeue(&q);
Queuedisplay(&q);
Dequeue(&q);
Queuedisplay(&q);
}