1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| #include <iostream> using namespace std;
#define MAXSIZE 20 typedef int ElemType;
class SqList { public: SqList(); SqList(ElemType elems[],int n); ~SqList(); bool CreatList(); bool UnionList(SqList L1,SqList L2); int LocateElem(ElemType e); int ListLength(); bool GetElem(int i, ElemType& e); bool ListInsert(int i,ElemType e); bool ListDelete(int i,ElemType& e); bool ListEmpty(); void clearList(); void display();
private: ElemType data[MAXSIZE]; int length; };
SqList::SqList() { length=0; }
SqList::SqList(ElemType elems[],int n) { if(n>MAXSIZE) { cout<<"传入的顺序表长度超出最大范围,只接收了前"<<MAXSIZE<<"个元素"<<endl; length=MAXSIZE; } else length=n;
for(int i=0;i<length;i++) data[i]=elems[i]; }
SqList::~SqList() {
}
bool SqList::CreatList() {
cout<<"插入多少个元素(0-20)?"<<endl; cin>>length; if(length<0||length>MAXSIZE) { length=0; return false; } for(int i=1;i<=length;i++) {
data[i-1]=i; } return true; }
bool SqList::UnionList(SqList L1,SqList L2) { int i,j; for(i=0;i<L1.length;i++) { data[i]=L1.data[i]; }
for(j=0;j<L2.length;j++) if(L1.LocateElem(L2.data[j])==0) { if(i>=MAXSIZE) return false; data[i]=L2.data[j]; i++; } length=i; return true; }
int SqList::LocateElem(ElemType e) { for(int i=0;i<length;i++) if(data[i]==e) return i+1; return 0; }
int SqList::ListLength() { return length; }
bool SqList::GetElem(int i, ElemType& e) { if(length==0 || i<1|| i>length) return false; e=data[i-1]; return true; }
bool SqList::ListInsert(int i,ElemType e) { if(length==MAXSIZE || i<1|| i>length+1) return false; if(i<=length) { for(int k=length-1;k>=i-1;k--) data[k+1]=data[k]; } data[i-1]=e; length++; return true; }
bool SqList::ListDelete(int i,ElemType& e) { if(length==0 || i<1|| i>length) return false; e=data[i-1]; if(i<=length) { for(int k=i-1;k<length-1;k++) data[k]=data[k+1]; } length--; return true; }
bool SqList::ListEmpty() { if (length==0) return true; return false; }
void SqList::clearList() { length=0; }
void SqList::display() { for(int i=0;i<length;i++) cout<<data[i]<<" "; cout<<endl; }
int main() { SqList list; int num; ElemType elem; bool flag; cout<<"1.顺序表的创建与显示"<<endl; if(!list.CreatList()) cout<<"顺序表创建失败!"<<endl; else cout<<"顺序表创建成功!"<<endl; list.display(); cout<<endl<<endl;
cout<<"2.按元素查找"<<endl; num=list.LocateElem(3); cout<<"3是顺序表的第"<<num<<"个元素"<<endl<<endl<<endl;
cout<<"3.按位置查找"<<endl; list.GetElem(4,elem); cout<<"顺序表的第四个元素是:"<<elem<<endl<<endl<<endl;
cout<<"4.顺序表的插入"<<endl; if(list.ListInsert(2,10)) cout<<"插入成功!在第2个位置插入10后:"<<endl; else cout<<"插入失败"<<endl; list.display(); cout<<endl<<endl;
cout<<"5.删除元素"<<endl; list.ListDelete(5,elem); cout<<"删除第5个元素:"<<elem<<endl; cout<<"该表的长度为:"<<list.ListLength()<<endl; list.display(); cout<<endl<<endl;
cout<<"6.清空顺序表"<<endl; cout<<"清空顺序表前-----------"; if(!list.ListEmpty()) { cout<<"当前顺序表不是空表!"<<endl; list.clearList(); cout<<"清空顺序表后-----------"; if(list.ListEmpty()) cout<<"当前顺序表是空表!"<<endl; } cout<<endl<<endl;
cout<<"7.合并顺序表"<<endl; ElemType elems1[8]={0,1,2,3,4,5,6,7}; ElemType elems2[9]={5,6,7,8,9,10,11,1,12};
SqList list1={elems1,8}; SqList list2={elems2,9}; SqList list3; cout<<"合并前的两个表为:"<<endl; list1.display(); list2.display(); flag=list3.UnionList(list1,list2); if(!flag) cout<<"合并后,顺序表的长度超过最大范围"<<endl; cout<<"该表的长度为:"<<list3.ListLength()<<endl; list3.display(); return 0; }
|