楼主: fantuanxiaot
22589 85

[编程语言] [C++改编]基于VC++6.0和VS2012的线性表实现 [推广有奖]

回帖奖励 363 个论坛币 回复本帖可获得 3 个论坛币奖励! 每人限 2 次

Ψ▄┳一大卫卍卐席尔瓦

大师

8%

还不是VIP/贵宾

-

威望
7
论坛币
-234475 个
通用积分
124.0224
学术水平
3783 点
热心指数
3819 点
信用等级
3454 点
经验
150207 点
帖子
7546
精华
32
在线时间
1327 小时
注册时间
2013-2-3
最后登录
2022-2-24

初级学术勋章 初级热心勋章 中级热心勋章 中级学术勋章 初级信用勋章 中级信用勋章 高级热心勋章 高级学术勋章 特级学术勋章 特级热心勋章 高级信用勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Visual C++ 6.0代码和Visual Studio 2012的代码稍微有些不同。
Visual C++ 6.0代码:

本帖隐藏的内容

  1. //  线性表
  2. //  线性表
  3. //  by fantuanxiaot
  4. #include <iostream>
  5. #include <cmath>
  6. #include <iomanip>
  7. #include <cstdlib>
  8. #include <vector>
  9. #include <string>
  10. using namespace std;
  11. //  member function definition looks like a ctor,
  12. //  but name does not match enclosing class
  13. //  这个错误是因为除构造函数外所有的类成员函数都要有返回值的类型,
  14. //  如果没有返回值,就在函数定义前面加上void,这个编译错误就可以解决
  15. //  ////////////////////////////////////////////////////////////////////////
  16. //  Assert函数
  17. void Assert(bool val,string s)
  18. {
  19.         if(!val)
  20.         {
  21.                 cout<<"Asserted Failed:"<<s<<endl;
  22.                 exit(1);
  23.         }
  24. }
  25. //  ////////////////////////////////////////////////////////////////////////
  26. //  定义一个List的虚拟类
  27. template<class E>
  28. class List
  29. {
  30. private:
  31.         void operator=(const List&){}
  32.         List(const List&){}
  33. public:
  34.         //  constuctor and destructor
  35.         List(){}
  36.         virtual ~List(){}
  37.         //  一些基本的成员函数
  38.         virtual void clear()=0;
  39.         virtual void insert(const E& item)=0;
  40.         virtual void append(const E& item)=0;
  41.         virtual E remove()=0;
  42.         virtual void moveToStart()=0;
  43.         virtual void moveToEnd()=0;
  44.         virtual void prev()=0;
  45.         virtual void next()=0;
  46.         //  常成员函数
  47.         virtual int length() const=0;
  48.         virtual int currPos() const=0;
  49.         virtual void moveToPos(int pos)=0;
  50.         virtual const E& getValue() const=0;
  51. };
  52. //  ////////////////////////////////////////////////////////////////////////
  53. //  ////////////////////////////////////////////////////////////////////////
  54. //  线性表的制作
  55. template<class E>
  56. class AList:public List<E>
  57. {
  58. private:
  59.         int maxSize;
  60.         int listSize;
  61.         //  postion of current elements
  62.         int curr;
  63.         E* listArray;
  64. public:
  65.         //  构造函数
  66.         //  这里的defaultSize必须定义
  67.         AList(int size=100)
  68.         {
  69.                 maxSize=size;
  70.                 listSize=curr=0;
  71.                 //  创建一个新的指针
  72.                 listArray=new E[maxSize];
  73.         }
  74.         //  析构函数
  75.         ~AList(){delete[] listArray;}
  76.         void clear()
  77.         {
  78.                 delete[] listArray;
  79.                 listSize=curr=0;
  80.                 listArray=new E[maxSize];
  81.         }
  82.         //  在curr的位置内插入一个元素
  83.         void insert(const E&it)
  84.         {
  85.                 Assert(listSize<maxSize,"List Capacity Exceeded");
  86.                 for(int i=listSize;i>curr;i--)
  87.                 {
  88.                         listArray[i]=listArray[i-1];
  89.                 }
  90.                 listArray[curr]=it;
  91.                 listSize++;
  92.         }
  93.         //  在表的末尾加入一个元素
  94.         void append(const E&it)
  95.         {
  96.                 Assert(listSize<maxSize,"List Capacity Exceeded");
  97.                 listArray[listSize++]=it;
  98.         }
  99.         //  移动当指向的元素
  100.         E remove()
  101.         {
  102.                 Assert((curr>=0)&&(curr<listSize),"No Current elements");
  103.                 E it=listArray[curr];
  104.                 for(int i=curr;i<=listSize-1;i++)
  105.                         listArray[i]=listArray[i+1];
  106.                 listSize--;
  107.                 //  返回排除的元素
  108.                 return it;
  109.         }
  110.         //  返回开始
  111.         void moveToStart()
  112.         {
  113.                 curr=0;
  114.         }
  115.         //  返回结尾
  116.         void moveToEnd()
  117.         {
  118.                 curr=listSize;
  119.         }
  120.         //  Back up
  121.         void prev()
  122.         {
  123.                 if(curr!=0) curr--;
  124.         }
  125.         //  Next size
  126.         void next()
  127.         {
  128.                 if(curr<listSize) curr++;
  129.         }
  130.         //  return the list size
  131.         int length() const
  132.         {
  133.                 return listSize;
  134.         }
  135.         int currPos() const
  136.         {
  137.                 return curr;
  138.         }
  139.         //  来到当前的位置
  140.         void moveToPos(int pos)
  141.         {
  142.                 Assert((pos>=0)&&(pos<=listSize),"Pos Out of range");
  143.                 curr=pos;
  144.         }
  145.         //  return current elements
  146.         const E& getValue() const
  147.         {
  148.                 Assert((curr>=0)&&(curr<listSize),"No Current elements");
  149.                 return listArray[curr];
  150.         }
  151. };
  152. //  ////////////////////////////////////////////////////////////////////////
  153. //  ////////////////////////////////////////////////////////////////////////
  154. //  主函数
  155. int main()
  156. {
  157.         AList<int> list1(100);
  158.         for(int i=0;i<=50;i++)
  159.         {
  160.                 list1.append(i+1);
  161.         }
  162.         for(i=0;i<=50;i++)
  163.         {

  164.                 cout<<list1.getValue()<<" ";
  165.                 list1.next();
  166.                 if(i%5==4) cout<<endl;
  167.         }
  168.         return 0;
  169. }
  170. //  ////////////////////////////////////////////////////////////////////////
  171. //  ////////////////////////////////////////////////////////////////////////
