我的程序崩溃了

use*_*561 0 c crash queue

我的程序崩溃,我不知道为什么.我试图创建一个10个元素的队列.我的主要代码:

#include "queue.h"

int main(void){

Queue * queue;

queue = create_q(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的queue.h代码:

#ifndef QUEUE_H_
#define QUEUE_H_
#endif /* QUEUE_H_ */

#include <stdbool.h> /* for bool type */
#include <stdio.h> /* for standard IO support */
#include <stdlib.h> /* for malloc() and free() functions */

typedef struct patient { /* my structure */
    char name [20];
    char surname [20];
    int priority;
    struct patient * next; /* pointer to next node */
}Node;

typedef struct queue {
    Node * head;
    Node * last;
}Queue;

Queue * create_q(int size);
Run Code Online (Sandbox Code Playgroud)

我的queue.c代码:

#include "queue.h"

Queue * create_q(int size){

Node * temp, * temp2;
Queue * new ;
int i=0,j;

temp = (Node *) malloc(sizeof(Node));
new->head = temp;
if(temp == NULL){
    printf("There is not enough memory to create the %dth Node of your queue",i);
    for (j=1; j<=i; j++){
        new->head = temp->next;
        free(temp);
    }
    return NULL;
}
if (temp != NULL){
    new->head = temp;
    for (i=1; i<size; i++){
        temp2 = (Node *) malloc(sizeof(Node));
        temp->next = temp2;
    }
    return new;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用Eclipse调试器,它似乎在new-> head = temp崩溃.希望有人可以找到我的错误,因为我不能.

Bar*_*mar 5

您从未分配过新的队列.你需要:

Queue *new = malloc(sizeof(Queue));
Run Code Online (Sandbox Code Playgroud)