救命啊,单链表头节点问题

来源:百度知道 编辑:UC知道 时间:2024/06/27 14:14:44
我用书上面的方法建立单链表
同学说用这种方法La,Lb分别是这两个链表的头节点
之后我把La,Lb当头节点用
可是问题几乎全出在这两个头节点中
到底头节点应该用什么表示?

#define Flag 0
LL Creat_LL(){
LL La;
LN *s;
int x,i;
i=1;
La=NULL;
cout<<"多项式a的输入,请输入常数项的值"<<endl;
cin>>x;
while(x!=Flag){
s=new LN;
s->data=x;
s->next=La;
La=s;
cout<<"输入第"<<i<<"次方未知数的序数,如完成请输入Flag结束"<<endl;
i++;
}
return La;
}
#define Flag 0
LL Creat_LL2(){
LL Lb;
LN *t;
int y,j;
j=1;
Lb=NULL;
cout<<"多项式a的输入,请输入常数项的值"<<endl;
cin>>y;
while(y!=Flag){
t=new LN;
t->data=y;
t->next=Lb;
Lb=t;
cout<<"输入第"<<j<<"次方未知数的序数,如完成请输入Flag结束"<<endl;
j++;
}
return Lb;
}
int Leng_LL(LL L){
LN *p1;

你没有给节点申请空间当然不行了

看看我这个程序吧,借鉴一下:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct int_type)
struct int_type *addp(int,struct int_type *);
struct int_type
{
int i;
struct int_type *next;
}
main(){
int j,n,q;
struct int_type *p,*head;
scanf("%d",&n);
head=NULL;
for(j=0;j<n;j++){
scanf("%d",&q);
head=addp(q,head);
}
/* 下面是输出结果,表头就是head */
p=head;
while(p!=NULL)
{
printf("%d ",p->i);
p=p->next;
}
}
struct int_type *addp(int k,struct int_type *h){
struct int_type *p1;
if((h->i)>k || h==NULL){
p1=(struct int_type *)malloc(LEN);
p1->i=k;
p1->next=h;
h=p1;
}
else{
h->next=addp(k,h->next);
}
return h;
}