| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 655 人关注过本帖
标题:kai进来,你未完的讨论题
只看楼主 加入收藏
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
 问题点数:0 回复次数:0 
kai进来,你未完的讨论题

#include<iostream> using namespace std;

template<class T> class LinearList { public: LinearList(){} LinearList(int MaxSize); ~LinearList(); //创建一个空线性表 void Create(); bool isEmpty() const; //空表检查 bool isFull() const; //满表检查 LinearList<T> & addElement(const T e);//增加元素 int Length();//返回表的长度(元素个数) bool Find(int, T&); //寻找表中第k个元素,并把它保存到x中,如果不存在,则返回false int Search(const T x) const;//返回元素x在表中的位置,如果不在表中,则返回0 void Delete(int,T&);//删除表中第k个元素,并把它保存到x中 LinearList<T>&Insert(int, const T&);//在第k个元素之后插入x,返回修改后的线性表 void Output(ostream&) const; friend ostream& operator <<(ostream&, const LinearList<T>&); private: int MaxSize; int pos; T* link; }; //构造函数2 template<class T> LinearList<T>::LinearList(int MaxSize) { //cout<<"请输入你的数据有多少:"<<endl; //cin>>MaxSize; this->MaxSize = MaxSize; link = new T[MaxSize]; pos = 0; } //析构函数 template<class T> LinearList<T>::~LinearList() { delete [] link; } //空表检查 template<class T> bool LinearList<T>::isEmpty() const {

return pos == 0; } //满表检查 template<class T> bool LinearList<T>::isFull() const { return (pos == MaxSize); } //抽数 template<class T> bool LinearList<T>::Find(int index,T& x) {//寻找表中位于index的元素,并把它保存到x中,如果不存在,则返回false // have no the findElement if(index<1 || index >= pos) return false;

x = link[index-1]; return true; } //查询 template<class T> int LinearList<T>::Search(const T x) const { if(isEmpty()) return -1; for(int i = 0;i < pos;i++) if(link[i] == x) return ++i;

return -1; } //删除 template<class T> void LinearList<T>::Delete(int index,T& x) { if(Find(index,x) && index != pos - 1) { for(int i = index+1;i<pos;i++) { link[i-1] = link[i]; } pos--; } } //增加元素 template<class T> LinearList<T>& LinearList<T>::addElement(const T e) { if(!isFull()) { link[pos] = e; pos++; } return *this; } //插入 template<class T> LinearList<T>& LinearList<T>::Insert(int index,const T& x ) { if(index == pos && !isFull()) { addElement(x); } else if(index<0 || index>pos) { cout<<"数据已满,无法再接纳数据"<<endl; //return ; 引发异常 } else if(isFull()) { cout<<"数据已满,无法再接纳数据"<<endl; } else { for(int i = pos-1;i > index; i--) { link[i+1] = link[i]; } pos ++; link[index] = x; } return *this; } // template<class T> int LinearList<T>::Length() { return pos; }

template<class T> void LinearList<T>::Output(ostream& out) const { for(int i = 0; i <pos; i++) { out<<link[i]<<endl; } } //重载<< template<class T> ostream& operator <<(ostream & osObject,const LinearList<T>& x) { x.Output(osObject); return osObject; }

int main() { LinearList<int> L(10); L.addElement(1); L.addElement(2); L.addElement(3); L.addElement(4); L.addElement(5); cout<<"长度:"<<L.Length()<<endl; cout<<"插入表:"<<endl; L.Insert(1,8); L.Insert(0,9); int poss = L.Search(4); cout<<"插入后元素后的长度:"<<L.Length()<<endl; cout<<"查找元素的位置是: "<<poss<<endl; cout<<"元素:"; cout<<L; ////// 重载 return 0; } 问题1:我对你的代码稍微作了修改 2:除了重载<<,其他功能我测试过,绝对没问题 3:模板的影响,重载有点不同(用了成员函数调用友元重载函数) 4:这个好像能在VC++6.0上通过编译 5:cout<<L; ,倒数第2句出问题,在DEV,C-FREE中。 6:用了模板,重载<<有什么不同吗? 7:如果改,才能在标准C++中运行? //注释: 郁闷了N久的问题

搜索更多相关主题的帖子: kai 
2005-09-08 23:47
快速回复:kai进来,你未完的讨论题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018509 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved