zoukankan      html  css  js  c++  java
  • 数据结构--循环队列

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #define Max 5
    typedef struct SqQueue{
        int *base; //动态开辟内存  
        int front; //头指针 
        int rear;  //尾指针 
    }SqQueue;
    
    int InitQueue(SqQueue *q){
        q->base=(int *)malloc(Max*sizeof(int));  
        if(!q->base) return 0;  //存储分配失败 
        q->front=q->rear=0;
        return 1;
    }
    
    int QueueLength(SqQueue q){
        return (q.rear-q.front+Max)%Max; //求队列长度 
    }
    
    int EnQueue(SqQueue *q,int x){ //从队尾入队 
        if((q->rear+1)%Max==q->front) return 0;  //队满 
        q->base[q->rear]=x;
        q->rear=(q->rear+1)%Max;
        return 1;
    }
    
    int DeQueue(SqQueue *q,int *x){  //从队首出队 
        if(q->front==q->rear) return 0;
        *x=q->base[q->front];
        q->front=(q->front+1)%Max;
        return 1;
    }
    int main()
    {
        SqQueue q;
        char ss[10]; 
        int x, sta,l; 
        InitQueue(&q); 
        while(scanf("%s", ss)!=EOF) 
        {
            if(strcmp(ss, "enter")==0)
            {
                scanf("%d", &x);
                sta= EnQueue(&q,x);
                if(sta==0)
                    printf("FULL
    "); 
            }
            else if(strcmp(ss, "length")==0)
            {
                l=QueueLength(q); 
                printf("%d
    ", l);
            }
            else
            {
                sta = DeQueue(&q,&x);
                if(sta==0)
                    printf("EMPTY
    "); 
                else
                    printf("%d
    ", x);
            } 
        }
        return 0;
    }
    View Code

    描述

     

    创建一个循环队列,队列元素个数为4。能够实现队列的初始化、入队列、出队列、求队列长度等操作。

    循环队列数据类型定义如下:

    typedef struct
    {

        int data[Max];
        int front;
        int rear;
    }SqQueue;

    部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

    int main()
    {
        SqQueue q;
        char ss[10]; 
        int x, sta,l; 
        InitQueue(&q); 
        while(scanf("%s", ss)!=EOF) 
        {
            if(strcmp(ss, "enter")==0)
            {
                scanf("%d", &x);
                sta= EnQueue(&q,x);
                if(sta==0)
                    printf("FULL
    "); 
            }
            else if(strcmp(ss, "length")==0)
            {
                l=QueueLength(q); 
                printf("%d
    ", l);
            }
            else
            {
                sta = DeQueue(&q,&x);
                if(sta==0)
                    printf("EMPTY
    "); 
                else
                    printf("%d
    ", x);
            } 
        }
        return 0;
    }

    输入

     

    输入数据由以下几种命令组成:

    (1)enter x:x入队列

    (2)del:出队列

    (3)length:求队列长度

    每个命令占一行,以EOF结束。

    输出

     

    当执行enter操作时元素入队,若队满输出FULL。

    当执行del时输出出队的元素,若队为空,输出EMPTY。

    当。执行length时,输出队列长度。

    样例输入

     

     enter 3
    enter 15
    length
    del
    del
    del

     

    样例输出

    2
    3
    15
    EMPTY

    代码测试:

     

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #define Max 5
    typedef struct SqQueue{
        int *base; //动态开辟内存  
        int front; //头指针 
        int rear;  //尾指针 
    }SqQueue;
    
    int InitQueue(SqQueue *q){
        q->base=(int *)malloc(Max*sizeof(int));  
        if(!q->base) return 0;  //存储分配失败 
        q->front=q->rear=0;
        return 1;
    }
    
    int QueueLength(SqQueue q){
        return (q.rear-q.front+Max)%Max; //求队列长度 
    }
    
    int EnQueue(SqQueue *q,int x){ //从队尾入队 
        if((q->rear+1)%Max==q->front) return 0;  //队满 
        q->base[q->rear]=x;
        q->rear=(q->rear+1)%Max;
        return 1;
    }
    
    int DeQueue(SqQueue *q,int *x){  //从队首出队 
        if(q->front==q->rear) return 0;
        *x=q->base[q->front];
        q->front=(q->front+1)%Max;
        return 1;
    }
    int main()
    {
        SqQueue q;
        char ss[10]; 
        int x, sta,l; 
        InitQueue(&q); 
        while(scanf("%s", ss)!=EOF) 
        {
            if(strcmp(ss, "enter")==0)
            {
                scanf("%d", &x);
                sta= EnQueue(&q,x);
                if(sta==0)
                    printf("FULL
    "); 
            }
            else if(strcmp(ss, "length")==0)
            {
                l=QueueLength(q); 
                printf("%d
    ", l);
            }
            else
            {
                sta = DeQueue(&q,&x);
                if(sta==0)
                    printf("EMPTY
    "); 
                else
                    printf("%d
    ", x);
            } 
        }
        return 0;
    }
    View Code
  • 相关阅读:
    一笔期货成交的始末(可能有问题)
    tcp心跳模型
    spring boot 2.0 启动监控端点的方法(spring-boot-starter-actuator)
    netty channel的线程安全性与@Sharable
    为什么使用https
    angularjs post 跨域 Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.
    http的keep-alive和tcp的keepalive区别
    最大公约数 最小公倍数--------专题
    hdu 2024 C语言合法标识符
    hdu 2025 查找最大元素
  • 原文地址:https://www.cnblogs.com/momo-88/p/8932455.html
Copyright © 2011-2022 走看看