数据结构之队列代码如下:
//=======================================================================================
//headd.h
#ifndef _hhlisst_
#define _hhlisst_
#define TRUE 1
#define FALSE 0
#define OK 1 //成功
#define ERROR 0 //失败(错误)
#define INFEASIBLE -1 //不可行的
#define OVERFLOW -2 //(内存)溢出
#define UNDERFLOW -4 //(数组下标)下溢
#define ENDELEM -9999 //序列结束元素(为方便演示而设)
#define LIST_INIT_SIZE 100 //顺序表初始容量(能容纳的元素个数)
#define LISTINCREMENT 10 //容量增量
#define MAXQSIZE 5 // 最大队列长度(对于循环队列,最大队列长度要减1)
typedef int QElemType; //元素类型ElemType为int
typedef int Status; //元素类型ElemType为int
struct SqQueue
{
QElemType *base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素 front和rear都是相对位移、从0开始取
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
};
#endif
//=======================================================================================
#include
#include
#include "headd.h"
Status InitQueue(SqQueue &Q)
{ // 构造一个空队列Q
Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q.base) // 存储分配失败
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
Status DestroyQueue(SqQueue &Q)
{ // 销毁队列Q,Q不再存在
if(Q.base)
free(Q.base);
Q.base=NULL;
Q.front=Q.rear=0;
return OK;
}
Status ClearQueue(SqQueue &Q)
{ // 将Q清为空队列
Q.front=Q.rear=0;
return OK;
}
Status QueueEmpty(SqQueue Q)
{ // 若队列Q为空队列,则返回TRUE,否则返回FALSE
if(Q.front==Q.rear) // 队列空的标志
return TRUE;
else
return FALSE;
}
int QueueLength(SqQueue Q)
{ // 返回Q的元素个数,即队列的长度
return(Q.rear-Q.front);
}
Status GetHead(SqQueue Q,QElemType &e)
{ // 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR
if(Q.front== Q.rear) // 队列空
return ERROR;
e=*(Q.base+Q.front);
return OK;
}
Status EnQueue(SqQueue &Q,QElemType e)
{ // 插入元素e为Q的新的队尾元素
if(Q.rear>= MAXQSIZE)
{ // 队列满,增加1个存储单元
Q.base=(QElemType *)realloc(Q.base,(Q.rear+1)*sizeof(QElemType));
if(!Q.base) // 增加单元失败
return ERROR;
}
*(Q.base+Q.rear)=e; //也可写成Q.base[Q.rear]=e; 两种写法一样
Q.rear++;
return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e)
{ // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR
if(Q.front== Q.rear) // 队列空
return ERROR;
e=Q.base[Q.front];
Q.front=Q.front+1;
return OK;
}
Status QueueTraverse(SqQueue Q,void(*vi)(QElemType))
{ // 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败
int i;
i=Q.front;
while(i!= Q.rear)
{
vi(*(Q.base+i));
i++;
}
printf("\n");
return OK;
}
void visit(QElemType i)
{
printf("%d ",i);
}
void main()
{
Status j;
int i,n;
QElemType d;
SqQueue Q;
InitQueue(Q);
printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
printf("队列长度为:%d\n",QueueLength(Q));
printf("请输入队列元素个数n: ");
scanf("%d",&n);
printf("请输入%d个整型队列元素:\n",n);
for(i=0;i < n;i++)
{
scanf("%d",&d);
EnQueue(Q,d);
}
printf("队列长度为:%d\n",QueueLength(Q));
printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
printf("现在队列中的元素为: \n");
QueueTraverse(Q,visit);
DeQueue(Q,d);
printf("删除队头元素%d\n",d);
printf("队列中的元素为: \n");
QueueTraverse(Q,visit);
j=GetHead(Q,d);
if(j)
printf("队头元素为: %d\n",d);
else
printf("无队头元素(空队列)\n");
ClearQueue(Q);
printf("清空队列后, 队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
j=GetHead(Q,d);
if(j)
printf("队头元素为: %d\n",d);
else
printf("无队头元素(空队列)\n");
DestroyQueue(Q);
}
//======================================================================================