Tag: linked list

C:typedef结构中的函数指针

我试图在C中创建一个链接列表,但试图在一些C ++样式类中很好地打包它。 我有一些问题,但在C中使用函数指针。 typedef struct linkedList { int count; struct msgNode *front; struct msgNode *back; void (*addMSG)(unsigned char *, int, struct linkedList *); } msgList; void addMSG(unsigned char *data, int size, struct linkedList *self); 理想情况下,我希望你可以让你列出然后添加你可以简单地在结构中调用一个“方法”(函数),模拟你在C ++中看到的行为。 当我调用addMSG时,我得到了一个分段错误,当然这是因为addMSG没有指向一个函数。 但是,我不想指定一个函数指向我想要使用链表的每一次。 有没有很好的方法来获得函数指针而不必隐式地指向函数,或者你是否必须隐式地指向函数? 这只是这里显示的部分实现。 最后,这个结构将具有所有必要的function。 这只是为了让这个问题简短而重要。

LIST_HEAD_INIT和INIT_LIST_HEAD之间的区别

我正在尝试理解Linux内核链表API。 根据Linux内核链接列表,我应该通过INIT_LIST_HEAD初始化列表头,但是这里(Linux内核程序)建议使用LIST_HEAD_INIT代替。 这是我写的一个工作代码,但我不确定我是否以正确的方式做到了。 有人可以validation它没问题吗? #include #include #include “list.h” typedef struct edge_attr { int d; struct list_head list; } edge_attributes_t; typedef struct edge { int id; edge_attributes_t *attributes; } edge_t; int main () { int i; struct list_head *pos; edge_attributes_t *elem; edge_t *a = (edge_t*)malloc(sizeof(edge_t)); a->id = 12; a->attributes = (edge_attributes_t*) malloc(sizeof(edge_attributes_t)); INIT_LIST_HEAD(&a->attributes->list); for (i=0; id […]

使用for循环创建链接列表

这是我的结构 struct ListItem{ int data; struct ListItem *next; }; 假设链表的第一个节点将有data = 0,我想写一个for循环,创建一个大小为5的链表,但我不知道如何工作 我尝试了以下内容 int main(int argc, char* argv[]){ struct ListItem a; a.data = 0; for (int i = 1; i next; } } 但结果是a.data = 0和a.next-> data = 4

实现链表时指针奇怪的问题

我正在尝试在C中实现链接列表,并且我想将头节点存储在单独的结构中。 但是,每当我添加另一个节点时,似乎都会以某种方式重新分配头节点。 #include #include struct BC_node { struct BC_node *next; void *data; }; struct BC_list { struct BC_node *head; struct BC_node *tail; }; void BC_list_push(struct BC_list *list, void *data) { struct BC_node *node = calloc(1, sizeof(struct BC_node)); if (list->head != NULL) printf(“head: %d\n”, *((int *) (list->head)->data)); node->next = NULL; node->data = data; if (list->head == […]

在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; } 我错过了什么?

删除链表中的第一个节点有问题

我正在实现一个链表,它需要有一个函数,当给定一个链表和一个cstring的头时,它会找到并删除一个值为cstring的节点。 typedef struct node { char entry[21]; struct node* next; } node; /*returns true if node with phrase value found, otherwise false*/ bool findAndRemove(node* root, char phrase[21]) { if(root != NULL) { node* previous = NULL; while(root->next != NULL) { if(strcmp(root->entry, phrase) == 0)//found { if(previous == NULL)//node to delete is at head { node* […]

使用指针和strtok()

我正在构建一个链表,需要你的帮助,因为我是C的新手。我需要输入一个如下所示的字符串:( (word)_#_(year)_#_(DEFINITION(UPPER CASE)) 例如:输入一个字符串输入: invest_#_1945_#_TRADE 基本上我正在寻找构建一个扫描定义的函数,并让我回到与之相关的单词。 输入要在字典中搜索的单词输入:TRADE输出:在“投资”一词中找到“TREADE” 到目前为止,我设法使用strtok()函数,但是现在我不知道如何处理打印第一个单词。 这是我能想到的: char split(char words[99],char *p) { p=strtok(words, “_#_”); while (p!=NULL) { printf(“%s\n”,p); p = strtok(NULL, “_#_”); } return 0; } int main() { char hello[99]; char *s = NULL; printf(“Enter a string you want to split\n”); scanf(“%s”, hello); split(hello,s); return 0; } 关于我该怎么做的任何想法?

C中的链表排序

我正在为我的一个类编写一个简单的文件,这是一个简单的链表活动,我需要对链表进行排序。 到目前为止这是我的源代码: /* * Simple list manipulation exercise. * 1. Create a list of integers. * 2. Print the list. * 3. Sort the list. * 4. Print the list * 5. Free the list nodes. */ #include #include struct node { int value ; struct node *next ; } ; extern struct node *mk_node(int […]

mergesort与链表的复杂性

我有使用链表的mergesort代码,它运行正常,我的问题是什么是这个算法的复杂性?是O(nlog(n))?还稳定吗?我感兴趣因为我知道mergesort是稳定的,怎么样使用链表?如果我们的元素彼此相等,这段代码是否保留了元素的顺序?非常感谢 #include #include struct node { int number; struct node *next; }; struct node *addnode(int number,struct node *next); struct node*mergesort(struct node *head); struct node *merge(struct node *one,struct node *two); int main(void){ struct node *head; struct node *current; struct node *next; int test[]={8,3,1,4,2,5,7,0,11,14,6}; int n=sizeof(test)/sizeof(test[0]); int i; head=NULL; for (i=0;inext) printf(“%4d\t%4d\n”,test[i++],current->number); /* free list */ for […]

将不同的单词保存到链表中

基本上我在这里有2个链表:列表和不同。 有一些单词已经保存在“list”结构中。 是否会写一个程序,它会找到不同/独特的单词并将其保存到’distinct’结构中。 这是我到目前为止根据指针的概念得到的。 然而,当我尝试打印’distinct’时,程序崩溃了:(如果我错了,请纠正我。 struct list { char string[50]; struct list *next; }; struct distinct { char string[50]; struct distinct *next; }; void checkdistinct() { list *ori = NULL; distinct *copy = NULL; distinct *check = NULL; if(ori == NULL && copy == NULL) { //first time. ori = ori->next; copy = copy->next; copy […]