基本创建队列崩溃

时间:2018-04-17 01:13:57

标签: c

为什么程序会崩溃?虽然有时它没有。我使用的是visual studio 2017.我使用在线编译器运行我的整个程序(更多功能)并且它不会崩溃。谢谢。 [Image of the crash2 1 [Image of the crash] 2

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
}node;

typedef struct {
    int count;
    node *front;
    node *rear;
}QueueHead;

QueueHead *CreateQueue(){
    QueueHead *myQH = (QueueHead*)malloc(sizeof(myQH));
    //Empty list is created.
    //Emelent counter is set to zero
    myQH->count = 0;
    //Top should address NULL as it is an empty list
    myQH->front = NULL;
    myQH->rear = NULL;

    return myQH;
}

int main() {
    QueueHead *myQH = CreateQueue();
}

1 个答案:

答案 0 :(得分:0)

尝试更改代码行

QueueHead *myQH = (QueueHead*)malloc(sizeof(myQH));

QueueHead *myQH = (QueueHead*)malloc(sizeof(QueueHead));