Tag: 单链表

使用Glib向GSList添加自定义结构

我正在尝试使用函数g_slist_append(list,&structure)将结构添加到单链表中。 这似乎工作(它添加指针),但我似乎无法找到一种方法来查看链接列表时结构中的元素。 我的结构看起来像这样: struct customstruct { int var1; int var2; char *string_1; } 然后,我列出一个列表: GSList *list = NULL; 然后,我附加一个结构的实例,如下所示: struct customstruct list_entry; list_entry.var1 = 1; list_entry.var2 = 2; list_entry.string_1 = “String”; list = g_slist_append(list, &entry); printf(“Entry var1 = %d\n”, list->data->var1); 最后一行失败,因为无法找到var1 (请求成员不是结构或联合)。 我想我需要把它投到正确的类型,但我不知道如何。 任何人?

链接列表,带参数的操作

我正在尝试实现程序,我可以动态创建〜任意数量的单链表,并对特定的列表执行操作(由参数定义)。 我创建了头指针的动态数组,以便我可以引用由paramater定义的某个头节点(数组的索引+ 1)。 参数只是(1,2,3 ……列表)。 到目前为止,我已经设法只实现初始化和推送function,但是在complilation之后的程序不能按预期工作。 问题出在哪儿? #include #include #include #define CHUNK 10 typedef struct { char *str; struct node *next; } node; node *initialise(node **array, int *amount_of_lists); void push(node **array, int *amount_of_lists); char *getString(void); int main() { node **heads = NULL; //initially null, pointer to the dynamic array of head pointers int amount_of_lists = […]

在C中反转单​​链表

可能重复: 如何仅使用两个指针反转单链表? 这是反转链表的C代码。 但这并不能产生理想的输出。 struct node *temp,*prev; while(head->next!=NULL) { temp=prev=head; while(temp->next->next!=NULL) { temp=temp->next; prev=prev->next; } temp=temp->next; temp->next=prev; prev->next=NULL; } 我错过了什么?

在C中创建单链表

我正在尝试从输入文本文件创建单个链接列表以进行分配。 我试图一次做一点,所以我知道我的代码不完整。 我尝试创建头指针,只是打印出它的值,我甚至无法让它工作,但我不知道为什么。 我包括了struct,我的创建列表和打印列表函数。 由于该部分有效,我没有包含打开的文件。 typedef struct List { struct List *next; /* pointer to the next list node */ char *str; /* pointer to the string represented */ int count; /* # of occurrences of this string */ } LIST; LIST *CreateList(FILE *fp) { char input[LINE_LEN]; LIST *root; /* contains root of list */ […]