# 逆序建立链表(线性表)

（线性表)逆序建立一个链表，带头结点

#include <stdio.h>
#include <stdlib.h>
typedef struct number  //定义结构体,NUM为结构体变量名
{
int date;
struct number *before;//指向结点前驱的指针
}NUM;
{
NUM *p;
{
}
}
NUM *creat(int n)
{
scanf("%d",&p->date);
while(--n)  //尾插建表，同时记录每个结点的前驱
{
scanf("%d",&p->date);
/***********/

添加代码

/*************/    }
return q;
}
void play(NUM *h)
{
NUM *p;
for(p=h;p!=NULL;p=p->before)//从后往前倒着输出
printf("%d ",p->date);
destroy(p);
}
int main()
{
int n;
scanf("%d",&n);
play(creat(n));
return 0;
}

5 4 3 2 1

``````6
10 2 9 7 8 8``````

``8 8 7 9 2 10 ``

``````#include <stdio.h>
#include <stdlib.h>
typedef struct shu
{
int date;
int before;
}S;
S *creat(int n)
{
scanf("%d",&p->date);
while(--n)
{
p=malloc(sizeof(S));
scanf("%d",&p->date);
p->before=q;
q=p;
}
return q;
}
void play(S *h)
{
S *p;
for(p=h;p!=NULL;p=p->before)
printf("%d ",p->date);
}
int main()
{
int n;
scanf("%d",&n);
play(creat(n));
return 0;
}``````

``````#include <stdio.h>
#include <stdlib.h>
typedef struct shu
{
int date;
int before;
}S;
S *creat(int n)
{
scanf("%d",&p->date);
while(--n)
{
p=malloc(sizeof(S));
scanf("%d",&p->date);
p->before=q;
q=p;
}
return q;
}
void play(S *h)
{
S *p;
for(p=h;p!=NULL;p=p->before)
printf("%d ",p->date);
}
int main()
{
int n;
scanf("%d",&n);
play(creat(n));
return 0;
}``````