70,018
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#define elemtype int
typedef struct Node
{
elemtype data;
struct Node *next;
}Node;
int main(void)
{
Node *head, *p1, *p2; //一直没明白lz用p3做什么
p1 = (Node *)malloc(sizeof(Node));
if (p1 == NULL)
{
printf("failure in memory.\n");
exit(0);
}
scanf("%d",&p1->data);
p1->next = NULL;
if (p1->data == 0)
{
printf("List is empty.\n");
free(p1); // 你在退出前要释放空间
return 0;
}
head = p1;
while (p1->data != 0)
{
if (!(p2 = (Node *)malloc(sizeof(Node))))
{
printf("failure in memory.\n");
exit(0);
}
scanf("%d",&p2->data);
getchar();
p2->next = NULL;
p1->next = p2;
p1 = p1->next;
}
printf("List is made successfully.\n");
while (head!=NULL)
{
p2 = head;
head = head->next;
free(p2);
}
return 0;
}