代码实现
#include
#include
#include
#define MaxSize 100
#define ElemType int
typedef struct{
ElemType data[MaxSize];
int front,rear;
}SeqQueue;
void InitQueue(SeqQueue *queue){
queue->front = 0;
queue->rear = 0;
}
bool QueueEmpty(SeqQueue queue){
return queue.front == queue.rear;
}
bool EnQueue(SeqQueue *queue,ElemType e){
if(queue->rear == MaxSize){
return false;
}
queue->data[queue->rear] = e;
queue->rear++;
return true;
}
bool DeQueue(SeqQueue *queue,ElemType *e){
if(QueueEmpty(*queue))
return false;
*e = queue->data[queue->front];
queue->front++;
return true;
}
bool GetHead(SeqQueue queue,ElemType *e){
if(QueueEmpty(queue))
return false;
*e = queue.data[queue.front];
return true;
}
int main(){
SeqQueue queue;
ElemType Elem;
InitQueue(&queue);
EnQueue(&queue,1);
EnQueue(&queue,2);
EnQueue(&queue,3);
EnQueue(&queue,4);
GetHead(queue,&Elem);
printf("队头元素为%d\n",Elem);
DeQueue(&queue,&Elem);
printf("出队的元素为:%d\n",Elem);
DeQueue(&queue,&Elem);
printf("出队的元素为:%d\n",Elem);
DeQueue(&queue,&Elem);
printf("出队的元素为:%d\n",Elem);
DeQueue(&queue,&Elem);
printf("出队的元素为:%d\n",Elem);
return 0;
}
- 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
- 97
- 98
- 99
- 100