标签:style class blog c code ext
/*---编写打印出一个单链表的所有元素的程序---*/
#include <stdio.h>
#include <stdlib.h>
struct Node{
int val;
struct Node *next;
};
Node *findEnd(Node *list){
while(list->next) list = list->next;
return list;
}
void insert(int val, Node *list){
Node *p = (Node *)malloc(sizeof(Node));
p->val = val; p->next = NULL;
Node *end = findEnd(list);
end->next = p;
}
int main(){
Node *list = (Node *)malloc(sizeof(Node)), *p;
list->next = NULL;
int a;
while(scanf("%d", &a) == 1)
insert(a, list);
p = list->next;
while(p){
printf("%d ", p->val);
p = p->next;
}
return 0;
}【练习题】编写打印出一个单链表的所有元素的程序【链表】,布布扣,bubuko.com
标签:style class blog c code ext
原文地址:http://blog.csdn.net/chang_mu/article/details/26627091