我正在开发一个需要实现链表的项目。在我开始这个项目之前,我正在审查创建链表的经典方法。
我意识到在过去,我一直在通过遍历列表的head
指针向链表添加元素,直到它到达空指针。
我发现没有必要这样做,并且以涉及tail
指针的方式实现它,但我能想到的唯一方法是涉及三重指针的方式或全局指针。我想知道,是否有更简单的方法使用较少的间接级别?
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int val;
struct list *next;
} list;
void printlist(list *head);
void freelist(list *head);
void AddList(list ***tail, int val);
int main(void)
{
/* create first node */
list *head = (list *)malloc(sizeof(*head));
/* assign members of first node */
head->val = 1;
head->next = NULL;
/* create tail */
list **tail = &head->next;
/* add values to list */
AddList(&tail, 2);
AddList(&tail, 3);
AddList(&tail, 4);
AddList(&tail, 5);
AddList(&tail, 6);
AddList(&tail, 7);
AddList(&tail, 8);
AddList(&tail, 9);
/* print list */
printlist(head);
/* free list */
freelist(head);
/* exit program */
system("pause");
return 0;
}
void AddList(list ***tail, int val)
{
list *node = (list *)malloc(sizeof(*node));
node->val = val;
node->next = NULL;
**tail = node;
*tail = &node->next;
}
void printlist(list *head)
{
while (head) {
printf("%d\n", head->val);
head = head->next;
}
}
void freelist(list *head)
{
list *tmp = head;
while (head) {
head = head->next;
free(tmp);
tmp = head;
}
}
答案 0 :(得分:2)
tail
指针应指向最后一个节点,而不是最后一个节点的next
指针:
在main
:
/* create tail */
list *tail = head;
在AddList
:
void AddList(list **tail, int val)
{
list *node = malloc(sizeof(*node));
node->val = val;
node->next = NULL;
(*tail)->next = node;
*tail = node;
}
答案 1 :(得分:1)
你可以这样做:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node NODE;
struct llist {
unsigned int count;
NODE *head;
NODE *tail;
};
typedef struct llist LLIST;
LLIST *create_list() {
LLIST *llist = malloc(sizeof(LLIST));
if (llist == NULL)
exit(EXIT_FAILURE);
llist->head = NULL;
llist->tail = NULL;
llist->count = 0;
return llist;
}
NODE *create_ll_node(int data) {
NODE *node = malloc(sizeof(NODE));
if (node == NULL)
exit(EXIT_FAILURE);
node->data = data;
node->next = NULL;
return node;
}
void add_list_head(LLIST *llist, int data) {
NODE *node = create_ll_node(data);
if (llist->head == NULL) {
llist->head = node;
llist->tail = node;
} else {
node->next = llist->head;
llist->head = node;
}
llist->count++;
}
void add_list_tail(LLIST *llist, int data) {
NODE *node = create_ll_node(data);
if (llist->tail == NULL) {
llist->head = node;
llist->tail = node;
} else {
llist->tail->next = node;
llist->tail = node;
}
llist->count++;
}
void delete_list_elements(NODE *llist_head) {
NODE *node, *tmp;
if (llist_head == NULL) {
printf ("List is empty.\n");
return;
}
node = llist_head;
while(node) {
tmp = node->next;
free(node);
node = tmp;
}
}
void delete_list(LLIST *llist) {
if (llist == NULL) {
printf ("Invalid list.\n");
return;
}
delete_list_elements(llist->head);
free(llist);
}
void display_list(const LLIST *llist) {
if (llist == NULL) {
printf ("Invalid list.\n");
return;
}
if (llist->head == NULL) {
printf ("List is empty.\n");
return;
}
NODE *node = llist->head;
while(node) {
printf ("data: %d\n", node->data);
node = node->next;
}
}
unsigned int get_list_element_count(const LLIST *llist) {
if (llist == NULL)
return 0;
return llist->count;
}
int main() {
LLIST *llist = create_list();
add_list_head(llist, 5);
add_list_head(llist, 4);
add_list_head(llist, 3);
add_list_head(llist, 2);
add_list_head(llist, 1);
add_list_tail(llist, 6);
add_list_tail(llist, 7);
add_list_tail(llist, 8);
add_list_tail(llist, 9);
add_list_tail(llist, 10);
display_list(llist);
printf ("Total elements in list : %u\n", get_list_element_count(llist));
delete_list(llist);
return 0;
}