具有导致无限循环的函数的链表
Linked list with function resulting in infinite loop
我想做一个简单的链表,只有一个函数和两个指针。
#include <stdio.h>
#include <stdlib.h>
typedef struct leonor{
int x;
struct leonor * next;
}leo;
出于练习的目的,我总是寻找列表的末尾以添加我创建的最后一个新节点。函数(add)如下:
void add(leo **ad)
{
int i;
leo *current, *new, **previous; /*Previous points on the pointer NEXT
*of an element*/
new=malloc(sizeof(*new));
for (i=0;i<3;i++)
{
printf("x : ");
scanf("%d",&new->x);
new->next=NULL;
previous = ad; /*'previous' receives adresse of head of list*/
current = *previous;
while(current != NULL) /*Look for last element of list*/
{
previous = &(current->next);
current = current->next;
}
new->next = *previous;
*previous = new;
}
}
其余代码:
void display_(leo *hl)
{
while (hl)
{
printf("%d -> ",hl->x);
hl=hl->next;
}
}
int main()
{
leo * head;
head = NULL;
add(&head);
display_(head);
return 0;
}
问题是在创建链表(这里是 3 个整数的列表)之后,它总是只包含最后输入的数字。并且在显示结果时是相同所述数字的无限循环。将不胜感激帮助。
您只需分配一个 struct leo
并为您添加的每个元素使用相同的一个。您需要为放入列表的每个元素分配一个新元素。
您似乎试图将您的 link 条目信息与特定数据 information/types 结合起来。一般地执行 link 列表内容。那么实际的询问代码就容易写了。使用您的 link 列表创建列表的代码将很容易。
您的 link 列表条目结构应该如下所示(按照书本):
typedef struct{
void* prev;
void* data;
void* next;
}link_entry;
根据该结构构建代码 - 它会让生活变得更轻松。每次要查询数据时都需要强制转换,但这会使您的代码变得非常非常简单。
typedef struct
{
void* first;
void* last;
int n;
} LLst;
一些简单的函数:
LList create()
{
return(calloc(sizeof(LList));
}
list_entry* add_entry(LList* list, void* data)
{
list_entry* entry = malloc(sizeof(list_entry);
entry->prev = list->last;
entry->data = data;
entry->next = NULL;
list->last=entry;
if(!list->first) list->first=list->last;
list->n++;
return entry;
}
void del_entry(LList* list, list_entry* entry)
{
/* you may wish to add a free data client procedure to free the data as well */
list->last=entry->prev;
if(entry) free(entry);
list->n--;
if(!list->n) list->first=list->last=NULL;
return entry;
}
我想做一个简单的链表,只有一个函数和两个指针。
#include <stdio.h>
#include <stdlib.h>
typedef struct leonor{
int x;
struct leonor * next;
}leo;
出于练习的目的,我总是寻找列表的末尾以添加我创建的最后一个新节点。函数(add)如下:
void add(leo **ad)
{
int i;
leo *current, *new, **previous; /*Previous points on the pointer NEXT
*of an element*/
new=malloc(sizeof(*new));
for (i=0;i<3;i++)
{
printf("x : ");
scanf("%d",&new->x);
new->next=NULL;
previous = ad; /*'previous' receives adresse of head of list*/
current = *previous;
while(current != NULL) /*Look for last element of list*/
{
previous = &(current->next);
current = current->next;
}
new->next = *previous;
*previous = new;
}
}
其余代码:
void display_(leo *hl)
{
while (hl)
{
printf("%d -> ",hl->x);
hl=hl->next;
}
}
int main()
{
leo * head;
head = NULL;
add(&head);
display_(head);
return 0;
}
问题是在创建链表(这里是 3 个整数的列表)之后,它总是只包含最后输入的数字。并且在显示结果时是相同所述数字的无限循环。将不胜感激帮助。
您只需分配一个 struct leo
并为您添加的每个元素使用相同的一个。您需要为放入列表的每个元素分配一个新元素。
您似乎试图将您的 link 条目信息与特定数据 information/types 结合起来。一般地执行 link 列表内容。那么实际的询问代码就容易写了。使用您的 link 列表创建列表的代码将很容易。
您的 link 列表条目结构应该如下所示(按照书本):
typedef struct{
void* prev;
void* data;
void* next;
}link_entry;
根据该结构构建代码 - 它会让生活变得更轻松。每次要查询数据时都需要强制转换,但这会使您的代码变得非常非常简单。
typedef struct
{
void* first;
void* last;
int n;
} LLst;
一些简单的函数:
LList create()
{
return(calloc(sizeof(LList));
}
list_entry* add_entry(LList* list, void* data)
{
list_entry* entry = malloc(sizeof(list_entry);
entry->prev = list->last;
entry->data = data;
entry->next = NULL;
list->last=entry;
if(!list->first) list->first=list->last;
list->n++;
return entry;
}
void del_entry(LList* list, list_entry* entry)
{
/* you may wish to add a free data client procedure to free the data as well */
list->last=entry->prev;
if(entry) free(entry);
list->n--;
if(!list->n) list->first=list->last=NULL;
return entry;
}