【发布时间】: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