求大神调试 代码 谢谢着急中(代码目的是将string型按char型插入到链表中实现 树型存储。但是没有输出结果求点拨。
#include <iostream>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
const int size=100;
typedef struct node{
char a;
struct node*next;}list,*qlist;
class tree{
qlist head;
qlist flag;
public:
tree();
void inserter_tree(string&name);
void show_tree();
};
tree::tree(){qlist temp=new list;
temp->a='#';
head->next=temp;
temp->next=NULL;
flag=temp;
}
void tree::inserter_tree(string&name)
{
qlist flag1=flag;int j=name.size();
for(int i=0;i<j;i++)
{
qlist temp=new list;
temp->a=name[i];
temp->next=flag1->next;
flag1->next=temp;
flag1=flag1->next;
}
flag=flag->next;
}
void tree::show_tree()
{
qlist temp=head;
while(temp)
{
cout<<"show the tree:"<<cout<<temp->a<<'\t';
temp=temp->next;
}
cout<<endl;
}
int main()
{
tree ll;
ll.show_tree();
string hah="xiaojiba";
ll.inserter_tree(hah);
ll.show_tree();
return 0;
}
[解决办法]
构造函数应该是这样的。
tree::tree()
{
qlist temp=new list;
temp->a='#';
head=temp;
temp->next=NULL;
flag=temp;
}