![](/img/trans.png)
[英]Segmentation-fault Issue. Trying to implement a Doubly Linked List FIFO Queue in C
[英]Implement a FIFO list in C
我是一個新手程序員,我需要一些幫助。 我正在嘗試在C中實現FIFO列表(不是C ++或C#)。 這就是我定義結構的方式
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節點進行typedef
typedef struct node
{
Item data;
struct node* next;
} node;
然后清楚地知道它何時是節點*,何時不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.