线性表之顺序表

    数据结构草草学过,不过没有认真运用过。 虽然知道一些最为基本的抽象类型及一些常用操作,不过叫我把这些基本的算法写出来我也是写不出来的。因为常说数据结构+算法是一个程序员最基本的素质,所以这次认真加以复习。在复习的同时我尽量将自己学习的其他的一些基本知识比如C++中的面向对象思想也引入进来,同时也会将在复习中碰到其他的一些问题提出来,能解决的便解决,不能解决的可以试着解决。
    To be a programmer .
----------------------------------------------------------------------------------------------------------------

    线性表是由 n (n >= 0) 个类型相同的数据元素a1, a2, a3, …, an 组成的有限序列,记作(a1, a2, … , ai-1, ai, ai+1, …, an)。这里的数据元素 ai(1<=i<=n)只是一个抽象的符号,其具体的含义在不同情况下可以不同,它即可以是原子类型,也可以是结构类型,但同一线性表中的数据元素必须属于同一数据对象。
    线性表有两种基本的存储结构:顺序存储结构和链式存储结构。本文档只涉及顺序存储结构,顺序结构通常可用高级程序设计语言中的数组来表示。另外文章的组织结构分为三部分:开头是简单的介绍,之后是数据结构基本运算的代码,最后是在复习中遇到的一些问题。 
    以下代码实现了一些线性表最基本的运算,相比书上的程序例子,自己新增了三点,有些写法是从参考别人的程序而得来的:
    1) 运用了C++中的类模版概念;
    2) 加入了C++中的异常捕获;
    3) 规范了程序的写法,每个函数有了更为详细的介绍信息。
    实现代码分三个部分:seqlist.h定义顺序表模版类,seqlist.cpp是对模版类的基本实现,m
ain.cpp 是用于基本的测试。

----------------------------------------------------------------------------------------------------------------

Code:
  1. //seqlist.h   
  2.   
  3. #ifndef _SEQLIST_H_   
  4. #define _SEQLIST_H_   
  5.   
  6. const int MAX_SIZE = 100;   
  7.   
  8. template <class T>   
  9. class SeqList   
  10. {   
  11. public:   
  12.     SeqList();   
  13.     SeqList(T a[], int n);   
  14.     ~SeqList();   
  15.     int Length();   
  16.     T Get(int i);   
  17.     int Locate(T x);   
  18.     void Insert(int i, T x);   
  19.     T Delete(int i);   
  20.     void PrintList();   
  21. private:   
  22.     T data[MAX_SIZE];   
  23.     int length;   
  24. };   
  25.   
  26. #endif   
