数据结构实验之链表一:顺序建立链表
Time Limit: 1000 ms Memory Limit: 65536 KiB
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
1 #include<stdio.h> 2 #include<stdlib.h> 3 struct node 4 { 5 int data; 6 struct node*next; 7 }; 8 int main() 9 { 10 int n,i ; 11 struct node*head,*p,*tail; 12 head = (struct node*)malloc(sizeof(struct node)); 13 head->next = NULL; 14 tail = head; 15 scanf("%d",&n); 16 for(i = 0; i<n; i++) 17 { 18 p = (struct node*)malloc(sizeof(struct node)); 19 scanf("%d",&p->data); 20 p->next = NULL; 21 tail->next = p; 22 tail = p; 23 } 24 for(p = head->next; p!=NULL; p=p->next) 25 { 26 if(p->next==NULL) 27 printf("%d ",p->data); 28 else 29 printf("%d ",p->data); 30 } 31 return 0; 32 }