#include
typedef struct Node {
char data;
struct Node* next;
} Node;
void print_list(Node* root) {
while (root) {
printf("%c ", root->data);
root = root->next;
}
printf("\n");
}
Node* reverse(Node* root) {
Node* new_root = NULL;
while (root) {
Node* next = root->next;
root->next = new_root;
new_root = root;
root = next;
}
return new_root;
}
int main() {
Node f = { 'f', NULL };
Node e = { 'e', &f };
Node d = { 'd', &e };
Node c = { 'c', &d };
Node b = { 'b', &c };
Node a = { 'a', &b };
Node* root = &a;
print_list(root);
root = reverse(root);
print_list(root);
return 0;
}单链表倒序
最新推荐文章于 2021-09-20 14:12:53 发布
本文介绍了一个使用C语言实现的简单链表操作案例,包括链表的打印与反转功能。通过定义链表节点结构体,并实现遍历打印与反转链表的函数,展示了如何在C语言中操作链表数据结构。
1717

被折叠的 条评论
为什么被折叠?