复制代码



Visual Studio 2012的代码:

本帖隐藏的内容

  1. //  线性表
  2. //  线性表
  3. //  by fantuanxiaot
  4. #include <iostream>
  5. #include <cmath>
  6. #include <iomanip>
  7. #include <cstdlib>
  8. #include <vector>
  9. #include <string>
  10. using namespace std;
  11. //  member function definition looks like a ctor,
  12. //  but name does not match enclosing class
  13. //  这个错误是因为除构造函数外所有的类成员函数都要有返回值的类型,
  14. //  如果没有返回值,就在函数定义前面加上void,这个编译错误就可以解决
  15. //  ////////////////////////////////////////////////////////////////////////
  16. //  Assert函数
  17. void Assert(bool val,string s)
  18. {
  19.         if(!val)
  20.         {
  21.                 cout<<"Asserted Failed:"<<s<<endl;
  22.                 exit(1);
  23.         }
  24. }
  25. //  ////////////////////////////////////////////////////////////////////////
  26. //  定义一个List的虚拟类
  27. template<class E>
  28. class List
  29. {
  30. private:
  31.         void operator=(const List&){}
  32.         List(const List&){}
  33. public:
  34.         //  constuctor and destructor
  35.         List(){}
  36.         virtual ~List(){}
  37.         //  一些基本的成员函数
  38.         virtual void clear()=0;
  39.         virtual void insert(const E& item)=0;
  40.         virtual void append(const E& item)=0;
  41.         virtual E remove()=0;
  42.         virtual void moveToStart()=0;
  43.         virtual void moveToEnd()=0;
  44.         virtual void prev()=0;
  45.         virtual void next()=0;
  46.         //  常成员函数
  47.         virtual int length() const=0;
  48.         virtual int currPos() const=0;
  49.         virtual void moveToPos(int pos)=0;
  50.         virtual const E& getValue() const=0;
  51. };
  52. //  ////////////////////////////////////////////////////////////////////////
  53. //  ////////////////////////////////////////////////////////////////////////
  54. //  线性表的制作
  55. template<class E>
  56. class AList:public List<E>
  57. {
  58. private:
  59.         int maxSize;
  60.         int listSize;
  61.         //  postion of current elements
  62.         int curr;
  63.         E* listArray;
  64. public:
  65.         //  构造函数
  66.         //  这里的defaultSize必须定义
  67.         AList(int size=100)
  68.         {
  69.                 maxSize=size;
  70.                 listSize=curr=0;
  71.                 //  创建一个新的指针
  72.                 listArray=new E[maxSize];
  73.         }
  74.         //  析构函数
  75.         ~AList(){delete[] listArray;}
  76.         void clear()
  77.         {
  78.                 delete[] listArray;
  79.                 listSize=curr=0;
  80.                 listArray=new E[maxSize];
  81.         }
  82.         //  在curr的位置内插入一个元素
  83.         void insert(const E&it)
  84.         {
  85.                 Assert(listSize<maxSize,"List Capacity Exceeded");
  86.                 for(int i=listSize;i>curr;i--)
  87.                 {
  88.                         listArray[i]=listArray[i-1];
  89.                 }
  90.                 listArray[curr]=it;
  91.                 listSize++;
  92.         }
  93.         //  在表的末尾加入一个元素
  94.         void append(const E&it)
  95.         {
  96.                 Assert(listSize<maxSize,"List Capacity Exceeded");
  97.                 listArray[listSize++]=it;
  98.         }
  99.         //  移动当指向的元素
  100.         E remove()
  101.         {
  102.                 Assert((curr>=0)&&(curr<listSize),"No Current elements");
  103.                 E it=listArray[curr];
  104.                 for(int i=curr;i<=listSize-1;i++)
  105.                         listArray[i]=listArray[i+1];
  106.                 listSize--;
  107.                 //  返回排除的元素
  108.                 return it;
  109.         }
  110.         //  返回开始
  111.         void moveToStart()
  112.         {
  113.                 curr=0;
  114.         }
  115.         //  返回结尾
  116.         void moveToEnd()
  117.         {
  118.                 curr=listSize;
  119.         }
  120.         //  Back up
  121.         void prev()
  122.         {
  123.                 if(curr!=0) curr--;
  124.         }
  125.         //  Next size
  126.         void next()
  127.         {
  128.                 if(curr<listSize) curr++;
  129.         }
  130.         //  return the list size
  131.         int length() const
  132.         {
  133.                 return listSize;
  134.         }
  135.         int currPos() const
  136.         {
  137.                 return curr;
  138.         }
  139.         //  来到当前的位置
  140.         void moveToPos(int pos)
  141.         {
  142.                 Assert((pos>=0)&&(pos<=listSize),"Pos Out of range");
  143.                 curr=pos;
  144.         }
  145.         //  return current elements
  146.         const E& getValue() const
  147.         {
  148.                 Assert((curr>=0)&&(curr<listSize),"No Current elements");
  149.                 return listArray[curr];
  150.         }
  151. };
  152. //  ////////////////////////////////////////////////////////////////////////
  153. //  ////////////////////////////////////////////////////////////////////////
  154. //  主函数
  155. int main()
  156. {
  157.         AList<int> list1(100);
  158.         for(int i=0;i<=50;i++)
  159.         {
  160.                 list1.append(i+1);
  161.         }
  162.         for(int i=0;i<=50;i++)
  163.         {

  164.                 cout<<list1.getValue()<<" ";
  165.                 list1.next();
  166.                 if(i%5==4) cout<<endl;
  167.         }
  168.         return 0;
  169. }
  170. //  ////////////////////////////////////////////////////////////////////////
  171. //  ////////////////////////////////////////////////////////////////////////
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:线性表 fantuanxiaot definition function include 线性表

