70,023
社区成员




#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
struct node
{
int num;
struct node *next;
};
int main()
{
struct node *creat();
void print();
struct node *head;
head = NULL;
head = creat(head);
print(head);
return 0;
}
struct node *creat(struct node *head)
{
struct node *p1, *p2;
p1 = p2 = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p1 -> num);
p1 -> next = NULL;
while (p1 -> num != -1)
{
if (head == NULL)
head = p1;
else
p2 -> next = p1;
p2 = p1;
p1 = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p1 -> num);
//p1 -> next = NULL;//这一句网上那个例子里没有,自己加上去的。
}
p2 -> next = NULL;//p1 -> next = NULL;为什么放在这里不行呢?
return head;
}
void print(struct node *head)
{
struct node *temp;
temp = head;
while (temp != NULL)
{
printf("%3d", temp -> num);
temp = temp -> next;
}
}