c程序中的NULL解决思路
c程序中的NULL
#include<stdio.h>
#include<malloc.h>
#define LIAN sizeof(struct lianbiao)
struct lianbiao
{
int num;
struct lianbiao *next;
struct lianbiao *before;
};
void inputlian(struct lianbiao*);//输入链表
void outputlianfromhead(struct lianbiao*);//输出链表
void outputlianfromend(struct lianbiao*);//输出链表
//void insertnum();//插入数字
//void deletenum();//删除数字
int total=0;
int main()
{
struct lianbiao *head,*end;
//l1=(struct lianbiao*)malloc(100*sizeof(struct lianbiao));
end=((struct lianbiao*)malloc(LIAN));
head=end;
head->before=NULL;
end->next=NULL;
inputlian(end);
outputlianfromhead(head);
outputlianfromend(end);
return 0;
}
//----------------------------------------------
void inputlian(struct lianbiao* l1)
{
int a;
struct lianbiao *l2;
scanf("%d",&a);
while(a!=0)
{
++total;
l1->num=a;
printf("---%d\n",l1->num);
l2=((struct lianbiao*)malloc(LIAN));
l1->next=l2;
l2->before=l1;
l1=l2;
scanf("%d",&a);
};
l1=NULL;
}
//----------------------------------------------
void outputlianfromend(struct lianbiao* e)
{
struct lianbiao* p;
p=e;
p=p->before;
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->before;
}
}
//----------------------------------------------
void outputlianfromhead(struct lianbiao* h)
{
struct lianbiao* p;
p=h;
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}//该程序输出链表时while(p!=NULL)好像没有用一样,当链表输出完时,指针接着往下指,然后结果就显示该内存不可读了,用的是vc++6.0编译器。。
------解决方案--------------------
指针作为函数参数的时候如果要返回值的话,单级指针是不行的。因为那只是指针的一份拷贝
#include<stdio.h>
#include<malloc.h>
#define LIAN sizeof(struct lianbiao)
struct lianbiao
{
int num;
struct lianbiao *next;
struct lianbiao *before;
};
void inputlian(struct lianbiao**);//输入链表
void outputlianfromhead(struct lianbiao*);//输出链表
void outputlianfromend(struct lianbiao*);//输出链表
//void insertnum();//插入数字
//void deletenum();//删除数字
int total=0;
int main()
{
struct lianbiao *head,*end;
//l1=(struct lianbiao*)malloc(100*sizeof(struct lianbiao));
end=((struct lianbiao*)malloc(LIAN));
head=end;
head->before=NULL;
end->next=NULL;
inputlian( &end);
outputlianfromhead(head);
outputlianfromend(end);
return 0;
}
//----------------------------------------------
void inputlian(struct lianbiao ** ppl1)
#include<stdio.h>
#include<malloc.h>
#define LIAN sizeof(struct lianbiao)
struct lianbiao
{
int num;
struct lianbiao *next;
struct lianbiao *before;
};
void inputlian(struct lianbiao*);//输入链表
void outputlianfromhead(struct lianbiao*);//输出链表
void outputlianfromend(struct lianbiao*);//输出链表
//void insertnum();//插入数字
//void deletenum();//删除数字
int total=0;
int main()
{
struct lianbiao *head,*end;
//l1=(struct lianbiao*)malloc(100*sizeof(struct lianbiao));
end=((struct lianbiao*)malloc(LIAN));
head=end;
head->before=NULL;
end->next=NULL;
inputlian(end);
outputlianfromhead(head);
outputlianfromend(end);
return 0;
}
//----------------------------------------------
void inputlian(struct lianbiao* l1)
{
int a;
struct lianbiao *l2;
scanf("%d",&a);
while(a!=0)
{
++total;
l1->num=a;
printf("---%d\n",l1->num);
l2=((struct lianbiao*)malloc(LIAN));
l1->next=l2;
l2->before=l1;
l1=l2;
scanf("%d",&a);
};
l1=NULL;
}
//----------------------------------------------
void outputlianfromend(struct lianbiao* e)
{
struct lianbiao* p;
p=e;
p=p->before;
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->before;
}
}
//----------------------------------------------
void outputlianfromhead(struct lianbiao* h)
{
struct lianbiao* p;
p=h;
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}//该程序输出链表时while(p!=NULL)好像没有用一样,当链表输出完时,指针接着往下指,然后结果就显示该内存不可读了,用的是vc++6.0编译器。。
C
链表
null
------解决方案--------------------
指针作为函数参数的时候如果要返回值的话,单级指针是不行的。因为那只是指针的一份拷贝
#include<stdio.h>
#include<malloc.h>
#define LIAN sizeof(struct lianbiao)
struct lianbiao
{
int num;
struct lianbiao *next;
struct lianbiao *before;
};
void inputlian(struct lianbiao**);//输入链表
void outputlianfromhead(struct lianbiao*);//输出链表
void outputlianfromend(struct lianbiao*);//输出链表
//void insertnum();//插入数字
//void deletenum();//删除数字
int total=0;
int main()
{
struct lianbiao *head,*end;
//l1=(struct lianbiao*)malloc(100*sizeof(struct lianbiao));
end=((struct lianbiao*)malloc(LIAN));
head=end;
head->before=NULL;
end->next=NULL;
inputlian( &end);
outputlianfromhead(head);
outputlianfromend(end);
return 0;
}
//----------------------------------------------
void inputlian(struct lianbiao ** ppl1)