#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
typedef struct Node {
struct Node* next;
int data;
};
Node *CreateLink()
{
Node *list = (Node*)malloc(sizeof(Node));
list->next = NULL;
return list;
}
Node* findLinkLast(Node* list) {
while (list->next) {
list = list->next;
}
return list;
}
void insert(Node* list, int a) {
Node *p = (Node*)malloc(sizeof(Node));
p->data = a;
p->next = NULL;
Node* q = findLinkLast(list);
q->next = p;
}
int main()
{
Node *list = CreateLink();
int a;
while (scanf("%d", &a))
insert(list, a);
Node* p = list->next;
while (p) {
printf("%d", p->data);
p = p->next;
}
system("pause");
return 0;
}
输出单链表中所有元素
遍历单链表并输出所有节点
最新推荐文章于 2022-07-20 22:36:12 发布
本文详细讲解如何遍历单链表,并提供代码示例,展示如何依次输出链表中的每一个元素,帮助理解数据结构中的链表操作。
2465

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



