Queue 基于 LinkedList ,在创建新 Queue 时不能 return 空指针
Queue based on LinkedList , cannot return null pointer when creating new Queue
我正在尝试在 C 中基于 LinkedList 创建一个 Queue。
我有一个问题,当我尝试初始化 Queue 和 return QueueNode = NULL;
它仍然给了我一个内存地址,当我试图检查 Queue 是否为空时,结果总是不是。
如果我让 Queue pointer = NULL 在 main 中工作(指针等于 null)。
附上我的代码。
Queue节点Header:
typedef struct node
{
queueInfo value;
struct node* next;
}QueueNode;
QueueNode* createQueue();
int isEmptyQueue(QueueNode* Q);
void insert(QueueNode** Q, queueInfo x);
void delafter(QueueNode* p, queueInfo* x);
void PrintQueue(QueueNode* Q);
QueueNode.c
#include <stdio.h>
#include <stdlib.h>
QueueNode* createQueue()
{
/*
Aim: Initiolaze new Queue;
input: nothing
outpot: returns QueueNode pointer
*/
QueueNode* Q = NULL;
Q->next = NULL;
return Q;
}
int isEmptyQueue(QueueNode* Q)
{
/*
Aim: check if the Queue is empty
input: pointer to an queue
outpot: returns 1 if the queue is empty, 0 if not
*/
if ((!Q))
{
return 1;
}
return 0;
}
主要内容:
{
QueueNode* q = createQueue;
int res = isEmptyQueue(q);
printf("%d\n", res);
return 0;
}
来自调试模式的图片
enter image description here
至少这些问题:
分配 .next
的不当尝试
I am trying to create a Queue base on LinkedList in C. and I have an issue that when I tried to initialize the Queue and return QueueNode = NULL;
将 NULL
分配给 Q
是可以的,但是 Q->next = NULL;
是未定义的行为 (UB),因为 Q
没有指向太有效的内存。不要尝试 Q->next = NULL
因为不需要。
QueueNode* Q = NULL;
// Q->next = NULL;
return Q;
警告未全部启用
QueueNode* q = createQueue;
尝试将函数地址分配给 QueueNode
指针。相反,调用函数。启用警告的编译器会对此发出警告。
// QueueNode* q = createQueue;
QueueNode* q = createQueue();
我正在尝试在 C 中基于 LinkedList 创建一个 Queue。 我有一个问题,当我尝试初始化 Queue 和 return QueueNode = NULL; 它仍然给了我一个内存地址,当我试图检查 Queue 是否为空时,结果总是不是。 如果我让 Queue pointer = NULL 在 main 中工作(指针等于 null)。 附上我的代码。
Queue节点Header:
typedef struct node
{
queueInfo value;
struct node* next;
}QueueNode;
QueueNode* createQueue();
int isEmptyQueue(QueueNode* Q);
void insert(QueueNode** Q, queueInfo x);
void delafter(QueueNode* p, queueInfo* x);
void PrintQueue(QueueNode* Q);
QueueNode.c
#include <stdio.h>
#include <stdlib.h>
QueueNode* createQueue()
{
/*
Aim: Initiolaze new Queue;
input: nothing
outpot: returns QueueNode pointer
*/
QueueNode* Q = NULL;
Q->next = NULL;
return Q;
}
int isEmptyQueue(QueueNode* Q)
{
/*
Aim: check if the Queue is empty
input: pointer to an queue
outpot: returns 1 if the queue is empty, 0 if not
*/
if ((!Q))
{
return 1;
}
return 0;
}
主要内容:
{
QueueNode* q = createQueue;
int res = isEmptyQueue(q);
printf("%d\n", res);
return 0;
}
来自调试模式的图片 enter image description here
至少这些问题:
分配 .next
I am trying to create a Queue base on LinkedList in C. and I have an issue that when I tried to initialize the Queue and return
QueueNode = NULL;
将 NULL
分配给 Q
是可以的,但是 Q->next = NULL;
是未定义的行为 (UB),因为 Q
没有指向太有效的内存。不要尝试 Q->next = NULL
因为不需要。
QueueNode* Q = NULL;
// Q->next = NULL;
return Q;
警告未全部启用
QueueNode* q = createQueue;
尝试将函数地址分配给 QueueNode
指针。相反,调用函数。启用警告的编译器会对此发出警告。
// QueueNode* q = createQueue;
QueueNode* q = createQueue();