64,637
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int date;
struct node *next;
};
struct node *creatlist()
{
struct node * h,*p,*q;
int a;
h=(struct node *)malloc(sizeof(struct node));
p=q=h;
scanf("%d",&a);
while(a!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->date=a;
q->next=p;
q=p;
scanf("%d",&a);
}
p->next=NULL;
return h;
}
void printlist(struct node*h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->date);
p=p->next;
}
printf("\n");
return;
}
void Del(struct node* h)
{
struct node *p, *q;
p = q = h;
while (p)
{
q = p->next;
free(p);
p = q;
}
}
void main()
{
struct node *head;
head=creatlist();
printlist(head);
Del(head);// 添加释放内存的函数
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int date;
struct node *next;
};
struct node *creatlist()
{
struct node * h,*p,*q;
int a;
h=(struct node *)malloc(sizeof(struct node));
p=q=h;
scanf("%d",&a);
while(a!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->date=a;
q->next=p;
q=p;
scanf("%d",&a);
}
p->next=NULL;
return h;
}
void printlist(struct node*h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->date);
p=p->next;
}
printf("\n");
return;
}
void main()
{
struct node *head;
head=creatlist();
printlist(head);
}