尾插法建立一个大小为n的链表并换行输出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct Node {
int num;
struct Node *next;
}LNode;
void creat (LNode *H, int n) {
LNode *p, *r;
int x, i;
r = H;
for (i = 0; i < n; i ++) {
scanf("%d", &x);
p = (LNode *)malloc(sizeof(LNode));
p->num = x;
r->next = p;
r = p;
}
r->next = NULL;
}
void output(LNode *l) {
LNode *p = l->next;
while (p != NULL) {
printf("%d ", p->num);
p = p->next;
}
}
int main () {
int n;
scanf("%d", &n);
LNode *Head;
Head = (LNode *)malloc(sizeof(LNode));
creat (Head, n);
output(Head);
return 0;
}