Problem Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
第二行依次输入每个整数。
Output
输出这组整数。
Sample Input
8 12 56 4 6 55 15 33 62
Sample Output
12 56 4 6 55 15 33 62
Hint
不得使用数组!
Source
01 |
#include<stdio.h> |
02 |
#include
<stdlib.h> |
03 |
struct node |
04 |
{ |
05 |
int data; |
06 |
struct node
*next; |
07 |
}; |
08 |
int main
() |
09 |
{ |
10 |
struct node
*head , * p ,*q; |
11 |
head
=( struct node*) malloc ( sizeof ( struct node
)); |
12 |
head
->next = NULL; |
13 |
q=head; |
14 |
int n; |
15 |
scanf ( "%d" ,
&n); |
16 |
while (n--) |
17 |
{ |
18 |
p
= ( struct node*) malloc ( sizeof ( struct node
)); |
19 |
scanf ( "%d" ,
&p->data); |
20 |
p
-> next = NULL; |
21 |
q->next
= p; |
22 |
q=p; |
23 |
} |
24 |
q
= head -> next; |
25 |
while (q!=
NULL) |
26 |
{ |
27 |
printf ( "%d" ,
q->data); |
28 |
if (q->next!=NULL) |
29 |
printf ( "
" ); |
30 |
else |
31 |
printf ( "\n" ); |
32 |
q=q->next; |
33 |
} |
34 |
return 0; |
35 |
} |
36 |