我是一个新手程序员,我需要一些帮助。我正试图在C中实现FIFO列表(不是c++也不是c#)。这就是我如何定义struct
的typedef struct node *link;
struct node{
Item item;
link next;
};
我正在尝试用这个函数添加节点到我的列表。
void add(Item newItem, link head, link tail)
{ link helper;
helper = malloc(sizeof(link));
helper -> item = newItem;
if (head == NULL){
head = helper;
tail = helper;
}
else{
tail -> next = helper;
tail = helper;
}
}
但是当我使用showItem(tail -> item)函数时;主要是我得到一个分割错误。
分配节点时,使用节点的大小,而不是指针的大小
helper = malloc( sizeof( struct node ) );
最好不要对指针进行类型定义,因为这会使它们难以在各种上下文中看到,而是对结构节点进行类型定义
typedef struct node
{
Item data;
struct node* next;
} node;
那么当它是一个节点*时它就会被清除,当它不是。