zoukankan      html  css  js  c++  java
  • 链队列的基本接口实现

    基本接口实现代码

    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define TRUE   1
    #define FALSE  0
    #define OK     1
    #define ERROR  0
    #define IBFEASIBLE  -1
    #define OVERFLOW    -2 
    
    typedef int Status;
    typedef int ElemType; /* 元素类型为int类型*/
    
    // 链队列类型
    typedef struct LQNode{
        ElemType data;
          struct LQNode *next;
    }LQNode,*QueuePtr;    //结点及其指针类型
    typedef struct{
        QueuePtr front;        //队头指针
          QueuePtr rear;        //队尾指针
    }LQueue;    //链队列
    
    //接口
    
    //初始化队列 
    Status InitQueue_LQ(LQueue &Q);
    // 销毁队列
    Status DestroyQueue_LQ(LQueue &Q);
    //判断队列是否为空
    Status QueueEmpty_LQ(LQueue Q);
    //返回队列的长度
    int QueueLength_LQ(LQueue Q); 
    //返回队头元素
    Status GetHead_LQ(LQueue Q, ElemType &e);
    // 在队列Q的队尾插入元素
    Status EnQueue_LQ(LQueue &Q,ElemType e);
    //删除队头元素
    Status DeQueue_LQ(LQueue &Q,ElemType &e);
    //遍历打印队列
    void PrintQueue_LQ(LQueue Q); 
    //接口实现代码
    
    //1.初始化队列 
    Status InitQueue_LQ(LQueue &Q){
        Q.front=Q.rear=(LQNode *)malloc(sizeof(LQNode)); 
        if(Q.front==NULL) return OVERFLOW;
        Q.front->next= NULL;    //Q.front=Q.rear=NULL;
        return OK;
    }
    
    //2.销毁队列
    Status DestroyQueue_LQ(LQueue &Q){
        int e;
        while (!QueueEmpty_LQ(Q)){
            DeQueue_LQ(Q,e);
        }
        return OK;
    } 
    
    //3.判断是否为空
    Status QueueEmpty_LQ(LQueue Q){
        return Q.front==NULL&&Q.rear==NULL;  //实际上只须判断队头指针  是否为空即可
    }
    
    //4.求队列的长度
    int QueueLength_LQ(LQueue Q){
        int length=0;
        LQNode *pt;
        pt=Q.front->next;
         while(pt !=Q.rear->next){
             length++;
             pt=pt->next;
        }
        return length;
    }
    
    //5.返回队头元素
    Status GetHead_LQ(LQueue Q, ElemType &e){
         e=Q.front->next->data;  
    } 
    
    //6.入队
    Status EnQueue_LQ(LQueue &Q,ElemType e){
        LQNode *p;
        p=(LQNode *)malloc(sizeof(LQNode));
        if(NULL==p) return OVERFLOW;
        p->data=e;
        p->next = NULL;
        if(NULL==Q.front){
            Q.front=p;    //将e插入空队列
        }else{
            Q.rear->next=p;    //e插入非空队列
        } 
        Q.rear=p;         //队尾指针指向新的队尾    
    } 
    
    //7.出队
    Status DeQueue_LQ(LQueue &Q,ElemType &e){
        LQNode *p;
        if(NULL==Q.front) return ERROR; 
        p=Q.front;        //指向头结点 
        e=p->data;        //保存头结点数据 
        Q.front=p->next;
        if(Q.rear==p) Q.rear==NULL;    //队列只有一个结点,删去后队列变空 
        free(p);    //释放结点p 
        return OK; 
    } 
    
    //遍历打印队列
    void PrintQueue_LQ(LQueue Q){
        LQNode *pt;
        pt=Q.front->next;
        printf("链队列里元素为:"); 
         while(pt !=Q.rear->next){
             printf("%4d ",pt->data);
            pt=pt->next; //最后一次执行至此时将pt置为NULL,此时不用free(pt) 
        } 
        printf("
    ");
    } 
    
    //测试函数 
    int main(){
        int i,e,a,j; 
        LQueue Q;
        do{
            printf("1.初始化链队列
    ");
            printf("2.销毁链队列
    ");
            printf("3.判断链队列是否为空
    ");
            printf("4.链队列的长度
    ");
            printf("5.返回链队列的队头元素
    ");
            printf("6.在队尾插入元素
    ");
            printf("7.删除队头元素
    ");
            printf("8.遍历并打印队列元素
    "); 
            printf("请输入你要进行的操作:
    "); 
            scanf("%d",&i);
            switch(i){
                case 1:
                       if(InitQueue_LQ(Q)){
                           printf("初始化成功
    ");
                           printf("请输入5个元素:
    ");
                           for(j=0;j<5;j++){
                               scanf("%d",&a);
                               EnQueue_LQ(Q,a);
                           }
                       };
                       break;
                case 2:
                       DestroyQueue_LQ(Q);
                       printf("销毁队列成功
    ");
                       break; 
                case 3:
                       if(QueueEmpty_LQ(Q)){
                           printf("链队列为空!
    "); 
                       }else{
                           printf("链队列不为空!
    "); 
                       } 
                       break;
                case 4:
                       printf("链队列的长度为:%d
    ",QueueLength_LQ(Q));
                       break;
                case 5:
                       GetHead_LQ(Q,e);
                       printf("链队列队头元素为:%d
    ",e);
                       break;
                case 6:
                       printf("请输入你要插入的元素:");
                       scanf("%d",&e);
                       if(EnQueue_LQ(Q,e)){
                            printf("插入成功
    "); 
                       }
                       break;
                case 7: 
                       DeQueue_LQ(Q,e);
                       printf("删除成功
    "); 
                       break;
                case 8:
                       PrintQueue_LQ(Q);
                       break;
            } 
        }while(i>0&&i<10);
        return 0;
    }
  • 相关阅读:
    vue项目学习--2019/5/6
    JAVA注释--2019-04-28
    MyIbatis和Hibernate的区别--2019-04-26
    Thread类和Runnable接口实现多线程--2019-4-18
    Linux文件的扩展名--2019-04-25
    Linux文件名匹配和输出重定向--2019-4-24
    linux学习之命令的排列、替换和别名--2019-04-23
    linux学习--2019-04-22
    操作系统中线程和进程的概念--2019-4-17
    arrayList和vector的区别--2019-4-16
  • 原文地址:https://www.cnblogs.com/linwx/p/7900993.html
Copyright © 2011-2022 走看看