1 #include<iostream> 2 #include<cstdlib> 3 using namespace std; 4 #define OK 1 5 #define ERROR 0 6 #define OVERFLOW -2 7 typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。 8 typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型 9 10 #define MAXSIZE 100 //顺序表可能达到的最大长度 11 12 typedef struct{ 13 ElemType *elem; //存储空间的基地址 14 int length; //当前长度 15 }SqList; 16 17 Status InitList_Sq(SqList &L){ //算法2.1 顺序表的初始化 18 //构造一个空的顺序表L 19 L.elem=new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间 20 if(!L.elem) exit(OVERFLOW); //存储分配失败 21 L.length=0; //空表长度为0 22 return OK; 23 } 24 25 Status ListInsert_Sq(SqList &L,int i,ElemType e){ //算法2.3 顺序表的插入 26 //在顺序表L中第i个位置之前插入新的元素e 27 //i值的合法范围是1<=i<=L.length+1 28 if(i<1 || i>L.length+1) return ERROR; //i值不合法 29 if(L.length==MAXSIZE) return ERROR; //当前存储空间已满 30 for(int j=L.length-1;j>=i-1;j--) 31 L.elem[j+1]=L.elem[j]; //插入位置及之后的元素后移 32 L.elem[i-1]=e; //将新元素e放入第i个位置 33 ++L.length; //表长增1 34 return OK; 35 } 36 37 Status findValIndex_Sq(SqList L,int &index,ElemType e){// 5.依值查找该元素在该表中的位置,Status表示状态,返回引用下标index 38 for(int i=0;i<L.length;++i){ 39 if(L.elem[i]==e){ 40 index=i+1; 41 return OK; 42 } 43 } 44 return ERROR; 45 } 46 47 Status delVal_Sq(SqList &L,int index){ //6.删除该表中一个位置的元素 48 if(index<1 || index>L.length)return ERROR; 49 for(int i=index-1;i<L.length;i++){ 50 L.elem[i]=L.elem[i+1]; //从该位置开始,后面的元素往前移,往前覆盖 51 } 52 L.length--; //表长减1 53 return OK; 54 } 55 56 Status findMax_Sq(SqList L,int &index,int &maxVal){ //7.Status表示状态,此函数返回引用最大值的下标和其最大值 57 if(L.length==0)return ERROR; 58 maxVal=L.elem[0]; //先标记首元素为最大值 59 index=1; //这时下标是1 60 for(int i=1;i<L.length;++i){ 61 if(L.elem[i]>maxVal){ 62 maxVal=L.elem[i]; 63 index=i+1; 64 } 65 } 66 return OK; 67 } 68 69 Status delMin_Sq(SqList &L,int &index,int &minVal){ //8.删除线性表中最小的元素 70 if(L.length==0)return ERROR; 71 minVal=L.elem[0];//先把首元素标记为最小值 72 index=1; //同时标记位置 73 for(int i=1;i<L.length;++i){ 74 if(L.elem[i]<minVal){ 75 minVal=L.elem[i]; 76 index=i+1; 77 } 78 } 79 for(int j=index-1;j<L.length;++j) 80 L.elem[j]=L.elem[j+1]; //后面的元素往前移 81 L.length--; //表长减1 82 return OK; 83 } 84 85 Status DelItem_Sq(SqList &L,int item){ //9.删除表中所有值为item的元素 此算法的时间复杂度是O(n),空间复杂度是O(1) 86 int i=0,j=L.length-1; //用两个指针来扫描顺序表 87 while(i<j){ //两个指针没有相遇的时候 88 while(i<j && L.elem[i]!=item)//当当前i扫描到与item相等时即跳出与后面不与item相等的值交换 89 ++i; 90 if(i<j){ //先判断i<j,从后面往前扫描,如果是与item相等的话,j往前移 91 while(i<j && L.elem[j]==item) 92 --j; 93 } 94 if(i<j)//如果当前i<j时 95 L.elem[i++]=L.elem[j--]; //将尾元素的值赋予当前指向的i的值(其与item相等) 96 } 97 if(j==L.length-1)return ERROR; 98 else { 99 L.length=j+1;//将j+1赋予L.length,因为实际下标是从表长-1开始 100 return OK; 101 } 102 } 103 104 int main() 105 { 106 SqList L; 107 int i,choose,index; //此处定义了index用作线性表的下标位置 108 ElemType x; 109 choose=-1; 110 while(choose!=0) 111 { 112 cout<<"********************************************************************** "; 113 cout<<"1. 建立空表 2. 在表中输入指定个数元素 "; 114 cout<<"3. 在第i个元素的前面插入一个元素 4. 逐个显示表中元素 "; 115 cout<<"5. 依值查找 6. 删除表中第i个元素 "; 116 cout<<"7. 返回表中值最大元素及其在表中位置 8. 删除线性表中值最小的数据元素 "; 117 cout<<"9. 删除表中所有值为item的元素 0. 退出 "; 118 cout<<"********************************************************************** "; 119 120 cout<<"请选择:"; 121 cin>>choose; 122 switch(choose) 123 { 124 case 1: 125 if(InitList_Sq(L)) //创建顺序表 126 cout<<"成功建立顺序表 "; 127 else 128 cout<<"顺序表建立失败 "; 129 break; 130 case 2: 131 cout<<"请输入一个数,代表元素的个数:"; //初始化指定个数元素 132 cin>>L.length; 133 cout<<"请输入"<<L.length<<"个元素的数据(以空格隔开,按回车结束): "; 134 for(i=0;i<L.length;i++) 135 cin>>L.elem[i]; 136 cout<<endl; 137 break; 138 case 3: //顺序表的插入 139 cout<<"请输入两个数,分别代表插入的位置和插入数值(用空格间隔,最后回车):"; 140 cin>>i>>x; //输入a和b,a代表插入的位置,b代表插入的数值 141 if(ListInsert_Sq(L,i,x)) 142 cout<<"插入成功. "; 143 else 144 cout<<"插入失败. "; 145 break; 146 case 4: //顺序表的输出 147 if(L.length==0) 148 cout<<"当前为空表"<<endl<<endl; 149 else { 150 cout<<"当前顺序表为:"; 151 for(i=0;i<L.length;i++) 152 cout<<L.elem[i]<<" "; 153 cout<<",表有"<<L.length<<"个元素。"<<endl<<endl; 154 } 155 break; 156 case 5://依值查找 157 index=-1; //输入元素找该元素的下标 158 cout<<"请输入要查找的一个元素位置的元素:"; 159 cin>>x; 160 if(findValIndex_Sq(L,index,x)) 161 cout<<"该元素在该表中的位置是"<<index<<". "<<endl; 162 else 163 cout<<"该表中找不到该元素"<<endl<<endl; 164 break; 165 case 6: //删除表中第i个元素 166 index=-1; //用来输入要删除元素的下标 167 cout<<"请输入要删除该表中元素的位置:"; 168 cin>>index; 169 if(delVal_Sq(L,index)) 170 cout<<"表示删除成功"<<endl<<endl; 171 else 172 cout<<"表示这个位置没有元素"<<endl<<endl; 173 break; 174 case 7: //返回表中值最大元素及其在表中位置 175 ElemType maxVal; //得到最大值 176 index=-1; 177 if(findMax_Sq(L,index,maxVal)) 178 cout<<"该表中最大元素为"<<maxVal<<";其对应位置为"<<index<<". "<<endl; 179 else 180 cout<<"该表中没有元素"<<endl; 181 break; 182 case 8: //删除线性表中值最小的数据元素 183 ElemType minVal; //标记该线性表中最小的元素 184 index=-1; //标记最小元素的下标 185 if(delMin_Sq(L,index,minVal)) 186 cout<<"已经删除了此线性表中最小的元素:"<<minVal<<";其原本的位置为"<<index<<". "<<endl; 187 else 188 cout<<"当前表为空表"<<endl<<endl; 189 break; 190 case 9: //删除表中所有值为item的元素 191 if(L.length==0) 192 cout<<"当前为空表"<<endl<<endl; 193 else{ 194 ElemType item; //定义要删除的item值 195 cout<<"请输入表中你想要删除的一个元素:"; 196 cin>>item; 197 if(DelItem_Sq(L,item)) 198 cout<<"已经删除了该表中所有为"<<item<<"的元素"<<endl<<endl; 199 else 200 cout<<"该表中不存在"<<item<<"这个元素"<<endl; 201 } 202 break; 203 } 204 } 205 return 0; 206 }