假设我有字符串“Lamps”,它被传递到我的程序中,每个字符都存储到链表中的一个节点中。
我需要使用另一个链表以相反的顺序复制该列表,我该怎么做,我已经走了很远,但是如何在链表中向后移动?
您将看到该行评论了我需要放在那里以在链接列表中向后移动的内容。
#include <stdlib.h>
#include <stdio.h>
struct NODE {
struct NODE *next;
char data;
};
int main(int argc, char *argv[]) {
int i;
struct NODE *head;
struct NODE *current;
struct NODE *head2;
struct NODE *current2;
struct NODE *finger;
for(i = 0; i < argc; i++)
printf("arg %d: %s\n", i, argv[i]);
head = (struct NODE*)malloc(sizeof(struct NODE));
current = head;
for ( i = 0; i < sizeof(argv[1]) - 1; i++ ) {
current -> data = argv[1][i];
current -> next = (struct node*)malloc(sizeof(struct NODE));
current = current -> next;
current -> next = NULL;
}
head2 = (struct NODE*)malloc(sizeof(struct NODE));
current2 = head2;
while ( current != head) {
finger = head;
while (finger -> next != current)
finger = finger -> next;
current2 -> data = current -> data;
current2 -> next = (struct node*)malloc(sizeof(struct NODE));
current2 = current2 -> next;
// move backwards
} // ends loop
}
return 0;
}