想创建一个链式结构的队列,输入队列中每个人的程序并输出,但是为什么运行不了,调试的时候又报错?希望得到解答。
Thread 1 received signal SIGSEGV,Segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 40
typedef struct queue
{
char elemdata[N];
struct queue *next;
}Queueprt;
typedef struct link
{
Queueprt *front;
Queueprt *rear;
}Linkqueue;
void Createqueue(Linkqueue *Q);
void Insertqueue(Linkqueue *Q, char *data);
void Dequeue(Linkqueue *Q, char *str);
int main(void)
{
char name[N][N];
char str[N];
int n;
Linkqueue *people = NULL;
Createqueue(people);
printf("How many people?\n");
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s", name[i]);
Insertqueue(people, name[i]);
}
for(int i = 0; i < n; i++)
{
Dequeue(people, str);
printf("%s\n", str);
}
return 0;
}
void Createqueue(Linkqueue *Q)
{
Q->front = NULL;
Q->rear = NULL;
Q->front = Q->rear = (Queueprt*)malloc(sizeof(Queueprt));
if(Q->front == NULL)
{
printf("No enough memory!\n");
exit(0);
}
Q->front->next = NULL;
}
void Insertqueue(Linkqueue *Q, char *data)
{
Queueprt *p;
p = (Queueprt *) malloc(sizeof(Queueprt));
if(p == NULL)
{
printf("No enough memory to insert!\n");
exit(0);
}
strcpy(p->elemdata, data);
Q->rear->next = p;
Q->rear = p;
p->next = NULL;
}
void Dequeue(Linkqueue *Q, char *str)
{
Queueprt *p;
if(Q->front == Q->rear)
{
return;
}
p = Q->front->next;
strcpy(str, p->elemdata);
Q->front->next = p->next;
if(Q->rear == p)
{
Q->rear = Q->front;
}
free(p);
}