您的位置:首页 > 理论基础 > 数据结构算法

数据结构第七周项目2--建立链队算法库

2017-12-14 10:21 190 查看
/* 

*Copyright(c)2017,烟台大学计算机与控制工程学院 

*All rights reserved. 

*作    者:刘浩

*版 本 号:v1.0 

*问题描述:定义链队存储结构,实现其基本运算,并完成测试。 

*/  

头文件:liqueue.h,包含定义链队数据结构的代码、宏定义、要实现算法的函数的声明:

[cpp] view
plain copy

#ifndef LIQUEUE_H_INCLUDED  

#define LIQUEUE_H_INCLUDED  

  

typedef char ElemType;  

typedef struct qnode  

{  

    ElemType data;  

    struct qnode *next;  

} QNode;        //链队数据结点类型定义  

  

typedef struct  

{  

    QNode *front;  

    QNode *rear;  

} LiQueue;          //链队类型定义  

void InitQueue(LiQueue *&q);  //初始化链队  

void DestroyQueue(LiQueue *&q);  //销毁链队  

bool QueueEmpty(LiQueue *q);  //判断链队是否为空  

int QueueLength(LiQueue *q);  //返回队列中数据元素个数  

void enQueue(LiQueue *&q,ElemType e);  //入队  

bool deQueue(LiQueue *&q,ElemType &e);   //出队  

  

#endif // LIQUEUE_H_INCLUDED  

源文件:liqueue.cpp,包含实现各种算法的函数的定义:

[cpp] view
plain copy

#include <stdio.h>  

#include <malloc.h>  

#include "liqueue.h"  

  

void InitQueue(LiQueue *&q)  //初始化链队  

{  

    q=(LiQueue *)malloc(sizeof(LiQueue));  

    q->front=q->rear=NULL;  

}  

void DestroyQueue(LiQueue *&q)  //销毁链队  

{  

    QNode *p=q->front,*r;   //p指向队头数据节点  

    if (p!=NULL)            //释放数据节点占用空间  

    {  

        r=p->next;  

        while (r!=NULL)  

        {  

            free(p);  

            p=r;  

            r=p->next;  

        }  

    }  

    free(p);  

    free(q);                //释放链队节点占用空间  

}  

bool QueueEmpty(LiQueue *q)  //判断链队是否为空  

{  

    return(q->rear==NULL);  

}  

int QueueLength(LiQueue *q)  //返回队列中数据元素个数  

{  

    int n=0;  

    QNode *p=q->front;  

    while (p!=NULL)  

    {  

        n++;  

        p=p->next;  

    }  

    return(n);  

}  

void enQueue(LiQueue *&q,ElemType e)  //入队  

{  

    QNode *p;  

    p=(QNode *)malloc(sizeof(QNode));  

    p->data=e;  

    p->next=NULL;  

    if (q->rear==NULL)      //若链队为空,则新节点是队首节点又是队尾节点  

        q->front=q->rear=p;  

    else  

    {  

        q->rear->next=p;    //将*p节点链到队尾,并将rear指向它  

        q->rear=p;  

    }  

}  

bool deQueue(LiQueue *&q,ElemType &e)   //出队  

{  

    QNode *t;  

    if (q->rear==NULL)      //队列为空  

        return false;  

    t=q->front;             //t指向第一个数据节点  

    if (q->front==q->rear)  //队列中只有一个节点时  

        q->front=q->rear=NULL;  

    else                    //队列中有多个节点时  

        q->front=q->front->next;  

    e=t->data;  

    free(t);  

    return true;  

}  

在同一项目(project)中建立一个源文件(如main.cpp),编制main函数,完成相关的测试工作。 例:

[cpp] view
plain copy

<span style="font-family:SimSun;">#include <stdio.h>  

#include "liqueue.h"  

  

int main()  

{  

    ElemType e;  

    LiQueue *q;  

    printf("(1)初始化链队q\n");  

    InitQueue(q);  

    printf("(2)依次进链队元素a,b,c\n");  

    enQueue(q,'a');  

    enQueue(q,'b');  

    enQueue(q,'c');  

    printf("(3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));  

    if (deQueue(q,e)==0)  

        printf("队空,不能出队\n");  

    else  

        printf("(4)出队一个元素%c\n",e);  

    printf("(5)链队q的元素个数:%d\n",QueueLength(q));  

    printf("(6)依次进链队元素d,e,f\n");  

    enQueue(q,'d');  

    enQueue(q,'e');  

    enQueue(q,'f');  

    printf("(7)链队q的元素个数:%d\n",QueueLength(q));  

    printf("(8)出链队序列:");  

    while (!QueueEmpty(q))  

    {  

        deQueue(q,e);  

        printf("%c ",e);  

    }  

    printf("\n");  

    printf("(9)释放链队\n");  

    DestroyQueue(q);  

    return 0;  

}</span><span style="font-family:microsoft yahei;">  

</span>  

结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: