注册 登录
编程论坛 VC++/MFC

非循环单链表的输入输出重载

xiaoxun_1122 发布于 2011-04-30 16:38, 554 次点击
程序代码:
#include"LinkList.h"
template <typename ElemType>
class MyLinkList:public LinkList<ElemType>
{
public:
    MyLinkList userand(int num);
    void read(int k);
    void display(ostream& out) const;
    void ins(int i);
};
template <typename ElemType>
void MyLinkList<ElemType>::read(int k)
{
  int n,i;
  n=k;
  ElemType e;
  for(i=0;i<n;i++)
  {
  cin>>e;
  insert(i+1,e);
  }
  cout<<endl;
}

template <typename ElemType>
istream& operator >>(istream& in,MyLinkList<ElemType>&s)
{
    s.read(in);
    return in;
}

template <typename ElemType>
void MyLinkList<ElemType>::display(ostream& out) const   
{
    NodePointer p=head;
    if(!head)
    {
        out<<"该非循环单链表为空!"<<endl;
        return;
    }
    while(p)
    {
        out<<p->data<<"->";
        p=p->next;
    }
    out<<endl;
}
template <typename ElemType>
ostream& operator <<(ostream& out,const MyLinkList<ElemType>&oD)
{
    oD.display(out);
    return out;
}

template<typename ElemType>      //生成随机链表
void MyLinkList<ElemType>::ins(int k)
{
    int n=k,i;
    ElemType e=0;
    srand((int)time(0));
    cout<<"用如下随机数产生非循环单链表:"<<endl;
    for(i=0;i<n;i++){
        e=rand()%100;
        cout<<" "<<e;
        insert(i+1,e);
    }
    cout<<endl;
}
输出重载有问题!!!
0 回复
1