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

#include<iostream> #include<assert.h> using namespace std;

template<class T> class LinearList { public: LinearList(); ~LinearList(); //创建一个空线性表 void Create(); //空表检查 bool IsEmpty() const; //返回表的长度(元素个数) int Length(); //寻找表中第k个元素,并把它保存到x中,如果不存在,则返回false bool Find(int, T&); //返回元素x在表中的位置,如果不在表中,则返回0 int Search(T&) const; //删除表中第k个元素,并把它保存到x中 void Delete(int,T&); //在第k个元素之后插入x,返回修改后的线性表 LinearList<T>&Insert(int, const T&); //把线性表放入输出流out中 //ostream& operator<<(ostream& out,const LinearList<T> & x){} private: int length; int MaxSize; T* Link; }; //构造函数 template<class T> LinearList<T>::LinearList() { //cout<<"请输入你的数据有多少:"<<endl; //cin>>MaxSize; this->MaxSize = MaxSize; Link = new T[MaxSize]; Length = 0; } //析构函数 template<class T> LinearList<T>::~LinearList() { delete [] Link; } //创建一个空线性表 template<class T> void LinearList<T>::Create() { Link = new T[MaxSize]; length = 0; } //空表检查 template<class T> bool LinearList<T>::IsEmpty() const { assert(Length < 0); return (0 != Length); } //抽数 template<class T> bool LinearList<T>::Find(int k,T& x) { // have no the findElement if(k<1 || k > Length) return false;

x = Link[k-1]; return true; } //查询 template<class T> int LinearList<T>::Search(T& x) const { for(int i = 0;i < length;i++) if(Link[i] == x) return ++i;

return -1; }

//删除 template<class T> void LinearList<T>::Delete(int k,T& x) { if(Find(k,x)) { for(int i = k;i<length;i++) Link[i-1] = Link[i];

length--; } }

//插入 template<class T> LinearList<T>& LinearList<T>::Insert(int k,const T& x ) { if(k<0||k>length) { cout<<"位置错误."<<endl; //return; 引发异常 }

if(length == MaxSize) { cout<<"数据已满,无法再接纳数据"<<endl; //return ; 引发异常 }

length++;

for(int i = length-1;i > k; i--) Link[i+1] = Link[i];

Link[k] = x; return *this; } template<class T> int LinearList<T>::Length() { return length; } int main() { LinearList<int> L(5); L.Create(); cout<<"长度:"<<L.Length()<<endl; cout<<"插入表:"<<endl; L.Insert(1,2); cout<<"插入后元素后的长度:"<<L.Length()<<endl; L.Search(2); return 0; }

搜索更多相关主题的帖子: 线性表 int bool 元素 LinearList 
2005-09-08 14:12
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
我可是每天给你回答一道问题啊,你可得付我工资啊!!!

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-08 14:45
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
得分:0 
我正要给你送锦旗呢啊哟,
   老斑竹能继往开来,再接再厉吗?
       我那问题出在哪里啊

2005-09-08 14:46
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
////////////////////////
//
//      try it :-)
//
////////////////////////
#include&lt;iostream&gt;
using namespace std;

template&lt;class T&gt;
class LinearList
{
private:
  int pos;
  int maxSize;
  T * link;
public:
  LinearList();
  LinearList(int maxSize);
  ~LinearList();
  //空表检查
  bool isEmpty() const;

  bool isFull() const;
  //返回表的长度(元素个数)
  int  length();
  //寻找表中位于index的元素,并把它保存到x中,如果不存在,则返回false
  bool find(int, T&amp;);
  //返回元素x在表中的位置,如果不在表中,则返回-1
  int  search(const T) const;
  //删除表中位于index的元素,并把它保存到x中
  void remove(int,T&amp;);

  LinearList&lt;T&gt; &amp; addElement(const T e);

  //将元素插入位于index的位置,返回修改后的线性表
  LinearList&lt;T&gt;&amp;insert(int, const T);
   
  //把线性表放入输出流out中
  friend ostream &amp; operator&lt;&lt;(ostream &amp; out,const LinearList&lt;T&gt; &amp; x);
};

//构造函数
template&lt;class T&gt;
LinearList&lt;T&gt;::LinearList()
{
  cout&lt;&lt;"请输入你的数据有多少:"&lt;&lt;endl;
  cin&gt;&gt;maxSize;
  link = new T[maxSize];
  pos = 0;
}

template&lt;class T&gt;
LinearList&lt;T&gt;::LinearList(int maxSize)
{
  this-&gt;maxSize = maxSize;
  link = new T[maxSize];
  pos = 0;
}

//析构函数
template&lt;class T&gt;
LinearList&lt;T&gt;::~LinearList()
{
  delete [] link;
  pos = 0;
}

