还是c语言中空间的申请问题,线性链表
程序代码:[code]
#include <stdio.h>
#include <malloc.h>
typedef int DATATYPE ;
#define MAC (linklist )malloc(sizeof(Lnode))
typedef struct node
{
DATATYPE data;
struct Lnode *next;
} Lnode,*linklist;
linklist Creat()
{
int i_count=0;
int i_temp;
linklist p_head,p_st1,p_st2;
p_head=MAC; //MAC为预定义开辟空间
p_head->next=NULL;
p_st1=p_head;
printf("请输入数据,以-1结束\n");
scanf("%d",&i_temp);
while (i_temp!=-1)
{
if (p_head->next==NULL)
p_head->next=p_st1;
p_st2=MAC; //为什么p_st2要开辟空间而p_st1可以不开辟空间赋值?
p_st2->data=i_temp;
p_st1->next=p_st2; //这为什么可不开辟而直接存?疑问
p_st1=p_st2;
i_count++; //数组数字个数统计。。尾插法建立链表
scanf("%d",&i_temp);
}
p_head->data=i_count;
p_st1->next=NULL;
return p_head;
就是对那个p_st1那的疑问,空间为什么不开辟就可以用,而p_st2却必须先开辟后使用?达人呢
[/code]









