队列是特殊的线性表;
队列仅在线性表两端进行操作;
队头(Front):取出数据的一端;
队尾(Rear):放入数据的一端;
性质: 先进先出。
长度= rear -front
=(rear - front +size) %size SIZE是容量,上图中容量是11,长度10
判断队满 : rear + 1 == front
(rear + 1) % size == front
头文件
#ifndef SEQUENCEQUEUE_H
#define SEQUENCEQUEUE_H
#define SIZE 10
#define SUCCESS 1000
#define FAILURE 1001
#define TRUE 1002
#define FALSE 1003
struct SequenceQueue
{
int *data;
int front;
int rear;
};
typedef struct SequenceQueue Queue;
int InitQueue(Queue *q);
int push(Queue *q, int e);
int EmptyQueue(Queue q);
int LengthQueue(Queue q);
int pop(Queue *q);
int ClearQueue(Queue *q);
int DestroyQueue(Queue *q);
int GetTop(Queue q);
#endif
调用子函数,函数文件
#include "SequenceQueue.h"
#include <stdlib.h>
/*
函数描述:队列的初始化
函数参数:队列的地址
函数返回值:成功返回SUCCESS,失败返回FAILURE;
*/
int InitQueue(Queue *q)
{
if (NULL == q)
return FAILURE;
q->data = (int *)malloc(sizeof(int) * SIZE);
if (NULL == q->data)
{
return FAILURE;
}
q->front = q->rear = 0;
return SUCCESS;
}
/*
函数描述:元素进队
函数参数:输入元素,队列地址
函数返回值:成功返回SUCCESS,失败返回FAILURE
*/
int push(Queue *q, int e)
{
if (NULL == q)
return FAILURE;
if (NULL == q->data)
return FAILURE;
if((q->rear + 1) % SIZE == q->front)
{
return FAILURE;
}
q->data[q->rear] = e;
q->rear = (q->rear + 1) % SIZE;
return SUCCESS;
}
/*
函数描述:判断队列是否为为空
函数参数:队列
函数返回值:空返回TRUE,不空返回FALSE
*/
int EmptyQueue(Queue q)
{
return (q.front == q.rear) ? TRUE : FALSE;
}
int LengthQueue(Queue q)
{
return (q.rear - q.front + SIZE) % SIZE;
}
/*
函数描述:队列里元素出队
函数参数:队列的地址
函数返回值:失败返回FAILURE,成功返回出队元素
*/
int pop(Queue *q)
{
if (NULL == q)
return FAILURE;
if(q->rear == q->front)
return FAILURE;
int i = q->data[q->front];
q->front = (q->front + 1) % SIZE;
return i;
}
/*
函数描述:清空队列
函数参数:队列的地址
函数返回值:失败返回FAILURE,成功返回SUCCESS
*/
int ClearQueue(Queue *q)
{
if (NULL == q)
return FAILURE;
q->front = q->rear;
return SUCCESS;
}
/*
函数描述:清除队列
函数参数:队列的地址
函数返回值:失败返回FAILURE,成功返回SUCCESS
*/
int DestroyQueue(Queue *q)
{
if (NULL == q)
return FAILURE;
free(q->data);
q->data = NULL;
return SUCCESS;
}
int GetTop(Queue q)
{
return (q.rear == q.front) ? FAILURE :q.data[q.front];
}
主函数执行文件
#include <stdio.h>
#include "SequenceQueue.h"
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
Queue queue;
int ret;
ret = InitQueue(&queue);
if (SUCCESS == ret)
{
printf("初始化成功!\n");
}
else
{
printf("初始化失败!\n");
}
int i, num;
for (i = 0; i < 10; i++)
{
num = rand() % 10;
ret = push(&queue, num);
if (SUCCESS == ret)
{
printf("%d进队成功!\n", num);
}
else
{
printf("%d进队失败!\n", num);
}
}
ret = GetTop(queue);
if (FAILURE == ret)
{
printf("没有队头\n");
}
else
{
printf("队头是%d\n", ret);
}
ret = EmptyQueue(queue);
if (TRUE == ret)
{
printf("队列为空!\n");
}
else
{
printf("队列不为空!\n");
}
printf("队列长度%d\n", LengthQueue(queue));
for (i = 0; i < 5; i++)
{
ret = pop(&queue);
if (FAILURE == ret)
{
printf("出队失败!\n");
}
else
{
printf("%d出队成功!\n", ret);
}
}
ret = ClearQueue(&queue);
if (FAILURE == ret)
{
printf("清空失败!\n");
}
else
{
printf("清空成功!\n");
}
ret = EmptyQueue(queue);
if (TRUE == ret)
{
printf("队列为空!\n");
}
else
{
printf("队列不为空!\n");
}
ret = DestroyQueue(&queue);
if (SUCCESS == ret)
{
printf("销毁成功!\n");
}
else
{
printf("销毁失败!\n");
}
for (i = 0; i < 10; i++)
{
num = rand() % 10;
ret = push(&queue, num);
if (SUCCESS == ret)
{
printf("%d进队成功!\n", num);
}
else
{
printf("%d进队失败!\n", num);
}
}
return 0;
}