头文件
#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include<stdio.h>
#include<stdlib.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
struct QueueNode //表示队列结点的信息
{
int data;
struct Queue *next;
};
typedef struct QueueNode node;
struct QueueInfo //表示队列的信息
{
node *front;
node *rear;
};
typedef struct QueueInfo Queue;
int InitQueue(Queue *q);
int push(Queue *q, int e);
int EmptyQueue(Queue q);
int GetTop(Queue q);
int pop(Queue *q);
int ClearQueue(Queue *q);
int DestroyQueue(Queue *q);
#endif
调用子函数,函数文件
#include"LinkQueue.h"
/*
函数描述:链式队列初始化
函数参数:队列的地址
函数返回值:成功返回SUCCESS,失败返回FAILURE;
*/
int InitQueue(Queue *q)
{
if(NULL == q)
return FAILURE;
q -> front = (node*)malloc(sizeof(node)*1);
if(NULL == q -> front)
{
return FAILURE;
}
q -> rear = q -> front;
q -> front -> next = NULL;
return SUCCESS;
}
/*
函数描述:输入的元素进队(从队尾进,相当于尾插法)
*/
int push(Queue *q, int e)
{
if(NULL == q)
return FAILURE;
if(q -> front == NULL)
return FAILURE;
node *n = q ->rear;
q -> rear = (node*)malloc(sizeof(node)*1);
n -> next = q -> rear;
q -> rear -> data = e;
q -> rear -> next = NULL;
return SUCCESS;
}
/*
函数描述:判断队列是否为空
*/
int EmptyQueue(Queue q)
{
return (q.front == q.rear) ? TRUE :FALSE;
}
/*
函数描述:获取队头元素
*/
int GetTop(Queue q)
{
return (q.rear == q.front) ? FAILURE : q.front -> next ->data;
}
/*
函数描述:让队列里的元素出队(从队头出)
*/
int pop(Queue *q)
{
if(NULL ==q)
return FAILURE;
if(q -> front == q -> next) //空对
{
return FAILURE;
}
node *n = q -> front -> next;
if(!(n -> next)) //只有一个结点
{
q -> rear = q -> front;
}
q -> front -> next = n -> next;
int e = n -> data;
free(n);
return e;
}
/*
函数描述:清空队列
*/
int ClearQueue(Queue *q)
{
if(NULL == q)
return FAILURE;
node *n = q -> front -> next;
while(n)
{
q -> front -> next = n -> next;
free(n);
n = q -> front -> next;
}
q -> rear = q -> front;
return SUCCESS;
}
/*
函数描述:清除队列
*/
int DestoryQueue(Queue *q)
{
if(NULL == q)
return FAILURE;
free(q -> front);
q -> front = q -> rear = NULL;
return SUCCESS;
}
主函数执行文件
#include"LinkQueue.h"
#include<time.h>
int main()
{
srand(time(NULL));
Queue queue;
int ret, i, num;
ret = InitQueue(&queue);
if(SUCCESS == ret)
{
printf("初始化成功!\n");
}
else
{
printf("初始化失败!\n");
}
ret = EmptyQueue(queue);
if(TRUE == ret)
{
printf("队列是空!\n");
}
else
{
printf("队列不是空!\n");
}
for(i = 0; i < 6; i++)
{
num =rand () %10;
ret = push(&queue, num);
if(FAILURE == 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;
}