题目描述 (线性表)试编写算法将线性表就地逆置,以链式存储结构实现。 PS:输入以eof结束。 输入 1 2 3 4 5 6 7 8 9 输出 9 8 7 6 5 4 3 2 1 使用C语言写出完整代码
时间: 2024-01-11 10:03:48 浏览: 51
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node, *LinkedList;
LinkedList createLinkedList() {
LinkedList L = (LinkedList) malloc(sizeof(Node));
L->next = NULL;
return L;
}
void insert(LinkedList L, int data) {
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->data = data;
newNode->next = L->next;
L->next = newNode;
}
void printList(LinkedList L) {
Node *p = L->next;
while (p) {
printf("%d ", p->data);
p = p->next;
}
}
void reverseList(LinkedList L) {
Node *p = L->next;
L->next = NULL;
while (p) {
Node *next = p->next;
p->next = L->next;
L->next = p;
p = next;
}
}
int main() {
LinkedList L = createLinkedList();
int data;
while (scanf("%d", &data) != EOF) {
insert(L, data);
}
reverseList(L);
printList(L);
return 0;
}