//空表检查
template&lt;class T&gt;
bool LinearList&lt;T&gt;::isEmpty() const
{
  return (pos == 0);
}

template&lt;class T&gt;
bool LinearList&lt;T&gt;::isFull() const
{
  return (pos == maxSize);
}

//抽数
template&lt;class T&gt;
bool LinearList&lt;T&gt;::find(int index, T &amp; x)
{
  // have no the findElement
  if(index&lt;1 || index &gt;= pos)
    return false;
   
  x = link[index];
  return true;
}

//查询
template&lt;class T&gt;
int LinearList&lt;T&gt;::search(const T x) const
{
  if(isEmpty())
    return -1;
   
  for(int i = 0; i &lt; pos; i++)
  {   
    if(link[i] == x)
      return i;
  }
  return -1;
}

//删除
template&lt;class T&gt;
void LinearList&lt;T&gt;::remove(int index, T &amp; x)
{
  if(Find(index, x) &amp;&amp; index != pos-1)
  {
    for(int i = index+1; i&lt;pos; i++)
    {
      link[i-1] = link[i];
    }
    pos--;
  }
}

//addElement
template&lt;class T&gt;
LinearList&lt;T&gt;&amp; LinearList&lt;T&gt;::addElement(const T e)
{
  if(!isFull())
  {
    link[pos] = e;
    pos++;
  }
  return *this;
}

//插入
template&lt;class T&gt;
LinearList&lt;T&gt;&amp; LinearList&lt;T&gt;::insert(int index, const T x )
{
  if(index == pos &amp;&amp; !isFull())
  {
    addElement(x);
  }

  else if(index&lt;0 || index&gt;pos)
  {
    cout&lt;&lt;"位置错误."&lt;&lt;endl;
  }
   
  else if(isFull())
  {
    cout&lt;&lt;"数据已满,无法再接纳数据"&lt;&lt;endl;
  }
   
  else
  {
    for(int i = pos-1; i &gt; index; i--)
    {
      link[i+1] = link[i];
    }
    pos++;

    link[index] = x;
  }
  return *this;
}

template&lt;class T&gt;
int LinearList&lt;T&gt;::length()
{
  return pos;
}

template&lt;class T&gt;
ostream &amp; operator&lt;&lt;(ostream &amp; out,const LinearList&lt;T&gt; &amp; x)
{
  for(int i = 0; i&lt;x.pos; i++)
    out&lt;&lt;x.link[i]&lt;&lt;"  ";
  return out;
}

