求助 关系c语言编程

来源:百度知道 编辑:UC知道 时间:2024/06/24 01:57:22
创建一个含有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;
HEAD hd;
printf("n=");
scanf("%d",&n);
hd.count=n;
hd.head=creat(n);
prt(hd.head);
hd.head=FREE(hd.head);
hd.count=0;
return 0