回帖推荐

nickchen 发表于11楼  查看完整内容

宽客好像还是用C++做开发的多呢

condmn 发表于9楼  查看完整内容

现在也在学C语言

hkmonte 发表于8楼  查看完整内容

谢谢分享!

xddlovejiao1314 发表于7楼  查看完整内容

谢谢分享。
已有 4 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
niuniuyiwan + 60 + 60 + 5 + 5 + 5 精彩帖子
zbin7451f + 100 + 5 + 5 + 5 对论坛有贡献
kychan + 100 + 1 + 1 + 1 精彩帖子
xddlovejiao1314 + 60 + 50 + 3 + 3 + 3 精彩帖子

总评分: 经验 + 320  论坛币 + 110  学术水平 + 14  热心指数 + 14  信用等级 + 14   查看全部评分

本帖被以下文库推荐

沙发
Elena3 发表于 2015-4-17 22:48:10 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

[C++改编]基于VC++6.0和VS2012的线性表实现

使用道具

藤椅
晓七 在职认证  发表于 2015-4-17 23:33:42 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

使用道具

板凳
晓七 在职认证  发表于 2015-4-17 23:36:48 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

使用道具

报纸
fjrong 在职认证  发表于 2015-4-18 00:43:59 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

使用道具

地板
fjrong 在职认证  发表于 2015-4-18 00:44:37 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

使用道具

7
xddlovejiao1314 学生认证  发表于 2015-4-18 10:20:41 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

谢谢分享。

使用道具

8
hkmonte 发表于 2015-4-18 11:07:22 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

谢谢分享!

使用道具

9
condmn 发表于 2015-4-19 12:14:33 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

现在也在学C语言

使用道具

10
condmn 发表于 2015-4-19 12:16:10 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

强大啊,楼主

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 16:16