繁体   English   中英

如何在C中创建一个带有头尾指针的双向循环链表?

[英]How to create a doubly circular linked list with a head and tail pointer in C?

我目前正在编写以下代码,以便在 C 中创建一个双向循环链表。当给定 2 或 3 个数字作为参数时,该代码确实可以工作,但是从第四个数字开始,一些数字开始从新创建的列表中消失一些奇怪的原因。

#include <stdio.h>
#include <stdlib.h>

typedef struct s_node
{
        int                             value; // each node contains a integer storing an arbitrary value
        struct s_node   *next;
        struct s_node   *prev;
}                               t_node;

static void     create(t_node **head, t_node **tail, int value, int i)
{
        t_node  *tmp; // create temporary node
        t_node  *result; // create the new node

        tmp = *head;
        result = malloc(sizeof(t_node)); // malloc the new node
        if (!result)
                return ;
        result->value = value;
        result->next = NULL;
        if (*head == NULL) // if the head is NULL, result is the first node to be added
        {
                result->prev = NULL;
                *head = result;
                return ;
        }
        while (tmp->next != NULL && i-- > 0)
                tmp = tmp->next; // parse the linked list until the end
        tmp->next = result; // set result as the next pointer to the last element of the linked list        result->prev = tmp; // set the head has to the previous pointer of the new node
        *tail = result; // tail is now the new node
        (*tail)->next = *head; //link the tail's next pointer to the head to make it circular
        (*head)->prev = *tail; // link the head's previous pointer to make it backwards circular
}

void    save(t_node **head, t_node **tail, int argc, char **argv)
{
        int     i;

        i = 1;
        while (i < argc) // go through the list of given numbers as argument
        {
                create(head, tail, atoi(argv[i]), i); // append a new node to the linked list
                i++;
        }
}

int     main(int argc, char **argv)
{
        t_node  *head;
        t_node  *tail;
        t_node  *tmp;
        int     i;

        i = 4;
        head = NULL;
        tail = NULL;
        if (argc == 0)
                return (0);
        save(&head, &tail, argc, argv);
        tmp = head;
        while (tmp->next != NULL && i-- > 0)
        {
                printf("%d - ", tmp->value);
                tmp = tmp->next;
        }
        printf("\n");
        return (0);
}

编译和执行时使用两个参数,如下所示:

./a.out 10 20

返回以下内容(这是正确的):

10 - 20 - 10 - 20 -

当使用三个参数(10、20 和 30)执行时,会打印以下内容(这是正确的):

10 - 20 - 30 - 10 -

但是,当将第四个数字添加到列表中时(10、20、30 和 40),将打印以下不正确的列表:

10 - 20 - 40 - 10 -

30号消失了,我真的不明白为什么。 有人可以帮我吗?

我在 main 中发现了一个错误

        int     i;

        i = 4;

改成这个

while (argc-- > -1)

不要声明t

另一个错误是在函数创建中,更改为此

        --i;
        while (--i){
                tmp = tmp->next; // parse the linked list until the end
        }

我在windows下运行,效果很好。

编辑:某些变量可以声明为全局

typedef struct s_t_node
{
    int value;
    struct s_t_node *prev, *next;
} t_node;

t_node *head=NULL, *tmp=NULL, *tail=NULL;

void create(int value){
        t_node* result=malloc(sizeof(t_node));
        result->value=value;
        result->prev=NULL;
        result->next=NULL;
        if(head==NULL){
                head=result;
                tail=result;
                return;
        }
        tmp=head;
        while(tmp->next)
                tmp=tmp->next;
        tmp->next=result;
        result->prev=tmp;
}

int main(int argc, char const *argv[])
{
        if (argc==1)
                return 0;
        for (int i = 1; i < argc; ++i)
        {
                create(atoi(argv[i]));
        }
        tmp=head;
        while(tmp->next)
                tmp=tmp->next;
        tail=tmp;
        head->prev=tail;
        tail->next=head;
        tmp=head;
        argc-=2;
        do{
                printf("%d - ", tmp->value);
                tmp=tmp->next;
        }while(argc--);

        return 0;
}

暂无
暂无

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

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