1 //============================================================================
  2  // Name        : 2.cpp
  3  // Author      : menglei
  4  // Version     :
  5  // Copyright   : Your copyright notice
  6  // Description : Hello World in C++, Ansi-style
  7  //============================================================================
  8  /**
  9   * 题目2:顺序表相关算法的实验验证
 10  [实验目的]
 11  验证线性表的顺序存储及其上的基本操作。
 12  [实验内容及要求]
 13  定义顺序存储的线性表类。
 14  实验验证如下算法的正确性、各种功能及指标:
 15  1)创建可容纳M个结点的顺序表,(M10);
 16  2)在顺序表中第k个结点后插入一个新结点;
 17  3)存取顺序表中第k个结点的值;
 18  4)删除顺序表中第k个结点;
 19  5)顺序查找值为x的元素在顺序表中的位置(下标)。
 20  为便于观察程序的运行结果,设计的输出函数能在输出设备上以图形或表格或其它直观的形式展现、存储计算结果。
 21  测试程序时,对所有输入变量取遍各种有代表性的值。
 22  为了增强程序的可读性,程序中要有适当的注释。
 23   */
 24  #include <iostream>
 25  #include <cString>
 26  #include <stdlib.h>
 27  using namespace std;
 28  #define max_size 100
 29  
 30  class mArray{  //
 31  private:
 32      int *head;
 33      int count;
 34  public:
 35      mArray(){
 36          head = new int [max_size];
 37          count = 0;
 38          memset(head,0,sizeof(int));//clear to 0
 39      }
 40      ~mArray(){
 41          delete []head;
 42      }
 43      void insert(int value,int pos){
 44          if(pos<0)
 45          {
 46              cout<<"input error!";
 47              exit(1);
 48          }
 49          if(count>max_size){
 50              cout<<"array overflow!";
 51              exit(1);
 52          }
 53          if(pos>count){
 54              head[count]=value;
 55              count++;
 56              cout<<">element "<<value<<" had insert to posion "<<count<<endl;
 57              return ;
 58          }
 59          if(pos==count){
 60              head[pos]=value;
 61              count ++;
 62              cout<<"=element "<<value<<" had insert to posion "<<count<<endl;
 63              return ;
 64          }
 65          if(pos<count){ //insert and move the elements behind
 66              //cout<<count<<" "<<pos<<endl;
 67              for(int i = count ; i >= pos ;i--){
 68                  //move form the back
 69                  head[i+1] = head[i];
 70              }
 71              head[pos] = value;
 72              count++;
 73              cout<<"<element "<<value<<" had insert to posion "<<pos+1<<endl;
 74          }
 75  
 76  
 77  
 78      }
 79      int getValue(int pos){
 80          return head[pos-1];
 81      }
 82      void del(int pos){
 83          for(int i = pos ; i< count ;i++){
 84              head[i] = head[i+1];
 85          }
 86          count--;
 87          cout<<"element posion "<<pos+1<<" had deleted"<<endl;
 88      }
 89      int find(int value){
 90          for(int i = 0;i<count ;i++){
 91              if(getValue(i)==value)
 92                  return i;
 93          }
 94          cout<<"element \""<<value<<"\"not find"<<endl;
 95          return -1;
 96      }
 97      void print(){
 98          cout<<"链结:";
 99          for(int i = 0; i < count ;i++){
100              cout<<head[i];
101              cout<<"->";
102          }
103          cout<<endl;
104      }
105  
106  };
107  int main() {
108      mArray m;
109      m.insert(1,0);//add value 2 in 0 pos
110      m.insert(2,1);
111      m.insert(3,2);
112      m.insert(5,5);
113      m.print();
114      m.insert(4,3);
115      m.print();
116      m.insert(7,2);
117      m.print();
118      m.insert(8,10);//special
119      m.print();
120      cout<<"element value 4'posion is "<<m.find(4)<<endl;
121      cout<<"posion 2'value is "<<m.getValue(2)<<endl;
122      m.del(3);
123      m.print();
124      return 0;
125  }
126  

运行结果:

posted on 2012-10-29 20:31  _兜  阅读(437)  评论(0编辑  收藏  举报