• 循环队列的基本接口实现


    基本接口实现代码,欢迎补充

    #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{
        ElemType *elem;      //存储空间的基址
        int front;            //对头位标
        int rear;             //队尾位标
        int maxSize;         //存储容量 
    }SqQueue; 
    
    
    //初始化空队列 
    Status InitQueue_Sq(SqQueue &Q, int size);
    //销毁队列
    Status DestroyQueue_Sq(SqQueue &Q);
    //将Q置为空队列
    void  ClearQueue_Sq(SqQueue &Q);
    //判断是否为空队列
    Status QueueEmpty_Sq(SqQueue &Q);
    //返回队列元素个数
    int QueueLength_Sq(SqQueue &Q);
    //若队列不空,则用e返回Q的队列头
    Status GetHead_Sq(SqQueue Q,ElemType &e);
    //在队尾插入元素
    Status EnQueue_Sq(SqQueue &Q,ElemType e);
    //删除队头元素
    Status DeQueue_Sq(SqQueue &Q,ElemType &e); 
    //遍历打印队列
    void TraverseQueue_Sq(SqQueue &Q); 
    
    //循环队列基本接口实现
    
    //1.初始化空队列 
    Status InitQueue_Sq(SqQueue &Q, int size){
        Q.elem = (ElemType*)malloc(size*sizeof(ElemType));
        if(NULL==Q.elem) return OVERFLOW;
        Q.maxSize=size;
        Q.front=Q.rear=0;
        return OK;
    } 
    
    //2.销毁队列
    Status DestroyQueue_Sq(SqQueue &Q){
        free(Q.elem); 
           Q.elem=NULL; 
          Q.front=Q.rear=0;
          return OK; 
    } 
    
    //3. 置为空队列
    void  ClearQueue_Sq(SqQueue &Q){
        Q.front=Q.rear=0; 
    }
    
    //4. 判断是否为空对列
    Status QueueEmpty_Sq(SqQueue &Q){
         if(Q.front==Q.rear){
             return TRUE;
         }else{
             return FALSE;
         }
    }
    
    //5.求队列元素个数
    int QueueLength_Sq(SqQueue &Q){
        if(Q.rear-Q.front<0) {
            return Q.maxSize-Q.front+Q.rear;
        }else{
            return Q.rear-Q.front;
        } 
    } 
    
    //6.返回队头元素,并返回OK
    Status GetHead_Sq(SqQueue Q,ElemType &e){
        if(Q.maxSize<=0) return ERROR; 
        e=Q.elem[Q.front];
        return OK;
    } 
    
    //7.在队尾位置插入元素
    Status EnQueue_Sq(SqQueue &Q,ElemType e){
        if((Q.rear+1)%Q.maxSize==Q.front) return ERROR;
        Q.elem[Q.rear]=e;
        Q.rear=(Q.rear+1)%Q.maxSize;
        return OK; 
    } 
    
    //8.出队
    Status DeQueue_Sq(SqQueue &Q,ElemType &e){
        if(Q.front==Q.rear) return ERROR;
        e=Q.elem[Q.front];
        Q.front=(Q.front+1)%Q.maxSize;
        return OK; 
    }
    //9.遍历并打印元素
    void TraverseQueue_Sq(SqQueue &Q)  
    {  
        int i=Q.front;  
        printf("队中的元素是:");  
        while(i%Q.maxSize!=Q.rear)  
        {  
            printf("%4d ",Q.elem[i]);  
            i++;  
        }  
        printf("
    ");  
    } 
    //测试函数 
    int main(){
        int i,e,n=10,a,j; 
        SqQueue Q;
        do{
            printf("1.初始化循环队列
    ");
            printf("2.销毁循环队列
    ");
            printf("3.将循环队列置为空队列
    ");
            printf("4.判断循环队列是否为空
    ");
            printf("5.循环队列的长度
    ");
            printf("6.返回循环队列的队头元素
    ");
            printf("7.在队尾插入元素
    ");
            printf("8.删除队头元素
    ");
            printf("9.遍历并打印队列元素
    "); 
            printf("请输入你要进行的操作:
    "); 
            scanf("%d",&i);
            switch(i){
                case 1:
                       if(InitQueue_Sq(Q,n)){
                           printf("初始化成功
    ");
                           printf("请输入5个元素:
    ");
                           for(j=0;j<5;j++){
                               scanf("%d",&a);
                               EnQueue_Sq(Q,a);
                           }
                       };
                       break;
                case 2:
                       DestroyQueue_Sq(Q);
                       printf("销毁队列成功
    ");
                       break; 
                case 3:
                       ClearQueue_Sq(Q);
                       printf("置空成功
    "); 
                       break;
                case 4:
                       if(QueueEmpty_Sq(Q)){
                           printf("循环队列为空!
    "); 
                       }else{
                           printf("循环队列不为空!
    "); 
                       } 
                       break;
                case 5:
                       printf("循环队列的长度为:%d
    ",QueueLength_Sq(Q));
                       break;
                case 6:
                       GetHead_Sq(Q,e);
                       printf("循环队列队头元素为:%d
    ",e);
                       break;
                case 7:
                       printf("请输入你要插入的元素:");
                       scanf("%d",&e);
                       if(EnQueue_Sq(Q,e)){
                            printf("插入成功
    "); 
                       }
                       break;
                case 8: 
                       DeQueue_Sq(Q,e);
                       printf("删除成功
    "); 
                       break;
                case 9:
                       TraverseQueue_Sq(Q);
                       break;
            } 
        }while(i>0&&i<10);
        return 0;
    } 
     觉得有用将点赞哦!
     
  • 相关阅读:
    Asp.net Treeview 客户端选中效果实现 (初级)
    MYSQL生成日历表,通常在做报表的时候需要用来生成一个临时表,用来左连接等。
    写了一个抽奖类,感觉还不错,可以适合各种变化
    将系统的内部类:HttpValueCollection 移到自己的系统中,使其能方便的解析id=1&name=张三&sex=男这样的字符串参数 querystring
    指定某个文件的创建 修改 访问时间
    Reqeust["keyname"] 的读取顺序
    pku1463 Strategic game
    pku1947 Rebuilding Roads
    pku1848 Tree
    pku1056 IMMEDIATE DECODABILITY
  • 原文地址:https://www.cnblogs.com/linwx/p/7900690.html
Copyright © 2020-2023  润新知