/*链队列存储结构(LinkQueue.h)*/
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
/**************************************************/
/* 链队列的基本操作(LinkQueue.c)*/
Status InitQueue(LinkQueue *Q)
{
if ((Q->front = (QueuePtr) malloc(sizeof(QNode))) == NULL)
exit(OVERFLOW);
Q->rear = Q->front;
Q->front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue *Q)
{
QueuePtr p = Q->front;
while (p != Q->rear)
{
Q->front = p->next;
free(p);
p = Q->front;
}
free(Q->rear);
Q->front = Q->rear = NULL;
return OK;
}
Status DestroyQueue1(LinkQueue *Q)
{
while (Q->front != NULL)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return OK;
}
Status ClearQueue(LinkQueue *Q)
{
QueuePtr p = Q->front->next;
while(p != NULL)
{
Q->rear = p->next;
free(p);
p = Q->rear;
}
Q->rear = Q->front;
Q->front->next = NULL;
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
return Q.front == Q.rear;
}
int QueueLength(LinkQueue Q)
{
int len = 0;
while (Q.front != Q.rear)
{
len++;
Q.front = Q.front->next;
}
return len;
}
Status GetHead_Q(LinkQueue Q,QElemType *e)
{
if (!QueueEmpty(Q))
{
*e = Q.front->next->data;
return OK;
}
else
return FALSE;
}
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr p;
if ((p = (QueuePtr) malloc(sizeof(QNode))) == NULL)
exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
if (!QueueEmpty(*Q))
{
p = Q->front->next;
if (p == Q->rear)
Q->rear = Q->front;
Q->front->next = p->next;
*e = p->data;
free(p);
return OK;
}
else
return FALSE;
}
Status QueueTraverse(LinkQueue Q,Status(*Visit)(QElemType))
{
QueuePtr p = Q.front->next;
while (p != NULL)
{
if (!Visit(p->data))
return ERROR;
p = p->next;
}
printf("\n");
return OK;
}
/***************************************************/
/*LinkQueue_Main.c 检验LinkQueue.c的主程序 */
#include"DS.h"
typedef int QElemType;
#include"LinkQueue.h"
#include"LinkQueue.c"
Status visit(QElemType i)
{
printf("%d ",i);
return OK;
}
void main()
{
int i;
QElemType d;
LinkQueue q;
i=InitQueue(&q);
if(i)
printf("成功地构造了一个空队列!\n");
printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
printf("队列的长度为%d\n",QueueLength(q));
EnQueue(&q,-5);
EnQueue(&q,5);
EnQueue(&q,10);
printf("插入3个元素(-5,5,10)后,队列的长度为%d\n",QueueLength(q));
printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
printf("队列的元素依次为:");
QueueTraverse(q,visit);
i=GetHead_Q(q,&d);
if(i==OK)
printf("队头元素是:%d\n",d);
DeQueue(&q,&d);
printf("删除了队头元素%d\n",d);
i=GetHead_Q(q,&d);
if(i==OK)
printf("新的队头元素是:%d\n",d);
ClearQueue(&q);
printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next);
DestroyQueue(&q);
printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
}
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
/**************************************************/
/* 链队列的基本操作(LinkQueue.c)*/
Status InitQueue(LinkQueue *Q)
{
if ((Q->front = (QueuePtr) malloc(sizeof(QNode))) == NULL)
exit(OVERFLOW);
Q->rear = Q->front;
Q->front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue *Q)
{
QueuePtr p = Q->front;
while (p != Q->rear)
{
Q->front = p->next;
free(p);
p = Q->front;
}
free(Q->rear);
Q->front = Q->rear = NULL;
return OK;
}
Status DestroyQueue1(LinkQueue *Q)
{
while (Q->front != NULL)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return OK;
}
Status ClearQueue(LinkQueue *Q)
{
QueuePtr p = Q->front->next;
while(p != NULL)
{
Q->rear = p->next;
free(p);
p = Q->rear;
}
Q->rear = Q->front;
Q->front->next = NULL;
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
return Q.front == Q.rear;
}
int QueueLength(LinkQueue Q)
{
int len = 0;
while (Q.front != Q.rear)
{
len++;
Q.front = Q.front->next;
}
return len;
}
Status GetHead_Q(LinkQueue Q,QElemType *e)
{
if (!QueueEmpty(Q))
{
*e = Q.front->next->data;
return OK;
}
else
return FALSE;
}
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr p;
if ((p = (QueuePtr) malloc(sizeof(QNode))) == NULL)
exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
if (!QueueEmpty(*Q))
{
p = Q->front->next;
if (p == Q->rear)
Q->rear = Q->front;
Q->front->next = p->next;
*e = p->data;
free(p);
return OK;
}
else
return FALSE;
}
Status QueueTraverse(LinkQueue Q,Status(*Visit)(QElemType))
{
QueuePtr p = Q.front->next;
while (p != NULL)
{
if (!Visit(p->data))
return ERROR;
p = p->next;
}
printf("\n");
return OK;
}
/***************************************************/
/*LinkQueue_Main.c 检验LinkQueue.c的主程序 */
#include"DS.h"
typedef int QElemType;
#include"LinkQueue.h"
#include"LinkQueue.c"
Status visit(QElemType i)
{
printf("%d ",i);
return OK;
}
void main()
{
int i;
QElemType d;
LinkQueue q;
i=InitQueue(&q);
if(i)
printf("成功地构造了一个空队列!\n");
printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
printf("队列的长度为%d\n",QueueLength(q));
EnQueue(&q,-5);
EnQueue(&q,5);
EnQueue(&q,10);
printf("插入3个元素(-5,5,10)后,队列的长度为%d\n",QueueLength(q));
printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
printf("队列的元素依次为:");
QueueTraverse(q,visit);
i=GetHead_Q(q,&d);
if(i==OK)
printf("队头元素是:%d\n",d);
DeQueue(&q,&d);
printf("删除了队头元素%d\n",d);
i=GetHead_Q(q,&d);
if(i==OK)
printf("新的队头元素是:%d\n",d);
ClearQueue(&q);
printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next);
DestroyQueue(&q);
printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
}