「已注销」 2019-09-04 20:31 采纳率: 0%
浏览 369

我想用C语言来写一个简单的链式队列,但是一直报错

我想用C语言来写一个简单的链式队列,但是一直报错,应该是指针没学好,在q->front=NULL就出错了,麻烦大佬们能提示一下吗?非常感谢!

#include<stdio.h>
#include<stdlib.h> 
struct node{
    int a;
    struct node* next;
};

typedef struct node* ptrq;

struct pn{
    struct node* front;
    struct node* rear;
};



typedef struct pn* Queue;

void makempty(struct pn** q);

void addq(struct pn** q);

void delqueue(struct pn** q);

int main()
{
    Queue q;
    q->front=NULL;
    q->rear=NULL;
    for(int i=0;i<2;i++)
    addq(&q);
    delqueue(&q);
 } 

void addq(struct pn** q)
{
    int num;
    ptrq mid;
    mid=(ptrq*)malloc(sizeof(struct node));
    printf("please enter the num you want to enter:\n");
    scanf("%d",&num);
    mid->a=num;
    mid->next=NULL;
    if(!(*q)->front)
    {
        (*q)->rear=mid;
        (*q)->front=mid;
    }
    else
    {
        (*q)->rear->next=mid;
        (*q)->rear=(*q)->rear->next;
    }
}

void makempty(struct pn** q)
{
    (*q)->front=NULL;
    (*q)->rear=NULL;
}

void delqueue(struct pn** q)
{
    int num,i=1;
    printf("enter the num you want to delete:\n");
    scanf("%d",&num);
    ptrq mid;
    mid=(*q)->front;
    while(mid->a!=num||!mid)
    {
        mid=mid->next;
        i++;
    }
    if(mid==NULL)
    printf("the num you enter is not found!\n");
    else
    printf("the num you enter is located in %d",i);
}
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2019-10-03 19:28
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