队列的表示和基本操作的实现

来源:互联网 发布:linux查看gtk版本 编辑:程序博客网 时间:2024/06/05 02:39
//库函数头文件包含#include <stdio.h>#include <stdlib.h>#include <malloc.h>//函数状态码的定义#define TRUE        1#define FALSE       0#define OK          1#define ERROR       0#define OVERFLOW   -2typedef int Status;typedef int QElemType;//------单链队列————队列的链式存储结构-----------typedef struct QNode{    QElemType data;    struct QNode *next;}QNode, *QueuePtr;typedef struct{    QueuePtr Front;         //队头指针    QueuePtr Rear;          //队尾指针}LinkQueue;//--------基本操作的函数的实现------------------//构造一个空队列QStatus InitQueue(LinkQueue &Q){    Q.Front = Q.Rear = (QueuePtr)malloc(sizeof(QNode));    if(!Q.Front)        exit(OVERFLOW);    Q.Front->next = NULL;    return OK;}//销毁队列Q,Q不再存在Status DestroyQueue(LinkQueue &Q){    while(Q.Front){        Q.Rear = Q.Front->next;        free(Q.Front);        Q.Front = Q.Rear;    }    Q.Front = NULL;    Q.Rear = NULL;    return OK;}//将Q清为空队列Status ClearQueue(LinkQueue &Q){    QueuePtr p, q;    Q.Rear = Q.Front;    p = Q.Front->next;    Q.Front->next = NULL;    while(p){        q = p;        p = p->next;        free(q);    }    return OK;}//判断队列是否为空Status QueueEmpty(LinkQueue Q){    return Q.Front == Q.Rear;}//返回Q的元素个数,即为队列的长度int QueueLength(LinkQueue Q){    int len = 0;    if(Q.Front == Q.Rear)        return len;    QueuePtr p = Q.Front;    while(p != Q.Rear){        p = p->next;        ++len;    }    return len;}//返回队头元素Status GetHead(LinkQueue Q, QElemType &e){    QueuePtr p;    if(!Q.Front)        return ERROR;    p = Q.Front->next;    e = p->data;    return OK;}//在队尾插入元素(即入队)Status EnQueue(LinkQueue &Q, QElemType e){    QueuePtr p;    p = (QueuePtr)malloc(sizeof(QNode));    if(!p)        exit(OVERFLOW);     //储存分配失败    p->data = e;    p->next = NULL;         //(划重点)    Q.Rear->next = p;    Q.Rear = p;    return OK;}//删除队头元素(即出队)Status DeQueue(LinkQueue &Q, QElemType &e){    if(Q.Front == Q.Rear)        return ERROR;    QueuePtr p;    p = Q.Front->next;    e = p->data;    Q.Front->next = p->next;    if(Q.Rear == p)        Q.Rear = Q.Front;       //队列中只有一个元素,修改尾指针指向的位置    free(p);    return OK;}//visit()函数Status Print(QElemType e){    printf("%d ", e);    return OK;}//遍历队列Status QueueTraverse(LinkQueue Q, Status (*visit)(QElemType)){    QueuePtr p = Q.Front->next;    while(p){        if(!visit(p->data))            return ERROR;        p = p->next;    }    return OK;}//主函数int main(){    LinkQueue Q;    QElemType a;    InitQueue(Q);                   //初始化一个空队列    printf("输入5个数:\n");    for(int i = 0; i < 5; ++i){        scanf("%d", &a);        EnQueue(Q, a);    }    printf("遍历队列:\n");    QueueTraverse(Q, Print);    DeQueue(Q, a);    printf("\n执行出队操作的结果:\n");    QueueTraverse(Q, Print);    printf("\n获得队列长度操作的结果:\n%d", QueueLength(Q));    GetHead(Q, a);    printf("\n获得队头元素操作的结果:\n%d", a);    printf("\n判断队列是否为空:\n");    if(QueueEmpty(Q))        printf("YES");    else        printf("NO");    printf("\n执行清空队列操作后队列是否为空:\n");    ClearQueue(Q);    if(QueueEmpty(Q))        printf("YES");    else        printf("NO");    DestroyQueue(Q);    return 0;}