Code:
  1. //seqlist.cpp   
  2.   
  3. #include <iostream>   
  4. #include "seqlist.h"   
  5.   
  6.   
  7. /*  
  8.  * pre condition: the seqlist is not exist.  
  9.  * input: no  
  10.  * purpose: to construct a seqlist.  
  11.  * output: no  
  12.  * for condition: created a seqlist.  
  13.  */  
  14.  template <class T>   
  15. SeqList<T>::SeqList()   
  16. {   
  17.     length = 0;   
  18. }   
  19.   
  20.   
  21. /*  
  22.  * pre condition: the seqlist is not exist.  
  23.  * input: one array containing n elements.  
  24.  * purpose: to construct a seqlist use the array.  
  25.  * output: no  
  26.  * for condition: created a seqlist.  
  27.  */  
  28. template <class T>   
  29. SeqList<T>::SeqList(T a[], int n)   
  30. {   
  31.     if (n>MAX_SIZE) throw "invalid arguments";   
  32.     for (int i=0; i<n; i++)   
  33.         data[i] = a[i];   
  34.     length = n;   
  35. }   
  36.   
  37. /*  
  38.  * pre condition: no  
  39.  * input: no  
  40.  * purpose: no  
  41.  * output: no  
  42.  * for condition: no  
  43.  */  
  44. template <class T>   
  45. SeqList<T>::~SeqList()   
  46. {   
  47. }   
  48.   
  49. /*  
  50.  * pre condition: the seqlist is existed.  
  51.  * input: the insert element 'x', insert position 'i'.  
  52.  * purpose: insert the element 'x' into the position 'i'.  
  53.  * output: no  
  54.  * for condition: the seqlist inserted the new element.  
  55.  */  
  56. template <class T>   
  57. void SeqList<T>::Insert(int i, T x)   
  58. {   
  59.     int j;   
  60.     if (length >= MAX_SIZE)   
  61.         throw "overflow";   
  62.     if (i<0 || i>length)   
  63.         throw "invalid position";   
  64.     for (j=length; j>=i; j--)   
  65.         data[j] = data[j-1];   
  66.     data[i] = x;   
  67.     length ++;   
  68. }   
  69.   
  70. /*  
  71.  * pre condition: the seqlist is existed.  
  72.  * input: the position of the element to be deleted.  
  73.  * purpose: delete the element 'x' in the position 'i'.  
  74.  * output: no  
  75.  * for condition: the seqlist deleted the new element.  
  76.  */  
  77. template <class T>   
  78. T SeqList<T>::Delete(int i)   
  79. {   
  80.     T  x;   
  81.     if (length == 0)   
  82.         throw "downflow";   
  83.     if (i<1 || i>length)   
  84.         throw "invalid position";   
  85.     x = data[i-1];   
  86.     for (int j=i; j<length; j++)   
  87.         data[j-1] = data[j];   
  88.     length --;   
  89.     return x;   
  90. }   
  91.   
  92. /*  
  93.  * pre condition: the seqlist is existed.  
  94.  * input: no  
  95.  * purpose: return the number of the seqlist.  
  96.  * output: no  
  97.  * for condition: no  
  98.  */  
  99. template <class T>   
  100. int SeqList<T>::Length()   
  101. {   
  102.     return length;   
  103. }   
  104.   
  105. /*  
  106.  * pre condition: the seqlist is existed.  
  107.  * input: the index of a element.  
  108.  * purpose: return the value of the element.  
  109.  * output: the value of a element.  
  110.  * for condition: no  
  111.  */  
  112. template <class T>   
  113. T SeqList<T>::Get(int i)   
  114. {   
  115.     if (i<1 || i>length)   
  116.         throw "invalid index.";   
  117.     return data[i-1];   
  118. }   
  119.   
  120. /*  
  121.  * pre condition: the seqlist is existed.  
  122.  * input: the value wanted to be located.  
  123.  * purpose: return the index of the element.  
  124.  * output: no  
  125.  * for condition: no  
  126.  */  
  127. template <class T>   
  128. int SeqList<T>::Locate(T x)   
  129. {   
  130.     for(int i=0; i<length; i++)   
  131.     {   
  132.         if (data[i] == x)   
  133.             return i;   
  134.     }   
  135.     return -1;   
  136. }   
  137.   
  138. /*  
  139.  * pre condition: the seqlist is existed.  
  140.  * input: no  
  141.  * purpose: print all the element in the list.  
  142.  * output: no  
  143.  * for condition: no  
  144.  */  
  145. template <class T>   
  146. void SeqList<T>::PrintList()   
  147. {   
  148.     std::cout <<" all the elems in the seqlist are:/n";   
  149.     for(int i=0; i<length; i++)   
  150.         std::cout <<data[i] <<" ";   
  151.     std::cout <<std::endl;   
  152. }   
Code:
  1. //main.cpp   
  2.   
  3. #include <iostream>   
  4. #include "seqlist.cpp"   
  5.   
  6. int main()   
  7. {   
  8.     try{   
  9.     /* the first test!  
  10.      * construct one empty seqlist and do something about it.  
  11.      */  
  12.   
  13.     SeqList<float> seqlist;   
  14.     for(int i=0; i<5; i++)   
  15.         seqlist.Insert(i, 2.2 + i);   
  16.     seqlist.PrintList();   
  17.     std::cout <<"the length is: " <<seqlist.Length();   
  18.     std::cout <<std::endl;   
  19.     // do deletion   
  20.     seqlist.Delete(1);   
  21.     seqlist.PrintList();   
  22.     // getting end locating the elem   
  23.     std::cout << "the 2th elem is: " <<seqlist.Get(2) <<"/n";   
  24.     std::cout << "the 3.2 elem position: " <<seqlist.Locate(3.2) <<"/n";   
  25.     // do insertion   
  26.     seqlist.Insert(3, 111.1);   
  27.     seqlist.PrintList();   
  28.   
  29.     /* the second test !  
  30.      * construct seqlist through one existing array.  
  31.      */  
  32.   
  33.     int base[6] = { 2, 3, 4, 5, 6, 7};   
  34.     SeqList<int> seqlist_a(base, 6);   
  35.     seqlist_a.PrintList();   
  36.     std::cout <<"the length is:" <<seqlist_a.Length();   
  37.     std::cout <<std::endl;   
  38.     // getting and locating the elem   
  39.     std::cout << "the 1st elem: "<<seqlist_a.Get(1) <<"/n";   
  40.     std::cout << "the 5 positin: " <<seqlist_a.Locate(5)<<"/n";    
  41.    // do deletion   
  42.     seqlist_a.Delete(2);   
  43.     seqlist_a.PrintList();   
  44.     // do insertion   
  45.     seqlist_a.Insert(3, 222);   
  46.     seqlist_a.PrintList();   
  47.     }   
  48.     catch(const char *err)   
  49.     {   
  50.         std::cout <<err;   
  51.     }   
  52.   
  53.   
  54.     return 1;   
  55. }   

