码迷,mamicode.com
首页 > 其他好文 > 详细

顺序表

时间:2015-12-05 00:27:37      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

  1 //---------------------------顺序表-------------------------------
  2 //=----------------------A集合是否为B集合的子集------------------
  3 #include<iostream>
  4 using namespace std;
  5 
  6 enum error{rangeerror,underflow,overflow,success};
  7 const int maxlen=1000;
  8 
  9 class list{
 10 public:
 11     list();
 12     int length()const;//求长度
 13     int get_int(const int i,int &x)const;//按序号取元素运算
 14     int locate(const  int x)const;//搜索元素运算对应的函数
 15     int insert(const int i,const int x);//插入元素运算对应的函数
 16     int dele(const int i);//删除元素运算对应的函数
 17 private:
 18     int data[maxlen];
 19     int count;
 20 };
 21 
 22 
 23 list::list(){//初始顺序表化
 24     count=0;
 25 }
 26 
 27 int list::length()const{//求表长度的实现
 28     return count;
 29 }
 30 
 31 int list::get_int(const int i,int &x)const{//按序号求元素
 32     if(i<=0||i>count)return underflow;
 33     else {
 34         x=data[i-1];
 35         return success;
 36     }
 37 }
 38 
 39 int list::locate(const int x)const{//查找元素
 40     bool flags=false;
 41     for(int i=0;i<count;i++){
 42         if(data[i]==x){
 43             flags=true;
 44             return i+1;
 45             break;
 46         }
 47     }
 48     if(flags==false)return -1;
 49 }
 50 
 51 int list::insert(const int i,const int x){//插入元素
 52     if(count==maxlen)return overflow;//溢出,不能插入
 53     if(i<1||i>length()+1)return rangeerror;//插入范围有错
 54     else {
 55         for(int j=count-1;j>=i-1;j--){
 56             data[j+1]=data[j];
 57         }
 58         data[i-1]=x;
 59         count++;
 60         return success;
 61     }
 62 }
 63 
 64 
 65 int list::dele(const int i){//删除元素
 66     int j;
 67     if(length()==0)return underflow;
 68     if(i<1||i>length())return rangeerror;
 69     else {
 70         for(j=i+1;j<=length();j++){
 71             data[j-2]=data[j-1];
 72         }
 73         count--;
 74         return success;
 75     }
 76 }
 77 
 78 
 79 bool subset(list A,list B){//判断集合A是否是集合B 的子集
 80     int ia,ib,x,y;
 81     bool flags;
 82     bool suc=true;
 83     for(ia=0;ia<A.length();ia++){
 84         A.get_int(ia+1,x);
 85         flags=false;
 86         for(ib=0;ib<B.length();ib++){
 87             B.get_int(ib+1,y);
 88             if(x==y){
 89                 flags=true;
 90                 break;
 91             }
 92         }
 93         if(flags==false){
 94             suc=false;
 95             break;
 96         }
 97     }
 98     return suc;
 99 }
100 
101 
102 void merge_list(list A,list B, list &C){//顺序表A,B合并
103     int ia=0,ib=0,ic=1;
104     int x,y;
105     while(ia<A.length()&&ib<B.length()){
106         A.get_int(ia+1,x);
107         B.get_int(ib+1,y);
108         if(x==y){
109             C.insert(ic,x);
110             ic++;
111             ia++;
112             C.insert(ic,y);
113             ic++;
114             ib++;
115         }else if(x>y){
116             C.insert(ic,y);
117             ic++;
118             ib++;
119         }else {
120             C.insert(ic,x);
121             ic++;
122             ia++;
123         }
124     }
125 
126     while(ia<A.length()){
127         A.get_int(ia+1,x);
128         C.insert(ic,x);
129         ic++;
130         ia++;
131     }
132     while(ib<B.length()){
133         B.get_int(ib+1,y);
134         C.insert(ic,y);
135         ic++;
136         ib++;
137     }
138 
139     int i;
140     for(i=0;i<C.length();i++){
141         C.get_int(i+1,x);
142         cout<<x<<" ";
143     }
144     cout<<endl;
145         
146 }
147 int main(){
148     list A,B,C;
149     A.insert(1,1);
150     A.insert(2,3);
151     A.insert(3,5);
152     A.insert(4,7);
153     A.insert(5,9);
154     A.insert(6,11);
155     A.insert(7,13);
156     B.insert(1,3);
157     B.insert(2,4);
158     B.insert(3,6);
159     B.insert(4,8);
160     B.insert(5,10);
161     B.insert(6,12);
162     B.insert(7,14);
163     B.insert(8,15);
164 
165     int x,i;
166     for( i=0;i<A.length();i++){
167     A.get_int(i+1,x);
168     cout<<x<<" ";
169     }
170     cout<<endl;
171 
172 
173     for( i=0;i<B.length();i++){
174     B.get_int(i+1,x);
175     cout<<x<<" ";
176     }
177     cout<<endl;
178 
179     merge_list(A,B,C);
180 /*    for( i=0;i<C.length();i++){
181     C.get_int(i+1,x);
182     cout<<x<<" ";
183     }
184     cout<<endl;*/
185     //cout<<subset(A,B)<<endl;
186     return 0;
187 }

 

顺序表

标签:

原文地址:http://www.cnblogs.com/minmsy/p/5020808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!