int main()
{
  LinearList&lt;int&gt; L(20);
  L.addElement(1);
  L.addElement(2);
  L.addElement(3);
  L.addElement(4);
  L.addElement(5);
  L.addElement(6);
  L.addElement(7);
  L.addElement(8);
  L.addElement(9);
  L.addElement(10);
  cout&lt;&lt;"长度:"&lt;&lt;L.length()&lt;&lt;endl;
  cout&lt;&lt;"插入表:"&lt;&lt;endl;
  L.insert(1,2);
  L.insert(0,2);
  cout&lt;&lt;"插入后元素后的长度:"&lt;&lt;L.length()&lt;&lt;endl;
  int pos = L.search(8);
  cout&lt;&lt;"Position: "&lt;&lt;pos&lt;&lt;endl;
  cout&lt;&lt;"元素:"&lt;&lt;L&lt;&lt;endl;
   
  return 0;   
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-08 16:34
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
得分:0 
感谢的5体头地

2005-09-08 18:07
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
得分:0 
我的不能通过编译,  指示错误在倒数第2行, (return 0;上面那句)

2005-09-08 18:16
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
你把4楼的程序copy 下来,再试试,我这边没问题。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-08 18:36
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
得分:0 
我仔细看了, 修改了你几处代码。
  详情看:  kai进来。。    那个帖子

2005-09-08 23:48
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

template&lt;class T&gt;
class LinearList
{
private:
  int pos;
  int maxSize;
  T * link;
public:
  LinearList();
  LinearList(int maxSize);
  ~LinearList();
  //空表检查
  bool isEmpty() const;

  bool isFull() const;
  //返回表的长度(元素个数)
  int  length();
  //寻找表中位于index的元素,并把它保存到x中,如果不存在,则返回false
  bool find(int, T&amp;);
  //返回元素x在表中的位置,如果不在表中,则返回-1
  int  search(const T) const;
  //删除表中位于index的元素,并把它保存到x中
  void remove(int,T&amp;);

  LinearList&lt;T&gt; &amp; addElement(const T e);

  //将元素插入位于index的位置,返回修改后的线性表
  LinearList&lt;T&gt;&amp;insert(int, const T);
   
  //把线性表放入输出流out中
  friend ostream &amp; operator&lt;&lt; (ostream &amp; out, const LinearList&lt;T&gt; &amp; x)
  {
    for(int i = 0; i&lt;x.pos; i++)
      out&lt;&lt;x.link[i]&lt;&lt;"  ";
    return out;
  }
};

//构造函数
template&lt;class T&gt;
LinearList&lt;T&gt;::LinearList()
{
  cout&lt;&lt;"请输入你的数据有多少:"&lt;&lt;endl;
  cin&gt;&gt;maxSize;
  link = new T[maxSize];
  pos = 0;
}

template&lt;class T&gt;
LinearList&lt;T&gt;::LinearList(int maxSize)
{
  this-&gt;maxSize = maxSize;
  link = new T[maxSize];
  pos = 0;
}

//析构函数
template&lt;class T&gt;
LinearList&lt;T&gt;::~LinearList()
{
  delete [] link;
  pos = 0;
}

//空表检查
template&lt;class T&gt;
bool LinearList&lt;T&gt;::isEmpty() const
{
  return (pos == 0);
}

template&lt;class T&gt;
bool LinearList&lt;T&gt;::isFull() const
{
  return (pos == maxSize);
}

//抽数
template&lt;class T&gt;
bool LinearList&lt;T&gt;::find(int index, T &amp; x)
{
  // have no the findElement
  if(index&lt;1 || index &gt;= pos)
    return false;
   
  x = link[index];
  return true;
}

//查询
template&lt;class T&gt;
int LinearList&lt;T&gt;::search(const T x) const
{
  if(isEmpty())
    return -1;
   
  for(int i = 0; i &lt; pos; i++)
  {   
    if(link[i] == x)
      return i;
  }
  return -1;
}

//删除
template&lt;class T&gt;
void LinearList&lt;T&gt;::remove(int index, T &amp; x)
{
  if(Find(index, x) &amp;&amp; index != pos-1)
  {
    for(int i = index+1; i&lt;pos; i++)
    {
      link[i-1] = link[i];
    }
    pos--;
  }
}

//addElement
template&lt;class T&gt;
LinearList&lt;T&gt;&amp; LinearList&lt;T&gt;::addElement(const T e)
{
  if(!isFull())
  {
    link[pos] = e;
    pos++;
  }
  return *this;
}

//插入
template&lt;class T&gt;
LinearList&lt;T&gt;&amp; LinearList&lt;T&gt;::insert(int index, const T x )
{
  if(index == pos &amp;&amp; !isFull())
  {
    addElement(x);
  }

  else if(index&lt;0 || index&gt;pos)
  {
    cout&lt;&lt;"位置错误."&lt;&lt;endl;
  }
   
  else if(isFull())
  {
    cout&lt;&lt;"数据已满,无法再接纳数据"&lt;&lt;endl;
  }
   
  else
  {
    for(int i = pos-1; i &gt; index; i--)
    {
      link[i+1] = link[i];
    }
    pos++;

    link[index] = x;
  }
  return *this;
}

template&lt;class T&gt;
int LinearList&lt;T&gt;::length()
{
  return pos;
}

int main()
{
  LinearList&lt;int&gt; L(20);
  L.addElement(1);
  L.addElement(2);
  L.addElement(3);
  L.addElement(4);
  L.addElement(5);
  L.addElement(6);
  L.addElement(7);
  L.addElement(8);
  L.addElement(9);
  L.addElement(10);
  cout&lt;&lt;"长度:"&lt;&lt;L.length()&lt;&lt;endl;
  cout&lt;&lt;"插入表:"&lt;&lt;endl;
  L.insert(1,2);
  L.insert(0,2);
  cout&lt;&lt;"插入后元素后的长度:"&lt;&lt;L.length()&lt;&lt;endl;
  int pos = L.search(8);
  cout&lt;&lt;"Position: "&lt;&lt;pos&lt;&lt;endl;
  cout&lt;&lt;"元素:"&lt;&lt;L&lt;&lt;endl;
  
  system("pause");  
  return 0;   
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-09 04:03
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

template&lt;class T&gt;
class LinearList;

template&lt;class T&gt;
ostream &amp; operator&lt;&lt;(ostream &amp; out, const LinearList &lt;T&gt; &amp; x);

template&lt;class T&gt;
class LinearList
{
private:
  int pos;
  int maxSize;
  T * link;
public:
  LinearList();
  LinearList(int maxSize);
  ~LinearList();
  //空表检查
  bool isEmpty() const;

  bool isFull() const;
  //返回表的长度(元素个数)
  int  length();
  //寻找表中位于index的元素,并把它保存到x中,如果不存在,则返回false
  bool find(int, T&amp;);
  //返回元素x在表中的位置,如果不在表中,则返回-1
  int  search(const T) const;
  //删除表中位于index的元素,并把它保存到x中
  void remove(int,T&amp;);

  LinearList&lt;T&gt; &amp; addElement(const T e);

  //将元素插入位于index的位置,返回修改后的线性表
  LinearList&lt;T&gt;&amp;insert(int, const T);
   
  //把线性表放入输出流out中
  friend ostream &amp; operator&lt;&lt; &lt;&gt;(ostream &amp; out,const LinearList&lt;T&gt; &amp; x);
};

//构造函数
template&lt;class T&gt;
LinearList&lt;T&gt;::LinearList()
{
  cout&lt;&lt;"请输入你的数据有多少:"&lt;&lt;endl;
  cin&gt;&gt;maxSize;
  link = new T[maxSize];
  pos = 0;
}

template&lt;class T&gt;
LinearList&lt;T&gt;::LinearList(int maxSize)
{
  this-&gt;maxSize = maxSize;
  link = new T[maxSize];
  pos = 0;
}

//析构函数
template&lt;class T&gt;
LinearList&lt;T&gt;::~LinearList()
{
  delete [] link;
  pos = 0;
}

//空表检查
template&lt;class T&gt;
bool LinearList&lt;T&gt;::isEmpty() const
{
  return (pos == 0);
}

template&lt;class T&gt;
bool LinearList&lt;T&gt;::isFull() const
{
  return (pos == maxSize);
}

//抽数
template&lt;class T&gt;
bool LinearList&lt;T&gt;::find(int index, T &amp; x)
{
  // have no the findElement
  if(index&lt;1 || index &gt;= pos)
    return false;
   
  x = link[index];
  return true;
}

//查询
template&lt;class T&gt;
int LinearList&lt;T&gt;::search(const T x) const
{
  if(isEmpty())
    return -1;
   
  for(int i = 0; i &lt; pos; i++)
  {   
    if(link[i] == x)
      return i;
  }
  return -1;
}

//删除
template&lt;class T&gt;
void LinearList&lt;T&gt;::remove(int index, T &amp; x)
{
  if(Find(index, x) &amp;&amp; index != pos-1)
  {
    for(int i = index+1; i&lt;pos; i++)
    {
      link[i-1] = link[i];
    }
    pos--;
  }
}

//addElement
template&lt;class T&gt;
LinearList&lt;T&gt;&amp; LinearList&lt;T&gt;::addElement(const T e)
{
  if(!isFull())
  {
    link[pos] = e;
    pos++;
  }
  return *this;
}

//插入
template&lt;class T&gt;
LinearList&lt;T&gt;&amp; LinearList&lt;T&gt;::insert(int index, const T x )
{
  if(index == pos &amp;&amp; !isFull())
  {
    addElement(x);
  }

  else if(index&lt;0 || index&gt;pos)
  {
    cout&lt;&lt;"位置错误."&lt;&lt;endl;
  }
   
  else if(isFull())
  {
    cout&lt;&lt;"数据已满,无法再接纳数据"&lt;&lt;endl;
  }
   
  else
  {
    for(int i = pos-1; i &gt; index; i--)
    {
      link[i+1] = link[i];
    }
    pos++;

    link[index] = x;
  }
  return *this;
}

template&lt;class T&gt;
int LinearList&lt;T&gt;::length()
{
  return pos;
}

template&lt;class T&gt;
ostream &amp; operator&lt;&lt;(ostream &amp; out,const LinearList&lt;T&gt; &amp; x)
{
  for(int i = 0; i&lt;x.pos; i++)
    out&lt;&lt;x.link[i]&lt;&lt;"  ";
  return out;
}

int main()
{
  LinearList&lt;int&gt; L(20);
  L.addElement(1);
  L.addElement(2);
  L.addElement(3);
  L.addElement(4);
  L.addElement(5);
  L.addElement(6);
  L.addElement(7);
  L.addElement(8);
  L.addElement(9);
  L.addElement(10);
  cout&lt;&lt;"长度:"&lt;&lt;L.length()&lt;&lt;endl;
  cout&lt;&lt;"插入表:"&lt;&lt;endl;
  L.insert(1,2);
  L.insert(0,2);
  cout&lt;&lt;"插入后元素后的长度:"&lt;&lt;L.length()&lt;&lt;endl;
  int pos = L.search(8);
  cout&lt;&lt;"Position: "&lt;&lt;pos&lt;&lt;endl;
  cout&lt;&lt;"元素:"&lt;&lt;L&lt;&lt;endl;
  
  system("pause");  
  return 0;   
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-09 04:16
快速回复:郁闷的线性表类
数据加载中...
 
   



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

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