zoukankan      html  css  js  c++  java
  • 数据结构单链队列——链式存储实现

    数据结构作业之四,保留做模板

    #include<string.h>
    #include<ctype.h>
    #include<malloc.h> // malloc()等
    #include<limits.h> // INT_MAX等
    #include<stdio.h> // EOF(=^Z或F6),NULL
    #include<stdlib.h> // atoi()
    #include<io.h> // eof()
    #include<math.h> // floor(),ceil(),abs()
    #include<process.h> // exit()
    #include<iostream> // cout,cin
    // 函数结果状态代码
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    // #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行
    typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
    typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE
    
    typedef int QElemType;
    //单链队列--队列的链式存储结构
    typedef struct QNode
    {
        QElemType data;
        QNode *next;
    }*QueuePtr;
    
    struct LinkQueue
    {
        QueuePtr front,rear; // 队头、队尾指针
        int length;
    };
    
    //链队列的基本操作(9个)
    Status InitQueue(LinkQueue &Q)
    {
        /// 构造一个空队列Q
        if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
            exit(OVERFLOW);
        Q.front->next=NULL;
        Q.length=0;
        return OK;
    }
    
    Status DestroyQueue(LinkQueue &Q)
    {
        /// 销毁队列Q(无论空否均可)
        Q.length=0;
        while(Q.front)
        {
            Q.rear=Q.front->next;
            free(Q.front);
            Q.front=Q.rear;
        }
        return OK;
    }
    
    Status ClearQueue(LinkQueue &Q)
    {
        /// 将Q清为空队列
        Q.front->next=NULL;
        Q.rear=Q.front->next;
        Q.length=0;
        return OK;
    }
    
    Status QueueEmpty(LinkQueue Q)
    {
        /// 若Q为空队列,则返回TRUE,否则返回FALSE
        if(Q.front==Q.rear)return TRUE;
        else return FALSE;
    }
    
    int QueueLength(LinkQueue Q)
    {
        /// 求队列的长度
        return Q.length;
    }
    
    Status GetHead(LinkQueue Q,QElemType &e)
    {
        /// 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR
        QueuePtr p;
        if(Q.front==Q.rear)
            return ERROR;
        p=Q.front->next;
        e=p->data;
        return OK;
    }
    
    Status EnQueue(LinkQueue &Q,QElemType e)
    {
        /// 插入元素e为Q的新的队尾元素
        QueuePtr p;
        if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存储分配失败
            exit(OVERFLOW);
        p->data=e;
        p->next=NULL;
        Q.rear->next=p;
        Q.rear=p;
        Q.length++;
        return OK;
    }
    
    Status DeQueue(LinkQueue &Q,QElemType &e)
    {
        /// 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR
        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);
        Q.length--;
        return OK;
    }
    
    Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType))
    {
        /// 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败
        QueuePtr p;
        p=Q.front->next;
        while(p)
        {
            vi(p->data);
            p=p->next;
        }
        printf("
    ");
        return OK;
    }
    
    void visit(QElemType i)
    {
        printf("%d ",i);
    }
    
    int main()
    {
        int i;
        QElemType d;
        LinkQueue q;
        i=InitQueue(q);
        if(i)
            printf("成功地构造了一个空队列!
    ");
        printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
        printf("队列的长度为%d
    ",QueueLength(q));
        EnQueue(q,-5);
        EnQueue(q,5);
        EnQueue(q,10);
        printf("插入3个元素(-5,5,10)后,队列的长度为%d
    ",QueueLength(q));
        printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
        printf("队列的元素依次为:");
        QueueTraverse(q,visit);
        i=GetHead(q,d);
        if(i==OK)
            printf("队头元素是:%d
    ",d);
        DeQueue(q,d);
        printf("删除了队头元素%d
    ",d);
        i=GetHead(q,d);
        if(i==OK)
            printf("新的队头元素是:%d
    ",d);
        ClearQueue(q);
        printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u
    ",q.front,q.rear,q.front->next);
        DestroyQueue(q);
        printf("销毁队列后,q.front=%u q.rear=%u
    ",q.front, q.rear);
    }
    
  • 相关阅读:
    java中JVM的原理重温【转】
    JavaBean 规范
    Java编程规范[转]
    spring mvc 多数据源切换,不支持事务控制[一]
    03-连连看-连通分析
    02-连连看-用例分析
    01参考资料
    03-稀疏矩阵
    02-对不重复的一组数据查找
    01-用链式结构打印学生成绩单
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11794316.html
Copyright © 2011-2022 走看看