写一个算法.逆序建立一个带头字节点的单链表

来源:百度知道 编辑:UC知道 时间:2024/06/24 04:24:51
节点的结构给出了
struct Node{
DdtaType data;
struct Node *next;
};

struct Node *create(int n)//逆序创建带头节点单链表,n为链表中元素个数
{
struct Node *L;//L为表头
L=(struct Node*)malloc(sizeof(struct Node));
L->next=NULL;
for(int i=0;i<n;i++)
{
DataType a;
scanf(a);//输入a的值
struct Node *p;
p=(struct Node*)malloc(sizeof(struct Node));
p->data=a;
p->next=L->next;//将生成的节点插入到头节点后面
L->next=p;
}
return L;//返回表头
}