注册 登录
编程论坛 C++教室

新手关于链表的建立,请看一下问题在哪?

ysrm 发布于 2007-09-27 10:40, 506 次点击

struct linknode
{char *data;
int oldn;
int newn;
struct linknode *next;
};

linknode *setnode()
{linknode *head,*p1,*p2;
int n=0;
p1=p2=new linknode;
head=NULL;
cout<<"input name: "<<endl;
cin>>p1->data;
int i=1;
while(p1->data!=NULL)
{
n++;
p1->oldn=i++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new linknode;
cin>>p1->data;
}
p2->next=NULL;
delete p1;
return(head);
}
void print(linknode *head)
{linknode *p;
cout<<"\nthe record is: "<<endl;
p=head;
while(p->data!=NULL)
{cout<<p->data<<endl;
p=p->next;
}
}
int main()
{linknode *head;
head=setnode();
print(head);
return 0;
}
逻辑上应该没问题,如果把结构体中的data,改int data,就可以运行了。我用的是字符指针,错在哪呢?大家帮帮忙,在线等

8 回复
#2
aipb20072007-09-27 11:12
char *str;
cin >> str;


这是错的。
#3
felicia2007-09-27 11:14
空间没有分配
#4
aipb20072007-09-27 11:15
LS 马甲?
#5
felicia2007-09-27 11:17
啥??什么呀??
#6
aipb20072007-09-27 11:24

跟我装,我一点你QQ不就知道了,呵呵~~~~~~~~```

#7
felicia2007-09-27 11:27
楼上看短消息啊
#8
ysrm2007-09-27 11:29
多谢,我再看看
#9
ysrm2007-09-27 11:33
那我改成把结构体中定义为char data[20],怎么也不行呢
1