----------------------------------------------------------------------------------------------------------------

    因为自己在决定复习数据结构的同时也复习下一些Linux的基本操作(学过,但是忘得也快),所以自己装了一个红帽的虚拟机(VMware),并通过SSH客户端来与虚拟系统进行文件传输操作。在对顺序表进行实现的过程中主要碰到三个问题:

    1)什么是SSH(Secure Shell)?
    我学会用这个东西时是在老师的介绍之下,之后也就知道了有这么一个工具可以完成与虚拟系统进行文件的传输工作,不需要每
次很麻烦的去装VMware tools 或者设置所谓的共享目录了,不过对于它是一个什么东西了解得不多。
    我们所熟悉的SSH通常是指的一种基于SSH协议的、通用的、功能强大的网络安全解决方案。一般的Unix系统、Linux系统都附带
有支持SSH的应用程序包。SSH最主要的功能便是支持安全的网络数据传输。在具体的应用上,SSH是可以替代Telnet及FTP的。
    因为在虚拟系统中已经有了支持SSH的应用程序包,所以我们便可以通过一个相对的客户端程序与之进行连接并实现数据传输。

    2)在VMware的网络连接设置方式上,有桥接、NAT、Host-only三种不同的连接方式,它们有什么区别,在通过SSH Client进行连接的时候需要怎么进行设置?

    桥接模式下,虚拟出来的操作系统与宿主主机的关系就像(局域网)是连接在同一个Hub上的两台主机,这是你需要为虚拟系统配置IP及Netmask,并且俩系统还需处于同一网段,这样俩系统便可进行通信。并且,虚拟系统可以访问局域网内任意一台主机。如果想利用VMWare在局域网内新建一个虚拟服务器,为局域网用户提供网络服务,就应该选择桥接模式。
    Host-only模式下,虚拟系统的IP等配置均有VMnet1(host-only)虚拟网络的DHCP服务器来动态分配的。如果想用VMWare创建一个与网内其
他机器相隔离的虚拟系统,可以选择此种模式。
    NAT模式下,虚拟系统借助NAT功能,通过宿主主机所在的网络来访问公网。这种模式下的IP信息时通过VMnet8(NAT)虚拟网络的DHCP服务
器提供的。无法修改,因此虚拟系统无法和本局域网中的其他真实主机进行通信。

    所以在设置SSH Client的时候,可以选择【桥接模式】或者【Host-only】模式,在【桥接模式】下,只需要保证虚拟系统的IP地址与主机的IP地址处于同一个网段即可进行通信。在【Host-only模式】下,必须要保证虚拟系统的IP地址与在本机上所虚拟出来的网卡的IP地址(VMware Network Adapter VMnet1)处在同一网段。

    3)在用GCC进行编译的时候便出现错误提示,但是用G++进行编译却没有错误提示,很显然,两个编译器是有所差别的?
    GCC最初的全称为 ‘GNU C Compiler’,之后随着高级程序设计语言的增多,GCC的全称逐渐演变为了’GNU Compiler
Collection’。
    对于G++来说,它会将被编译的文件都当作C++文件来看待,并且它会自动调用C++的标准库。
 对于GCC来说,它会根据源文件的后缀名将它们区分开来,但是有一点不同的是GCC在编译C++源程序时并不会主动链接相关的库
,所以需要额外指定参数。如如:gcc -lstdc++ testCpp.cpp -o testCpp。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值