求一个c语言程序

来源:百度知道 编辑:UC知道 时间:2024/06/18 07:58:27
创建一个含有n个元素的线性链表,并且带头结点

#include <stdlib.h>
#include <stdio.h>

typedef struct ln{
int data;
struct ln *next;
} node;

typedef struct{
node *head;
int count;
} HEAD;

node *creat(int n)/*创建含有n个结点的链表*/
{
node *a=NULL;
if (n){
a=malloc(sizeof(node));
scanf("%d",&(a->data));
a->next=creat(n-1);
}
return a;
}

void prt(node *a)/*输出链表的内容*/
{
while (a!=NULL)
{
printf("%d\n",a->data);
a=a->next;
}
}

node *FREE(node *a)/*释放链表占用的空间*/
{
if (a!=NULL)
{
FREE(a->next);
free(a);
}
return a;
}
int main(void)
{
int n=0;