线性表1-顺序表

顺序表属于线性表的一种存储表现形式,在计算机内部表示为一段连续的内存空间,数组就是一种顺序表。

下面是顺序表的主要操作:

  1 //顺序表的基本操作
  2 template <typename Type> class Vector {
  3 private:
  4     //size为容量,length为当前元素个数
  5     int size,length;
  6     Type *data;
  7 public:
  8     //构造函数
  9     Vector(int input_size) {
 10         size=input_size;
 11         length=0;
 12         data=new Type[size];
 13     }
 14 
 15     //析构函数
 16     ~Vector() {
 17         delete[] data;
 18     }
 19 
 20     //插入函数
 21     bool insert(int loc, Type value) {
 22         if (loc < 0 || loc > length) {
 23             return false;
 24         }
 25         if (length >= size) {
 26             expand();
 27         }
 28         for (int i = length; i > loc; --i) {
 29             data[i] = data[i - 1];
 30         }
 31         data[loc] = value;
 32         length++;
 33         return true;
 34     }
 35 
 36     // 扩容函数
 37     void expand(){
 38         Type *old_data=data;
 39         //这里将容量扩为原来的2倍
 40         size=size*2;
 41         data=new Type[size];
 42         for(int i=0;i<length;i++){
 43             data[i]=old_data[i];
 44         }
 45         delete[] old_data;
 46     }
 47 
 48     //查找函数,找到则返回下标,否则返回-1
 49     //传入参数:const防止其被修改,&避免传入后重新创建参数浪费空间
 50     int search(const Type &value) {
 51         for(int i=0;i<length;i++){
 52             if(data[i]==value){
 53                 return i;
 54             }
 55         }
 56         return -1;
 57     }
 58 
 59      //删除指定位置的元素
 60      bool remove(int index) {
 61         if(index<0||index>=length){
 62             return false;
 63         }
 64         for(int i=index+1;i<length;i++){
 65             data[i-1]=data[i];
 66         }
 67         length--;
 68         return true;
 69     }
 70 
 71     //遍历函数
 72     void print() {
 73         for(int i=0;i<length;i++){
 74             if(i>0){
 75                 cout<<" ";
 76             }
 77             cout<<data[i];
 78         }
 79         cout<<endl;
 80     }
 81 
 82     //获取指定位置的元素
 83     Type get_data(int loc){
 84         if(loc<0||loc>=length){
 85             cout<<"failed"<<endl;
 86             return 0;
 87         }
 88         return data[loc];
 89     }
 90 
 91     //修改指定位置的元素
 92     bool change_data(int loc,Type new_data){
 93         if(loc<0||loc>=length){
 94             return false;
 95         }
 96         data[loc]=new_data;
 97         return true;
 98     }
 99 
100     int get_length(){
101         return length;
102     }
103 
104     //将顺序表左移k位
105     void left_shift(int k){
106         if(k<=0||k>=length){
107             return;
108         }
109         Type *temp=new Type[k];
110         for(int i=0;i<k;i++){
111             temp[i]=data[i];
112         }
113         for(int i=k;i<length;i++){
114             data[i-k]=data[i];
115         }
116         for(int i=0;i<k;i++){
117             data[i+length-k]=temp[i];
118         }
119         delete[] temp;
120     }
121 
122 };
123 
124 
125 //求两个递增集合的交集:从第一个元素开始往后比较,小的往后继续走,相等的就加入记录结果的Vector中
126 void intersect(Vector<int> &listA, Vector<int> &listB, Vector<int> &intersection){
127     int length_A=listA.get_length(),length_B=listB.get_length();
128     int i_a=0,i_b=0,i_c=0;
129     while(i_a<length_A&&i_b<length_B){
130         if(listA.get_data(i_a)<listB.get_data(i_b)){
131             i_a++;
132         }
133         else if(listA.get_data(i_a)>listB.get_data(i_b)){
134             i_b++;
135         }
136         else{
137             intersection.insert(i_c, listA.get_data(i_a));
138             i_a++;
139             i_b++;
140             i_c++;
141         }
142     }
143 }

 

posted @ 2017-06-10 19:35  NoviScl  阅读(159)  评论(0编辑  收藏  举报