.c
/***************************************
* author:贺永虎
* mail:xxxxxx
* desc:队列的线性实现函数
*
* **************************************/
#include "SqQueue.h"
/*
return:
OK:表示初始化成功
OVERFLOW:表示初始化失败,内存为开成功
parm:
*Q:表示要开辟的空间的线性表的地址
*/
int InitQueue(SqQueue *Q)//构造一个空队列Q
{
Q->base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
if (!Q->base)
{
printf("init memory overflow\n");
exit (OVERFLOW);
}
Q->front = Q->rear = 0;
return OK;
}
/*
return:
无
parm:
*Q:要置空的循环队列
*/
void ClearQueue(SqQueue *Q)//将队列Q清空
{
Q->front = Q->rear;
}
/*
return:
TRUE:为空
FALSE:表示非空
parm:
*Q:表示要判断的循环队列
*/
int QueueEmpty(SqQueue *Q)//判断队列Q是否为空队列
{
if (Q->front == Q->rear)
return TRUE;
else
return FALSE;
}
/*
return:
循环队列元素个数
parm:
*Q:表示要判断元素个数的循环队列
*/
int QueueLength(SqQueue *Q)//返回Q的元素个数,即队列Q的长度
{
return (Q->rear - Q->front + MAXQSIZE) % MAXQSIZE;
}
/*
return:
OK:表示成功
FALSE:表示失败
parm:
*Q:循环队列
*e:获取的队头元素
*/
int GetHead(SqQueue *Q, QElemType *e)
{
if (Q == NULL)
return FALSE;
int data = (Q->front) % MAXQSIZE;
*e = Q->base[data];
return OK;
}
/*
return:
OK:表示成功
FALSE:表示失败
parm:
*Q:循环队列
e:要入队的元素
*/
int 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;
}
/*
return:
OK:表示成功
FALSE:表示失败
parm:
*Q:循环队列
*e:出队的元素
*/
int DeQueue(SqQueue *Q, QElemType *e)//若队列不空,则删除Q的对头元素,用e返回其值,并返回OK,否则返回ERROR
{
if (QueueEmpty(&Q))//判断是否为空
return ERROR;
*e = Q->base[Q->front];
Q->front = (Q->front + 1) % MAXQSIZE;
return OK;
}
/*
retuurn:
无
parm:
Q:循环队列
*/
void QueueTraverse(SqQueue *Q)
{
if (Q->base == NULL)
printf("ERROR\n");
int p = Q->front;
while (p%MAXQSIZE != Q->rear)
{
printf("%d ", Q->base[p]);
p++;
}
printf("\n");
}
/*
return:
无
parm:
*Q:要销毁的循环队列
*/
void DestroyQueue(SqQueue *Q)
{
free(Q->base);
Q->base = NULL;
Q->front = Q->rear;
}
.h
#ifndef SqQueue_H
#define SqQueue_H
#include <stdio.h>
#include <stdlib.h>
//定义宏常量
#define MAXQSIZE 100
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
//设置数据类型
typedef int QElemType;
typedef struct
{
QElemType *base;//初始化的动态分配存储空间
int front;//头指针,若队列不空,指向队列头元素
int rear; //尾指针,若队列不空,指向队列尾元素的下一个位置
} SqQueue;
//如下为所有队列的函数声明
int InitQueue(SqQueue *Q);//构造一个空队列Q
void ClearQueue(SqQueue *Q);//将队列Q清空
int QueueEmpty(SqQueue *Q);//判断队列Q是否为空队列
int QueueLength(SqQueue *Q);//返回Q的元素个数,即队列Q的长度
int GetHead(SqQueue *Q, QElemType *e);//返回队首元素
int EnQueue(SqQueue *Q, QElemType e);//插入元素e为Q的新的队尾元素
int DeQueue(SqQueue *Q, QElemType *e);//若队列不空,则删除Q的对头元素,用e返回其值,并返回OK,否则返回ERROR
void QueueTraverse(SqQueue *Q);//遍历队列Q
void DestroyQueue(SqQueue *Q);//销毁队列Q
#endif
main.c
#include"SqQueue.h"
//如下为验证过程
int main()
{
SqQueue Q;
int stat = InitQueue(&Q);
int x;
if(stat == OVERFLOW)
{
printf("init error\n");
}
EnQueue(&Q, 1);
EnQueue(&Q, 2);
EnQueue(&Q, 3);
EnQueue(&Q, 4);
EnQueue(&Q, 5);
printf("length = %d\n", QueueLength(&Q));
QueueTraverse(&Q);
DeQueue(&Q, &x);
printf("x = %d\n", x);
printf("length = %d\n", QueueLength(&Q));
GetHead(&Q, &x);
printf("head = %d\n", x);
ClearQueue(&Q);
printf("length = %d\n", QueueLength(&Q));
QueueTraverse(&Q);
DestroyQueue(&Q);
return 0;
}
main:main.o SqQueue.o
gcc -g main.o SqQueue.o -o main
main.o:main.c SqQueue.h
gcc -g -c main.c -o main.o
SqQueue.o:SqQueue.c SqQueue.h
gcc -g -c SqQueue.c -o SqQueue.o
clean:
rm -fr *.o
rm -fr main