C 中的简单链表函数
Simple Linked List Functions in C
我正在尝试创建一个简单的列表,我可以在其中添加俱乐部成员(其中数据是有效的成员 ID 号、姓名、姓氏、年龄)。
在使用 add_to_list 函数时,我在使用 main 函数中的成员数据填充节点时遇到问题。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Creating structure for node
struct test_struct
{
int val; // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};
// declaring global head and curr pointers
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
// creating a list
struct test_struct* create_list(int val, char* name, char* lastn, int age)
{
printf("\n creating list with head node as [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr->age = age;
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
// add member to list
struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end)
{
if(NULL == head)
{
return (create_list(val, name, lastn, age));
}
if(add_to_end)
{
printf("\n Adding node to end of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}
else
{
printf("\n Adding node to beginning of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}
struct test_struct *ptr = malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr-> age = age;
ptr-> next = NULL;
if (add_to_end)
{
curr-> next = ptr;
curr = ptr;
}
else
{
ptr -> next = head;
head = ptr;
}
return ptr;
}
//search a name in created list
struct test_struct* search_in_list(char name, char lastn, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
printf("\n Searching the list for the value [%s][%s]\n", name, lastn);
while(ptr != NULL) // searching loop
{
if(ptr->name == name && ptr->lastn == lastn)
{
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
return ptr;
if(true == found)
{
if(prev)
{
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}
}
//printing the list
void print_list(void)
{
struct test_struct *ptr = head;
printf("\n -----Printing list Start----- \n");
while(ptr != NULL)
{
printf("\n [%d] \n", ptr -> val);
printf("\n [%s] \n", ptr -> name);
printf("\n [%s] \n", ptr -> lastn);
printf("\n [%d] \n", ptr -> age);
ptr = ptr->next;
}
printf("\n -----Printing list end---- \n");
return;
}
//printing the list 2 is for printing age only
void print_list2(void)
{
struct test_struct *ptr = head;
printf("\n -----Printing list Start----- \n");
while(ptr != NULL)
{
printf("\n [%d] \n", ptr -> age);
ptr = ptr->next;
}
printf("\n -----Printing list end---- \n");
return;
}
// main function
int main(void)
{
char n, l;
struct test_struct *ptr = NULL;
// for adding member to list
add_to_list(123, "william", "shakespeare", 30, true);
add_to_list(124, "william", "gibson", 35, true);
add_to_list(125, "chuck", "palahniuk", 40, true);
add_to_list(126, "mario", "puzio", 50, true);
add_to_list(127, "umberto", "eco", 60, true);
add_to_list(128, "ezra", "pound", 125, true);
print_list();
// for searching name in list
ptr = search_in_list(n, l, NULL);
if(NULL == ptr)
{
printf("\n Search [name = %s] [lastn = %s] failed, no such element found \n", n, l);
}
else
{
printf("\n Search passed [name = %s] [lastn = %s] \n", ptr->name, ptr->lastn);
}
print_list();
return 0;
}
编译器是否给您错误?您似乎没有正确调用 add_to_list
。它应该是这样的,
add_to_list(123, "william", "shakespeare", 30, true);
而且 add_to_list
似乎还没有实现。如果列表为空,它将调用创建第一个条目,否则它不会将新数据添加到列表中。
在 C 中使用链表时,我总是喜欢使用 sys/queue.h
库。您可以在 queue.h 中使用不同的实现,但这里是 man page 的列表示例:
LIST_HEAD(listhead, entry) head;
struct listhead *headp; /* List head. */
struct entry {
...
LIST_ENTRY(entry) entries; /* List. */
...
} *n1, *n2, *np;
LIST_INIT(&head); /* Initialize the list. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
LIST_INSERT_HEAD(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
LIST_INSERT_AFTER(n1, n2, entries);
/* Forward traversal. */
for (np = head.lh_first; np != NULL; np = np->entries.le_next)
np-> ...
while (head.lh_first != NULL) /* Delete. */
LIST_REMOVE(head.lh_first, entries);
我会对此发表评论,因为我知道这不是您要寻找的答案,但我强烈建议您使用此库。
你的链表实现看起来很好,除了以下问题
- 你的链表节点成员变量 "name" 和 "lname" 是 char 类型,但你试图将 char* 添加到列表中。
- search_in_list 由于输入参数 "prev" 而失败。看起来不需要。只需在 while 循环后添加 "return ptr"。
我正在尝试创建一个简单的列表,我可以在其中添加俱乐部成员(其中数据是有效的成员 ID 号、姓名、姓氏、年龄)。
在使用 add_to_list 函数时,我在使用 main 函数中的成员数据填充节点时遇到问题。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Creating structure for node
struct test_struct
{
int val; // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};
// declaring global head and curr pointers
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
// creating a list
struct test_struct* create_list(int val, char* name, char* lastn, int age)
{
printf("\n creating list with head node as [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr->age = age;
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
// add member to list
struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end)
{
if(NULL == head)
{
return (create_list(val, name, lastn, age));
}
if(add_to_end)
{
printf("\n Adding node to end of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}
else
{
printf("\n Adding node to beginning of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}
struct test_struct *ptr = malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr-> age = age;
ptr-> next = NULL;
if (add_to_end)
{
curr-> next = ptr;
curr = ptr;
}
else
{
ptr -> next = head;
head = ptr;
}
return ptr;
}
//search a name in created list
struct test_struct* search_in_list(char name, char lastn, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
printf("\n Searching the list for the value [%s][%s]\n", name, lastn);
while(ptr != NULL) // searching loop
{
if(ptr->name == name && ptr->lastn == lastn)
{
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
return ptr;
if(true == found)
{
if(prev)
{
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}
}
//printing the list
void print_list(void)
{
struct test_struct *ptr = head;
printf("\n -----Printing list Start----- \n");
while(ptr != NULL)
{
printf("\n [%d] \n", ptr -> val);
printf("\n [%s] \n", ptr -> name);
printf("\n [%s] \n", ptr -> lastn);
printf("\n [%d] \n", ptr -> age);
ptr = ptr->next;
}
printf("\n -----Printing list end---- \n");
return;
}
//printing the list 2 is for printing age only
void print_list2(void)
{
struct test_struct *ptr = head;
printf("\n -----Printing list Start----- \n");
while(ptr != NULL)
{
printf("\n [%d] \n", ptr -> age);
ptr = ptr->next;
}
printf("\n -----Printing list end---- \n");
return;
}
// main function
int main(void)
{
char n, l;
struct test_struct *ptr = NULL;
// for adding member to list
add_to_list(123, "william", "shakespeare", 30, true);
add_to_list(124, "william", "gibson", 35, true);
add_to_list(125, "chuck", "palahniuk", 40, true);
add_to_list(126, "mario", "puzio", 50, true);
add_to_list(127, "umberto", "eco", 60, true);
add_to_list(128, "ezra", "pound", 125, true);
print_list();
// for searching name in list
ptr = search_in_list(n, l, NULL);
if(NULL == ptr)
{
printf("\n Search [name = %s] [lastn = %s] failed, no such element found \n", n, l);
}
else
{
printf("\n Search passed [name = %s] [lastn = %s] \n", ptr->name, ptr->lastn);
}
print_list();
return 0;
}
编译器是否给您错误?您似乎没有正确调用 add_to_list
。它应该是这样的,
add_to_list(123, "william", "shakespeare", 30, true);
而且 add_to_list
似乎还没有实现。如果列表为空,它将调用创建第一个条目,否则它不会将新数据添加到列表中。
在 C 中使用链表时,我总是喜欢使用 sys/queue.h
库。您可以在 queue.h 中使用不同的实现,但这里是 man page 的列表示例:
LIST_HEAD(listhead, entry) head; struct listhead *headp; /* List head. */ struct entry { ... LIST_ENTRY(entry) entries; /* List. */ ... } *n1, *n2, *np; LIST_INIT(&head); /* Initialize the list. */ n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ LIST_INSERT_HEAD(&head, n1, entries); n2 = malloc(sizeof(struct entry)); /* Insert after. */ LIST_INSERT_AFTER(n1, n2, entries); /* Forward traversal. */ for (np = head.lh_first; np != NULL; np = np->entries.le_next) np-> ... while (head.lh_first != NULL) /* Delete. */ LIST_REMOVE(head.lh_first, entries);
我会对此发表评论,因为我知道这不是您要寻找的答案,但我强烈建议您使用此库。
你的链表实现看起来很好,除了以下问题
- 你的链表节点成员变量 "name" 和 "lname" 是 char 类型,但你试图将 char* 添加到列表中。
- search_in_list 由于输入参数 "prev" 而失败。看起来不需要。只需在 while 循环后添加 "return ptr"。