【问题标题】:C - Linked list printing in the wrong orderC - 链表打印顺序错误
【发布时间】:2015-03-22 00:36:07
【问题描述】:

我创建了一个链表,其元素是从命令行参数获取的字符串:

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

    struct element_Args {
        char commandLineArgs[500];
    };


    struct list {

    struct element_Args element;
    struct list *next;
    };

    int main(int argc, char *argv[]) {

    struct list *head;
    struct list *current;

    head = (struct list *) malloc(sizeof(struct list));


    head->next = NULL;

    int i;
    for(i = 0; i < argc; i++) {
        current = malloc (sizeof(struct list));
        strcpy(current->element.commandLineArgs, argv[i]);
        current->next = head;
        head = current;

    }

    current = head;

    while(current->next != NULL) {
       printf("%s\n", current->element.commandLineArgs);
       current = current->next;
    }

    return 0;

    }

但是,当我打印链接列表中的元素时,它们会以与作为参数输入时相反的顺序打印出来。如何按照输入的顺序打印它们?我觉得好像我错过了一些小东西,但我不知道那是什么。

【问题讨论】:

    标签: c loops printing linked-list iteration


    【解决方案1】:

    这是你的问题

    head = current;
    

    你应该让head指向第一个节点,并且永远不要再覆盖它。

    因此,在您的代码中,head 实际上是 tail,因此以相反的顺序打印值是合乎逻辑的,您期望它的反转不是反转。

    试试这个

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct element_Args {
        char commandLineArgs[500];
    };
    
    
    struct list {
    
    struct element_Args element;
    struct list *next;
    };
    
    int main(int argc, char *argv[]) {
        struct list *head;
        struct list *current;
        struct list *last;
        int i;
    
        head = malloc(sizeof(*head));
        if (head == NULL)
            return -1;
        head->next = NULL;
    
        last = head;
        for(i = 0 ; i < argc ; i++) {
            current = malloc (sizeof(*current));
            if (current != NULL) {
    
                strcpy(current->element.commandLineArgs, argv[i]);
    
                last->next = current;
                last       = current;
            }
        }
    
        current = head;
        while(current != NULL) {
            printf("%s\n", current->element.commandLineArgs);
            current = current->next;
        }
    
        return 0;
    }
    

    不要忘记将freeList() 函数写入free 所有malloced 的东西。

    【讨论】:

    • 那么在 for 循环开始之前让 head 指向第一个节点?
    • 是的,永远不要再更改它,跟踪 previous 节点,并制作 current previousnext,然后制作 current previous
    • 我明白了。我会讨论这个。谢谢!
    【解决方案2】:

    在您的 for 循环中,删除 head = current

    基本上,您使用这条线会迷失方向。您可以稍后通过设置临时指针遍历head,但不要重置head(除非您插入新的head)。

    要插入一个新的头部,你会说,newHead-&gt;next = head;head = newHead; 如果你想按顺序插入它们,你应该保留一个尾指针并始终添加到末尾。

    int i;
    struct list* tail = head;
    for(i = 0; i < argc; i++) {
        current = malloc (sizeof(struct list));
        if(current != NULL){
            strcpy(current->element.commandLineArgs, argv[i]);
            tail->next =  current; // add this line
            tail = tail->next;
            current->next = head; //this line makes you add in reverse order. Remove this as well.
    
            head = current; // remove this line here
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 2021-07-24
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多