我想用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);
}