输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
输入格式:
第一行输入整数的个数N(1 <= N <= 100000)。
第二行依次输入每个整数。
输出格式:
输出这组整数。
输入样例:
8
12 56 4 6 55 15 33 62
输出样例:
12 56 4 6 55 15 33 62
#include <stdio.h>
#include <stdlib.h>
typedef struct node{ // 使用typedef后,再次定义该类型结构体可以忽略struct的书写
int data;
struct node *next;
}node;
node *creat(int n) { // 顺序建表
node *head, *tail, *p;
head = (node*)malloc(sizeof(node));
head->next = NULL;
tail = head;
for ( int i = 0; i < n; i++) {
p = (node*)malloc(sizeof(node));
scanf("%d", &p->data);
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
void pri(node *p) { // 输出
while(p) {
if ( p->next == NULL )
printf("%d\n", p->data);
else
printf("%d ", p->data);
p = p->next;
}
}
int main() {
int n;
scanf("%d", &n);
node *head, *p;
head = creat(n);
pri(head->next);
return 0;
}