首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

链表创建问提(!希望大家帮帮忙

2012-02-12 
链表创建问提(在线等!!!!!!!!!希望大家帮帮忙!#includeiostreamusing namespace std#define len sizeof

链表创建问提(在线等!!!!!!!!!希望大家帮帮忙!
#include<iostream>
using namespace std;
#define len sizeof(node)
struct node
{
int data;
node *next;

};
node *creatlist()
{
node *head=NULL;
node *last;//表尾指针
node *p; //新建结点指针
int n=0;
cin>>p->data;
  p=new node[len];
  while(p->data!=0)
{
n=n+1;
if(n==1) { head=last;last->next=NULL;}
else
{
last->next=p;
p->next=NULL;
// last=p;
}


}
return head;
}

void showlist(node *head)
{

while(head)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}

void main()
{
node *head;
head=creatlist();
showlist(head);
}
这个程序怎么运行不出结果,系统提示:建立链表.exe 遇到问题需要关闭


[解决办法]
问题出在这里:留意红色部份
node *creatlist()
{
node *head=NULL;
node *last;//表尾指针
node *p; //新建结点指针
int n=0;
cin > >p- >data;
p=new node[len];
while(p- >data!=0)
{
n=n+1;
if(n==1) { head=last;last- >next=NULL;}
else
{
last- >next=p;
p- >next=NULL;
 // last=p;
}
刚开始时p并没有指向任何对象,但你却cin>>p->data,请问,程序会将你的输入存放在哪里
即使你修改过来了,你仍然得不到一个链表因为while(p- >data!=0),当你输入非零的时候死循环,输入零则不执行循环体内的代码
即使你又修改过来,你还是得不到一个链表。
问题出现在你的creatlist函数

热点排行