calmcccc 2022-10-06 12:38 采纳率: 44.4%
浏览 161
已结题

用很基础的c语言实现顺序队列,循环队列,链式队列的基本操作

顺序队列基本操作:初始化、销毁、判断是否为空、进队出队
循环队列基本操作:已知队尾指针和元素个数可以计算出队头指针,设计这种环形队列的初始化、进队、出队判断是否为空
链队:类型定义、初始化、判断是否为空、销毁
注释要尽量详细,让初学者看得懂

  • 写回答

3条回答 默认 最新

  • ·星辰大海 2022-10-06 14:34
    关注
    
    #include<stdio.h>
    #define MaxSize 5
    
    typedef int ElemType;
    struct Queue
    {
        ElemType data[MaxSize];
        int rear,front;  //队尾,队头下标
    };
    
    //判断队列是否为空
    int isEmpty(struct Queue *Q){
        return Q->rear==Q->front;  
    }
    
    
    //初始化队列
    int InitQueue(struct Queue *Q){
        Q->rear=0;
        Q->front=0;
    }
    
    
    //入队
    int EnQueue(struct Queue *Q,ElemType x){
        if (Q->rear>=MaxSize)
        {
            return 0;
        }
        Q->data[Q->rear++]=x;
        return 1;
    }
    
    
    //出队
    int DeQueue(struct Queue *Q,ElemType *x){
        if (isEmpty(Q))
        {
            return 0;
        }
        *x=Q->data[Q->front++];
        return 1;
    }
    
    //销毁队列
    int DestroyQueue(struct Queue *Q){
        ElemType x;
        while (!isEmpty(Q))
        {
            DeQueue(Q,&x);
        }
        return 1;
    }
    
    
    #include<stdio.h>
    #define MaxSize 5
    
    typedef int ElemType;
    struct CQueue
    {
        ElemType data[MaxSize];
        int rear,num;  //num是队列元素个数
    };
     
    int InitCQueue(struct CQueue *Q){
        Q->rear=0;
        Q->num=0;
        return 1;
    }
     
    int isEmpty(struct CQueue *Q){
        return Q->num==0;
    }
     
    int EnQueue(struct CQueue *Q,ElemType x){
        if (Q->num>=MaxSize)
        {
            return 0;
        }
        Q->data[Q->rear]=x;
        Q->rear=(Q->rear+1)%MaxSize;
        Q->num++;
        return 1;
    }
     
    int DeQueue(struct CQueue *Q,ElemType *x){
        if (isEmpty(Q))
        {
            return 0;
        }
        int front=(Q->rear-Q->num+MaxSize)%MaxSize;
        *x=Q->data[front];
        Q->num--;
        return 1;
    }
     
    int DestroyCQueue(struct CQueue *Q){
        ElemType x;
        while (!isEmpty(Q))
        {
            DeQueue(Q,&x);
        }
        return 1;
    }
    
    
    #include<stdio.h>
    #include<malloc.h>
    
    typedef int ElemType;
    //链式队列
    //链队的数据存储方式
    struct LinkNode
    {
        ElemType data;
        struct LinkNode *next;
    };
     
    //链队的头尾指针
    struct LinkQueue
    {
        struct LinkNode *front,*rear;
    };
     
    int InitQueue(struct LinkQueue *Q){
        Q->front=Q->rear=(struct LinkNode *)malloc(sizeof(struct LinkNode)); //建立头节点
        Q->front->next=NULL;
        return 1;
    }
     
    int isEmpty(struct LinkQueue *Q){
        return Q->front==Q->rear;
    }
     
    //入队
    int EnQueue(struct LinkQueue *Q,ElemType x){
        //链队一般不会存满,无需判满
        struct LinkNode *s=(struct LinkNode *)malloc(sizeof(struct LinkNode));
        s->data=x;
        s->next=NULL;
        Q->rear->next=s;
        Q->rear=s;
        return 1;
    }
     
     
    //出队
    int DeQueue(struct LinkQueue *Q,ElemType *x){
        if(isEmpty(Q)){
            return 0;
        }
        struct LinkNode *p=Q->front->next;
        *x=p->data;
        Q->front->next=p->next;
        if (Q->rear==p)
        {
            Q->rear=Q->front; //如果队列里只剩下一个要删除的节点,那么此节点删除后,头尾指针要指向同一个地方
        }
        free(p);
        return 1;
        
    }
     
    //销毁队列
    int DestroyQueue(struct LinkQueue *Q){
        ElemType x;
        while (!isEmpty(Q))
        {
            DeQueue(Q,&x);
        }
        return 1;
        
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 创建了问题 10月6日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分