Tag: 链表

在文本文件中扫描到链接列表

我只是在学习链接列表,我必须做一个包含许多部分的任务,但我已经开始了,我需要做的第一件事是将输入文件读入链表。 部分文件是: George Washington, 2345678 John Adams, 3456789 Thomas Jefferson, 4567890 James Madison, 0987654 James Monroe, 9876543 John Quincy Adams, 8765432 并包含总共26行。 我现在要做的只是简单地在文件中读取。 我尝试使用此代码(现在在main中) #include #include struct node{ char name[20]; int id; struct node *next; }*head; int main(void){ struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); head = temp; FILE *ifp; ifp = fopen(“AssignmentOneInput.txt”, […]

简单链接列表 – 无法访问节点

我正在尝试构建一个简单的链接列表但是我收到编译错误告诉我我正在尝试访问的链接列表节点不包含我期望它的字段。 这些是我的链接列表方法: typedef struct TinCan { int label; } TinCan; typedef struct LinkedListNode { TinCan *data; struct LinkedListNode *next; } LinkedListNode; typedef struct LinkedList { LinkedListNode *head; } LinkedList; LinkedList* createList() /*creates empty linked list*/ { LinkedList* myList; myList = (LinkedList*)malloc(sizeof(LinkedList)); myList->head = NULL; } 我malloc一个结构并将其发送到列表,如下所示: LinkedList* canQueue=createList(); TinCan* testCan = (TinCan*) malloc(sizeof(TinCan)); testProc->pid=69; insertLast(canQueue, […]

在链表上实现mergesort

我的任务是在用C / C ++编写的列表上实现合并排序算法。 我有一般的想法,编写我的代码并成功编译它。 但是,当我运行它时,它会开始正常,但然后挂起“准备好的列表,现在开始排序”而不会出现任何错误。 我试图查看我的代码,但我完全不知道问题是什么。 我也非常业余的调试,所以使用gdb尽我最大的能力导致我没有在哪里。 任何建议或提示都将是一个巨大的帮助,谢谢大家! #include #include struct listnode { struct listnode *next; int key; }; //Finds length of listnode int findLength (struct listnode *a) { struct listnode *temp = a; int i = 0; while (temp != NULL) { i++; temp = temp->next; } return i; } struct listnode * […]

使用循环将C值添加到链接列表

我有一个链接列表,我正在尝试为其添加值。 但是我必须错误地设置我的指针,或者内存分配有问题。 我想将令牌添加到列表中,但每次有新的循环时数据都会重叠。 例如: 第一次: repl> a a 第二次: repl> b b b 注意a是如何消失的,我想在添加新值时保留先前的值。 到目前为止,这是我的代码: struct node { int val; struct node *next; }; struct node *head = NULL; struct node *cur = NULL; struct node* create_list (int value) { struct node *ptr = (struct node*) malloc(sizeof (struct node)); if (NULL == ptr) return NULL; […]

如何从链表中弹出元素?

我正在尝试使用各种推送和弹出function来学习链表,但我无法从链表中的尾部弹出元素。 任何人都可以帮我解决popBackfunction吗? 我尝试过类似的东西: typedef struct { float val; }data; typedef struct nodePtr { struct nodePtr *next; data *d; }node; typedef struct { node *head; node *tail; }linkList; linkList* createlinkList() { linkList *ll = (linkList*)malloc(sizeof(linkList)); ll->head = NULL; ll->tail = NULL; return ll; } node* createNode(data *d) { node *nd = (node*)malloc(sizeof(node)); nd-> d = d; […]

C – 如何调用链表中的第一个元素?

我想要一个链表来排序,然后能够显示它。 我的代码的问题是,我可以在排序之前显示它,但在排序后,它将不会显示,它将崩溃。 我认为它与“top”变量有关,因为通过调试,它不包含任何内容。 如何调用链表中的第一个元素并使用它来显示它们? 我真的很困惑。 以下仅是显示和排序function。 //Sort and display all employees void displayAllEmps() { if(numEmps == 0) { printf(“No employees are hired.”); fflush(stdout); } else { char output[80]; struct EMP* emp = top; int i; for(i = 1; i next; } } } //Sort function to call insertion sort function void sortEmps() { temp = […]

使用时间函数的链接列表打印错误

打印时间戳时程序崩溃。 我相信错误位于函数void flightRec_PrflightRecData(flightRecRead * thisFlight)中,它旨在做三件事: 声明时间结构的飞行时间,飞行时间是POSIX格式。 Localtime将POSIX时间转换为人类可读的时间。 第四个说明符使用asctime打印转换时间,该时间以Www Mmm dd hh:mm:ss yyyy格式打印。 错误是tb!= NULL并显示指定asctime的其他信息。 我做了什么来排除故障: 检查时间标题 检查指针和地址 关闭了弃用 检查格式说明符 任何帮助表示赞赏。 #include #include #include #include typedef struct flightRec_struct { // declare a struct to match the format of the binary data char FlightNum[7]; char OriginAirportCode[5]; char DestAirportCode[5]; int timestamp; } flightRec; typedef struct flightRecRead_struct { // […]

分段错误和链接列表的未知问题

所以我正在尝试编写一个机械程序。 程序很长但是这里有导致我问题的函数: recherche_noe和creation_noe 。 不需要打扰其余的。 这是法语所以请耐心lst_noe我的想法是这样的:首先在main我向用户询问lst_noe的noe lst_noe (这是noe列表)。 使用creation_noe他在向用户询问结构信息时做出了这一点。 最后recherche_noe返回我正在寻找的noe 。 所有信息都存储在struct maillage ,你有其他结构。 谢谢您的帮助。 #include #include #include /*==============================================*/ /* Déclaration des structures */ /*==============================================*/ struct matrice { char nom[20]; int n,m; double **tab; struct matrice *next; }; struct element { int num; int n1, n2; double k; struct element *next; }; struct noeud { […]

C链接列表错误

我需要一些认真的帮助来理解C ++中的链接列表我想要采用我几周前使用数组结构编写的程序并将它们转换为链接列表并添加几个新函数。 我最担心的是我对链接列表没有信心,并且花时间在这里和其他网站获取有关它们的知识。 但我找不到一个可以帮助我解决我现在遇到的问题的来源。 这是我的原始代码: #include #include #include #include #define MAX 100 struct YouTubeVideo { char video_name[1024]; // YouTube video name int video_ranking; // Number of viewer hits char video_url[1024]; // YouTube URL }; struct YouTubeVideo Collection[MAX]; int tail = 0; //– Forward Declaration –// void printall(); void insertion(); void sort(); void branching(char option); void […]

C字节读取器中的分段故障(核心转储)

我有一个系统操作类简介,它有两个不同的部分,第一个是读取可执行文件的简单程序,逐个字节,输出的字符串长度至少为4个字符。 它是您可以在UNIX中使用的字符串程序(命令)的简单建模。 我遇到了三个单独的示例可执行文件的分段错误(核心转储)错误。 我理解这实际上意味着我正在尝试访问一些我无法访问的内存地址(通过过度获取程序拥有的已分配块,或者通过其他方式)。 不幸的是,我不明白为什么这个程序正在这样做。 我认为问题在于我的链表实现 – 我用它来存储可读的字符,然后在不可读的字符出现时检查链表是否有4个条目。 如果有,我打印出来。 然后我清除链表并重新开始。 我逐字节遍历文件,我觉得这个程序的逻辑是合理的。 但是,我对指针,地址和malloc的完全理解并不合理。 由于缺乏知识,我预感到分段错误正在发生。 有人可以看看下面的代码,并找出我做错了什么? 最重要的是,你能解释一下我滥用的概念,为什么? 我担心程序应该按照它应该的方式运行,是的,但也担心我缺乏理解。 代码如下 – 谢谢。 #include #include struct node{ char ANSII; struct node *next_node; }; void clear_list(struct node *first_node); void print(struct node *first_node); int counter(struct node *first_node); void append(char temp, struct node *first_node); int main(int argc, char **argv){ FILE *f = […]