#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int score;
struct student *next;
}Linklist;
Linklist *creat(int n);//创造链表
void newprintf(Linklist *head);//输出链表
void Pfree(Linklist *head);//释放链表
int main()
{
int n=9;//n为链表长度
Linklist *head;
head=creat(n);
newprintf(head);
Pfree(head);
system("pause");//是暂停的意思,等待用户信号;不然控制台程序会一闪即过,你来不及看到执行结果。
return 0;
}
Linklist *creat(int n)
{
Linklist *head=NULL,*node,*end;
for(int i=0;i<n;i++)
{
node=(Linklist *)malloc(sizeof(Linklist));
scanf("%d",&node->score);
node->next=NULL;
if(head==NULL)
{
head=node;
head->next=NULL;
end=head;
}
else
{
end->next=node;
end=node;
}
}
end->next=NULL;
return head;
}
void newprintf(Linklist *head)
{
Linklist *p;
p=head;
if(p==NULL)
{
printf("此链表为空\n");
return;
}
while(p!=NULL)
{
printf("我是%d\n",p->score);
p=p->next;
}
}
void Pfree(Linklist *head)
{
Linklist *p;
p=head;
while(p!=NULL)
{
p=head->next;
free(head);
head=p;
}
}
c语言链表及输入输出释放函数
最新推荐文章于 2022-04-21 13:29:08 发布