#include <iostream>
using namespace std;
template<class T>
struct ChainNode
{//数据成员
T number;
ChainNode *next;
};
template<class T>
class Chain
{
public://构造函数
Chain(int l=0);
//fangfa
void creatchain(){head=new ChainNode<T>;}
void cinchainnode();//向链表中输入数据
void dispaly()************************************************是这个函数对不么?
{
ChainNode<T> *k=head;
for(;k!=NULL;k=k->next)
cout<<k->number<<” “;
}****************************************************************
//void get(const T&number);
private:
ChainNode<T> *head;//存放头指针
int listsize;
};
template<class T>
Chain<T>::Chain(int l)
{
listsize=l;
head=NULL;
}
template<class T>
void Chain<T>::cinchainnode()//向链表中输入数据
{
ChainNode<T> *p;
head=new ChainNode<T>;
p=head;
//cout<<“head=”<<head<<endl;
for(int n=0;n<listsize;n++)
{
//listsize++;
cin>>p->number;
//cout<<p->number<<” “;
if(listsize-1!=n)
{
p->next=new ChainNode<T>;
//cout<<“next=”<<p->next<<endl;
}
else
p->next=NULL;
}
}
template<class T>
istream& operator>>(istream &input,Chain<T> &t)
{
t.cinchainnode();
return input;
}
template<class T>
ostream& operator<<(ostream &output,Chain<T> &t)
{
t.dispaly();
return output;
}
int main()
{
Chain<int> a(4);
cin>>a;
cout<<a;
return 0;
}
using namespace std;
template<class T>
struct ChainNode
{//数据成员
T number;
ChainNode *next;
};
template<class T>
class Chain
{
public://构造函数
Chain(int l=0);
//fangfa
void creatchain(){head=new ChainNode<T>;}
void cinchainnode();//向链表中输入数据
void dispaly()************************************************是这个函数对不么?
{
ChainNode<T> *k=head;
for(;k!=NULL;k=k->next)
cout<<k->number<<” “;
}****************************************************************
//void get(const T&number);
private:
ChainNode<T> *head;//存放头指针
int listsize;
};
template<class T>
Chain<T>::Chain(int l)
{
listsize=l;
head=NULL;
}
template<class T>
void Chain<T>::cinchainnode()//向链表中输入数据
{
ChainNode<T> *p;
head=new ChainNode<T>;
p=head;
//cout<<“head=”<<head<<endl;
for(int n=0;n<listsize;n++)
{
//listsize++;
cin>>p->number;
//cout<<p->number<<” “;
if(listsize-1!=n)
{
p->next=new ChainNode<T>;
//cout<<“next=”<<p->next<<endl;
}
else
p->next=NULL;
}
}
template<class T>
istream& operator>>(istream &input,Chain<T> &t)
{
t.cinchainnode();
return input;
}
template<class T>
ostream& operator<<(ostream &output,Chain<T> &t)
{
t.dispaly();
return output;
}
int main()
{
Chain<int> a(4);
cin>>a;
cout<<a;
return 0;
}
解决方案:20分
加一句 p = p->next;
#include <iostream> using namespace std; template<class T> struct ChainNode {//数据成员 T number; ChainNode *next; }; template<class T> class Chain { public://构造函数 Chain(int l=0); //fangfa void creatchain(){head=new ChainNode<T>;} void cinchainnode();//向链表中输入数据 void dispaly() { ChainNode<T> *k=head; for(;k!=NULL;k=k->next) cout<<k->number<<" "; } //void get(const T&number); private: ChainNode<T> *head;//存放头指针 int listsize; }; template<class T> Chain<T>::Chain(int l) { listsize=l; head=NULL; } template<class T> void Chain<T>::cinchainnode()//向链表中输入数据 { ChainNode<T> *p; head=new ChainNode<T>; p=head; //cout<<"head="<<head<<endl; for(int n=0;n<listsize;n++) { //listsize++; cin>>p->number; //cout<<p->number<<" "; if(listsize-1!=n) { p->next=new ChainNode<T>; p = p->next; //cout<<"next="<<p->next<<endl; } else p->next=NULL; } } template<class T> istream& operator>>(istream &input,Chain<T> &t) { t.cinchainnode(); return input; } template<class T> ostream& operator<<(ostream &output,Chain<T> &t) { t.dispaly(); return output; } int main() { Chain<int> a(4); cin>>a; cout<<a; return 0; }