头文件:head.h
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
#define MAXQSIZE 10 //最大队列长度
typedef int QElemType;
typedef struct{
QElemType *base; //初始化的动态非配存储空间
int front; //头指针,若队列不为空,指向队列头元素
int rear; //尾指针,若队列不为空,指向队列尾元素的下一个位置
}SqQueue;
Status InitQueue(SqQueue *Q);
Status DestoryQueue(SqQueue *Q);
Status ClearQueue(SqQueue *Q);
Boolean QueueEmtpy(SqQueue Q);
int QueueLength(SqQueue Q);
Status GetHead(SqQueue Q, QElemType *e);
Status EnQueue(SqQueue *Q, QElemType e);
Status DeQueue(SqQueue *Q, QElemType *e);
Status QueueTraverse(SqQueue Q);
算法实现:
#include"head.h"
Status InitQueue(SqQueue *Q)
{
(*Q).base = (SqQueue *)malloc(MAXQSIZE * sizeof(SqQueue));
if (!(*Q).base)
{
printf("初始化失败!");
system("pause");
exit(-1);
}
(*Q).front = (*Q).rear = 0;
return OK;
}
Status DestoryQueue(SqQueue *Q)
{
if ((*Q).base)
free((*Q).base);
(*Q).base = NULL;
(*Q).front = (*Q).rear = 0;
return OK;
}
Status ClearQueue(SqQueue *Q)
{
DestoryQueue(Q);
InitQueue(Q);
return OK;
}
Boolean QueueEmtpy(SqQueue Q)
{
if (Q.rear == Q.front)
return TRUE;
else
return ERROR;
}
int QueueLength(SqQueue Q)
{
if (QueueEmtpy(Q))
return 0;
return (Q.rear + MAXQSIZE - Q.front) % MAXQSIZE;
}
Status GetHead(SqQueue Q, QElemType *e)
{
if (QueueEmtpy(Q))
return ERROR;
*e = (Q.base)[Q.front];
return OK;
}
Status EnQueue(SqQueue *Q, QElemType e)
{
//插入元素e为Q的队尾元素
if (((*Q).rear + 1) % MAXQSIZE == (*Q).front) //队列满
return ERROR;
(*Q).base[(*Q).rear] = e;
(*Q).rear = ((*Q).rear + 1) % MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue *Q, QElemType *e)
{
if (QueueEmtpy(*Q))
return ERROR;
*e = ((*Q).base)[(*Q).front];
(*Q).front = ((*Q).front + 1) % MAXQSIZE;
return OK;
}
Status QueueTraverse(SqQueue Q)
{
int n = 0;
int len = QueueLength(Q);
while (n < len)
{
printf("%d ", Q.base[n]);
n++;
}
printf("\n");
return OK;
}
测试文件:
#include"head.h"
void main()
{
Status j;
int i = 0, l;
QElemType d;
SqQueue Q;
InitQueue(&Q);
printf("初始化队列后,队列空否?%u(1:空 0:否)\n", QueueEmtpy(Q));
printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ", MAXQSIZE - 1);
do
{
scanf_s("%d", &d);
if (d == -1)
break;
i++;
EnQueue(&Q, d);
} while (i<MAXQSIZE - 1);
printf("队列长度为: %d\n", QueueLength(Q));
printf("现在队列空否?%u(1:空 0:否)\n", QueueEmtpy(Q));
printf("连续%d次由队头删除元素,队尾插入元素:\n", MAXQSIZE);
for (l = 1; l <= MAXQSIZE; l++)
{
DeQueue(&Q, &d);
printf("删除的元素是%d,请输入待插入的元素: ", d);
scanf_s("%d", &d);
EnQueue(&Q, d);
}
l = QueueLength(Q);
printf("现在队列中的元素为: \n");
QueueTraverse(Q);
printf("共向队尾插入了%d个元素\n", i + MAXQSIZE);
if (l - 2>0)
printf("现在由队头删除%d个元素:\n", l - 2);
while (QueueLength(Q)>2)
{
DeQueue(&Q, &d);
printf("删除的元素值为%d\n", d);
}
j = GetHead(Q, &d);
if (j)
printf("现在队头元素为: %d\n", d);
ClearQueue(&Q);
printf("清空队列后, 队列空否?%u(1:空 0:否)\n", QueueEmtpy(Q));
DestoryQueue(&Q);
system("pause");
}
初始化队列后,队列空否?1(1:空 0:否)
请输入整型队列元素(不超过9个),-1为提前结束符: 1 2 3 4 5 6 7 8
-1
队列长度为: 8
现在队列空否?0(1:空 0:否)
连续10次由队头删除元素,队尾插入元素:
删除的元素是1,请输入待插入的元素:
9
删除的元素是2,请输入待插入的元素: 8
删除的元素是3,请输入待插入的元素: 7
删除的元素是4,请输入待插入的元素: 6
删除的元素是5,请输入待插入的元素: 5
删除的元素是6,请输入待插入的元素: 5
删除的元素是7,请输入待插入的元素: 3
删除的元素是8,请输入待插入的元素: 2
删除的元素是9,请输入待插入的元素: 1
删除的元素是8,请输入待插入的元素: 0
现在队列中的元素为:
7 6 5 5 3 2 1 0
共向队尾插入了18个元素
现在由队头删除6个元素:
删除的元素值为7
删除的元素值为6
删除的元素值为5
删除的元素值为5
删除的元素值为3
删除的元素值为2
现在队头元素为: 1
清空队列后, 队列空否?1(1:空 0:否)
请按任意键继续. . .