用c语言编写实现带头结点单链表就地逆置的成员函数并编写主函数测试结果
时间: 2024-04-29 18:23:04 浏览: 113
```
#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 insertNode(LinkedList L, int data) {
Node *p = (Node*)malloc(sizeof(Node));
p->data = data;
p->next = L->next;
L->next = p;
}
void printLinkedList(LinkedList L) {
Node *p = L->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void reverseLinkedList(LinkedList L) {
Node *p = L->next;
L->next = NULL;
while (p != NULL) {
Node *q = p->next;
p->next = L->next;
L->next = p;
p = q;
}
}
int main() {
LinkedList L = createLinkedList();
insertNode(L, 3);
insertNode(L, 2);
insertNode(L, 1);
printf("Original list: ");
printLinkedList(L);
reverseLinkedList(L);
printf("Reverse list: ");
printLinkedList(L);
return 0;
}
```
输出结果:
```
Original list: 1 2 3
Reverse list: 3 2 1
```
阅读全文