繁体   English   中英

通过尾指针添加到链接列表,而没有3个间接级别

[英]Add to linked list via tail pointer without 3 levels of indirection

我正在一个需要实现链表的项目。 在开始该项目之前,我正在回顾创建链接列表的经典方法。

我意识到,过去,我一直通过遍历列表直到到达空指针的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;
    }
}

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;
}

你可以做:

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM