数据结构的问题(C++)

来源:百度知道 编辑:UC知道 时间:2024/05/16 13:52:43
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
class Lnode
{public:
Lnode *next;
char data;
void Creatlist(int n);
void Print(Lnode *h);
};
void Lnode::Creatlist(int n)//创建链表
{Lnode *p,*s,*h;
h=new Lnode;s=h;
h->next=NULL;
for(int i=0;i<n;i++)
{p=new Lnode;p->next=NULL;cin>>p->data;s->next=p;s=p;}
}
void Print(Lnode *h)//输出
{Lnode *p;p=h->next;
while(p!=NULL)
{cout<<p->data<<'\t';p=p->next;}
}
void main()
{Lnode *h,a;int n;
cout<<"length of the linklist: ";
cout<<"输入长度"<<'\t';cin>>n;
h=new Lnode;h->next=NULL;
a.Creatlist(n);
a.Print(h);
}

#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
class Lnode
{public:
Lnode *next;
char data;
Lnode * Creatlist(int n);
void Print(Lnode *h);
};
Lnode * Lnode::Creatlist(int n)//创建链表
{Lnode *p,*s,*h;
h=new Lnode;s=h;
h->next=NULL;
for(int i=0;i<n;i++)
{p=new Lnode;p->next=NULL;cin>>p->data;s->next=p;s=p;}
return h;//返回你创建的链头指针 链接到main中的附加头结点上面
}
void Lnode::Print(Lnode *h)//输出 <-这里有点小问题
{Lnode *p;p=h->next;
while(p!=NULL)
{cout<<p->data<<'\t';p=p->next;}
}
void main()
{Lnode *h,a;int n;
cout<<"length of the linklist: ";
cout<<"输入长度"<<'\t';cin>>n;
h=a.Creatlist(n);//这里创建链 明显悬空了 你创建的链怎么用呢?这样改
a.Print(h);
}