综述
队列是一种先进先出(FIFO)的线性表。它只允许在表的一端进行插入,而在另一端删除元素。队尾能插,对头能删。
队列的顺序存储结构,为了防止空间浪费,并不适宜像顺序表一样在插入元素时申请新地址,为了解决假溢出,循环队列也就应运而生,借助对数组下标的操作,以及修改算法,让顺序表在逻辑上完成了首尾相接。
循环队列
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define MAXQSIZE 100
typedef int Status;
typedef int QElemType;
typedef struct{
QElemType *base;
int front;
int rear;
}SqQueue;//定义结构
Status InitQueue(SqQueue *Q){
Q->base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
if(!Q->base) exit(OVERFLOW);
Q->front = Q->rear = 0;
return OK;
}//1初始化
Status DestoryQueue(SqQueue *Q){
free(Q->base);
Q->base = NULL;
Q->front = Q->rear = 0;
return OK;
}//2销毁队列
Status ClearQueue(SqQueue *Q){
Q->front = Q->rear = 0;
return OK;
}//3清空队列
Status QueueEmpty(SqQueue Q){
if(Q.front = Q.rear) return 1;
else return 0;
}//4判空
Status QueueLength(SqQueue Q){
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}//5求长度
Status GetHead(SqQueue Q,QElemType *e){
if(Q.front == Q.rear) return ERROR;
*e = Q.base[Q.front];
return OK;
}//6读取头元素
Status EnQueue(SqQueue *Q,QElemType e){
if((Q->rear+1)%MAXQSIZE==Q->front) return ERROR;
Q->base[Q->rear] = e;
Q->rear = (Q->rear+1) % MAXQSIZE;
return OK;
}//7入队
Status DeQueue(SqQueue *Q,QElemType *e){
if(Q->front == Q->rear) return ERROR;
*e = Q->base[Q->front];
Q->front = (Q->front+1) % MAXQSIZE;
return OK;
}//8出队
int main(void){
SqQueue Q;
QElemType e;
printf("%p\n",Q.base);
InitQueue(&Q);//test1
printf("%p\n",Q.base);
printf("\n");
EnQueue(&Q,111);//test7
EnQueue(&Q,222);//test7
EnQueue(&Q,333);//test7
EnQueue(&Q,444);//test7
GetHead(Q,&e);//test6
printf("%d\n",e);
printf("\n");
if(QueueEmpty(Q)) printf("此队列为空\n");
else printf("此队列非空\n");//test4
printf("此队列现有%d个元素\n",QueueLength(Q));//test5
printf("\n");
DeQueue(&Q,&e);//test8
printf("%d\n",e);
printf("此队列现有%d个元素\n",QueueLength(Q));//test5
printf("\n");
ClearQueue(&Q);//test3
if(QueueEmpty(Q)) printf("此队列为空\n");
else printf("此队列非空\n");//test4
printf("此队列现有%d个元素\n",QueueLength(Q));//test5
printf("\n");
printf("%p\n",Q.base);
DestoryQueue(&Q);//test2
printf("%p\n",Q.base);
return 0;
}
链队列
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode;
typedef struct QNode *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue *Q){
if(!Q) return ERROR;
Q->rear = (QNode*)malloc(sizeof(QNode));
Q->front = Q->rear;
if(!Q->front) exit(OVERFLOW);
Q->front->next = NULL;
return OK;
}//1初始化
Status DestoryQueue(LinkQueue *Q){
while(Q->front){
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return OK;
}//2销毁队列
Status ClearQueue(LinkQueue *Q){
QueuePtr p;
if(Q->front == Q->rear) return ERROR;
while(Q->front->next){
p = Q->front->next;
Q->front->next = p->next;
free(p);
}
Q->rear = Q->front;
return OK;
}//3清空队列
Status QueueEmpty(LinkQueue Q){
if(Q.front==Q.rear) return 1;
else return 0;
}//4判断是否为空
Status QueueLength(LinkQueue Q){
int i = 0 ;
while(Q.front->next){
i++;
Q.front = Q.front->next;
}
return i;
}//5求队列长度
Status GetHead(LinkQueue Q,QElemType* e){
if(Q.front==Q.rear) return ERROR;
*e = Q.front->next->data;
return OK;
}//6读取第一个
Status EnQueue(LinkQueue* Q,QElemType e){
QueuePtr p;
p = (QNode*)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
//printf("%d",Q->front->next->data);
return OK;
}//7入队
Status DeQueue(LinkQueue* Q,QElemType* e){
QueuePtr p;
if(Q->front == Q->rear) return ERROR;
p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(Q->rear == p) Q->rear = Q->front;
free(p);
return OK;
}//8出队
int main(void){
LinkQueue Q;
QElemType e;
printf("%p\n",Q.front);
InitQueue(&Q);//test1
printf("%p\n",Q.front);
printf("\n");
EnQueue(&Q,111);//test7
EnQueue(&Q,222);//test7
EnQueue(&Q,333);//test7
EnQueue(&Q,444);//test7
GetHead(Q,&e);//test6
printf("%d\n",e);
printf("\n");
if(QueueEmpty(Q)) printf("此队列为空\n");
else printf("此队列非空\n");//test4
printf("此队列现有%d个元素\n",QueueLength(Q));//test5
printf("\n");
DeQueue(&Q,&e);//test8
printf("%d\n",e);
printf("此队列现有%d个元素\n",QueueLength(Q));//test5
printf("\n");
ClearQueue(&Q);//test3
if(QueueEmpty(Q)) printf("此队列为空\n");
else printf("此队列非空\n");//test4
printf("此队列现有%d个元素\n",QueueLength(Q));//test5
printf("\n");
printf("%p\n",Q.front);
DestoryQueue(&Q);//test2
printf("%p\n",Q.front);
return 0;
}