本文介绍了HELP,INFINIT LOOP ...简单的链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这里是一个简单的链表程序。我认为DeleteNode函数是 产生一个无限循环,但我不知道在哪里.. #include< stdio.h> typedef struct { char * str; // str是一个动态的字符数组 int length; //字符数 }字符串; typedef结构节点 { 字符串数据; struct node * Link; } ListNode; typedef struct listStuct { ListNode * Head; }列表; void AddNode(List * L,String item) { ListNode * currNode,* newNode; currNode = L-> Head; if(L-> Head = = NULL) { L-> Head =(ListNode *)malloc(sizeof(ListNode)); L-> Head-> Data = item; L-> Head-> Link = NULL; } else { while(currNode-> Link!= NULL) currNode = currNode-> Link; //转到最后一个 newNode =(ListNode *)malloc(sizeof(ListNode)); newNode-> Data = item; newNode-> Link = NULL; currNode-> Link = newNode; } } void DeleteNode(List * L,String item) { ListNode * currNode,* prevNode; currNode = L-> Head; prevNode = L-> Head; if(L-> Head == NULL) { printf("空列表"); } while(currNode!= NULL)//检查是否第一次 { if(strcmp(currNode-> Data.str,item.str)== 0)//找到 { if(currNode == L-> Head)//第一个 { L-> Head == currNode-> Link; currNode-> Link = NULL; free(currNode); printf(" Remove First"); } 其他 { prevNode-> Link = currNode->链接; currNode - > Link = NULL; 免费( currNode); printf("删除"); } } 其他 { prevNode = currNode; currNode = currNode-> Link; //转到下一个 } } } int main() { FILE * fp = NULL; 列表清单; 字符串字符串; int i ; int records = 0; char filename [100]; //list.Head =(ListNode *)malloc(sizeof( ListNode)); list.Head = NULL; string.str =(char *)malloc(sizeof(char)* 20); printf(" Enter filename \\\"); scanf("%s",filename); fp = fopen(filename," r" ;); if(fp == NULL) {printf(" Error\ n);; 退出(0); } 其他 { fscanf(fp,"%d", (&=;记录); for(i = 0; i< records; i ++) { fscanf(fp,"%s" ;,string.str); AddNode(& list,string); printf("%s",string.str); } printf(&\\; \ n \ n输入节点到删除:"); scanf("%s",string.str); DeleteNode(& list,string); } 返回1; } here''s a simple linked list program. the DeleteNode function isproducing an infinit loop i think, but i can''t figure out where..#include <stdio.h>typedef struct{char *str; //str is a dynamic array of charactersint length; //number of characters} String; typedef struct node{String Data;struct node* Link;} ListNode; typedef struct listStuct{ListNode *Head;}List; void AddNode(List *L, String item){ListNode *currNode, *newNode;currNode = L->Head;if (L->Head == NULL){L->Head = (ListNode*)malloc(sizeof(ListNode));L->Head->Data = item;L->Head->Link = NULL;}else{while(currNode->Link != NULL)currNode = currNode->Link; //go to the lastnewNode = (ListNode*)malloc(sizeof(ListNode));newNode->Data = item;newNode->Link = NULL;currNode->Link = newNode;}} void DeleteNode(List *L, String item){ListNode *currNode, *prevNode;currNode = L->Head;prevNode = L->Head;if (L->Head == NULL){printf("empty list");}while(currNode != NULL) //check if first{if(strcmp(currNode->Data.str,item.str)==0) //found{if(currNode == L->Head) //first one{L->Head == currNode->Link;currNode->Link = NULL;free(currNode);printf("Remove First");}else{prevNode->Link = currNode->Link;currNode->Link = NULL;free(currNode);printf("removed ");}}else{prevNode = currNode;currNode = currNode->Link; //go to next one}}}int main(){FILE *fp = NULL;List list;String string;int i;int records = 0;char filename[100];//list.Head = (ListNode*)malloc(sizeof(ListNode));list.Head = NULL;string.str = (char *)malloc(sizeof(char) *20);printf("Enter filename\n");scanf("%s", filename);fp = fopen(filename, "r"); if (fp == NULL){printf ("Error\n");exit(0);}else{fscanf(fp, "%d", &records);for (i = 0; i<records; i++){fscanf(fp, "%s", string.str);AddNode(&list, string);printf("%s ", string.str);}printf("\n\nEnter a Node to delete: ");scanf("%s", string.str);DeleteNode(&list, string);}return 1;}推荐答案 还有其他一些代码有问题,但我不会在这里进入 。但一般来说,你有特殊情况需要处理的事实通常表明你的设计比你需要的更复杂。\\ b 。考虑一下 - 如果你仍然遇到 麻烦,请务必重新发布。 HTH, - ag - Artie Gold - 德克萨斯州奥斯汀 哦,对于常规老垃圾邮件的美好时光。 There are some other things wrong with the code, but I won''t go intothat here. In general, though, the fact that you have special casesto deal with typically indicates that your design is more complexthan it needs to be. Think about that -- and if you still run intotrouble, by all means repost. HTH,--ag--Artie Gold -- Austin, TexasOh, for the good old days of regular old SPAM. - Richard Heathfield: bi****@eton.powernet.co.uk Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。 C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K& R答案,C书等: http://users.powernet.co.uk/eton --Richard Heathfield : bi****@eton.powernet.co.uk"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.C FAQ: http://www.eskimo.com/~scs/C-faq/top.htmlK&R answers, C books, etc: http://users.powernet.co.uk/eton 这篇关于HELP,INFINIT LOOP ...简单的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 09:14