一、题目简介
实现顺序表的创建、初始化、赋值、插入、删除、按数据查找元素位置、按元素位置查找数据、清空、删除
的操作,以上操作用可用菜单选项完成
二、源程序代码
1 #include<iostream> 2 #define MAXSIZE 100 3 using namespace std; 4 typedef int ElemType; 5 typedef struct List 6 { 7 ElemType *elem; 8 int length; 9 }Sqlist; 10 void InitList(Sqlist &L); 11 void DestroyList(Sqlist &L); 12 void ClearList(Sqlist &L); 13 void GetLength(Sqlist L); 14 void isEmpty(Sqlist L); 15 void Getelem(Sqlist &L,int i); 16 void Locateelem(Sqlist &L,ElemType e); 17 void ListInsert(Sqlist &L,int i,ElemType e); 18 void ListDelete(Sqlist &L,int i); 19 void fuzhi(Sqlist &L); 20 void print(Sqlist &L); 21 void mainscreen(); 22 int main() 23 { 24 mainscreen(); 25 return 0; 26 } 27 void InitList(Sqlist &L) 28 { 29 L.elem=new ElemType[MAXSIZE]; 30 if(!L.elem){ 31 cout<<"内存空间分配失败"<<endl; 32 } 33 L.length=0; 34 cout<<"顺序表初始化成功"<<endl; 35 } 36 void mainscreen() 37 { 38 int flag=0; 39 while(flag==0) 40 { 41 cout<<"请选择对顺序表的操作"<<endl; 42 cout<<"\t1 初始化顺序表"<<endl; 43 cout<<"\t2 给顺序表赋值"<<endl; 44 cout<<"\t3 向顺序表中插入数据元素"<<endl; 45 cout<<"\t4 向顺序表中删除数据元素"<<endl; 46 cout<<"\t5 清空顺序表"<<endl; 47 cout<<"\t6 删除顺序表"<<endl; 48 cout<<"\t7 根据数据元素查询在顺序表中位置"<<endl; 49 cout<<"\t8 根据在顺序表中位置查询数据元素"<<endl; 50 cout<<"\t9 查询顺序表长度"<<endl; 51 cout<<"\t10 输出顺序表"<<endl; 52 cout<<"\t11 退出操作"<<endl; 53 int s; 54 cin>>s; 55 if(1<=s<=10) 56 { 57 switch(s) 58 { 59 case 1: 60 Sqlist L; 61 InitList(L); 62 break; 63 case 2: 64 fuzhi(L); 65 break; 66 case 3: 67 int a; 68 ElemType e; 69 cout<<"请输入你要插入的数据元素位置"<<endl; 70 cin>>a; 71 cout<<"请输入你要插入的数据元素"<<endl; 72 cin>>e; 73 ListInsert(L,a,e); 74 break; 75 case 4: 76 int b; 77 cout<<"请输入你要删除的数据元素位置"<<endl; 78 cin>>b; 79 ListDelete(L,b); 80 break; 81 case 5: 82 ClearList(L); 83 break; 84 case 6: 85 DestroyList(L); 86 break; 87 case 7: 88 ElemType f; 89 cout<<"请输入你要查找的数据元素"<<endl; 90 cin>>f; 91 Locateelem(L,f); 92 break; 93 case 8: 94 int c; 95 cout<<"请输入你要查找的数据元素的位置"<<endl; 96 cin>>c; 97 Getelem(L,c); 98 break; 99 case 9: 100 GetLength(L); 101 break; 102 case 10: 103 print(L); 104 break; 105 case 11: 106 flag=1; 107 cout<<"已成功退出对顺序表的操作"<<endl; 108 exit(0); 109 break; 110 } 111 } 112 } 113 } 114 void DestroyList(Sqlist &L) 115 { 116 if(L.elem) 117 { 118 delete[] L.elem; 119 cout<<"顺序表删除成功"<<endl; 120 } 121 else{ 122 cout<<"顺序表删除失败"<<endl; 123 } 124 } 125 void ClearList(Sqlist &L) 126 { 127 L.length=0; 128 cout<<"顺序表清空成功"<<endl; 129 } 130 void GetLength(Sqlist L) 131 { 132 cout<<"顺序表中有"<<L.length<<"个元素"<<endl; 133 } 134 void isEmpty(Sqlist L) 135 { 136 if(L.length==0) 137 cout<<"顺序表为空"<<endl; 138 else 139 cout<<"顺序表不为空"<<endl; 140 } 141 void Getelem(Sqlist &L,int i) 142 { 143 ElemType e; 144 if(i<1||i>L.length){ 145 cout<<"输入的查找参数错误"<<endl; 146 } 147 else{ 148 e=L.elem[i-1]; 149 cout<<"查找成功"<<endl; 150 cout<<"顺序表中第"<<i<<"个数据元素是"<<e<<endl; 151 } 152 } 153 void Locateelem(Sqlist &L,ElemType e) 154 { 155 int i=0; 156 int flag=0; 157 for(i=0;i<L.length;i++) 158 { 159 if(L.elem[i]==e){ 160 cout<<"查找成功"<<endl; 161 cout<<e<<"位于顺序表中第"<<i+1<<"个数据元素"<<endl; 162 flag=1; 163 } 164 } 165 if(flag==0) 166 cout<<"查找失败"<<endl; 167 } 168 void ListInsert(Sqlist &L,int i,ElemType e) 169 { 170 int j=0; 171 if(i<1||i>L.length) 172 { 173 cout<<"输入的查找参数错误"<<endl; 174 } 175 if(L.length==MAXSIZE) 176 { 177 cout<<"当前储存空间已满,无法插入"<<endl; 178 } 179 for(j=L.length-1;j>=i-1;j--) 180 { 181 L.elem[j+1]=L.elem[j]; 182 } 183 L.elem[i-1]=e; 184 ++L.length; 185 cout<<"插入顺序表中第"<<i<<"个数据元素成功"<<endl; 186 } 187 void ListDelete(Sqlist &L,int i) 188 { 189 int j=0; 190 if(i<1||i>L.length) 191 { 192 cout<<"输入的查找参数错误"<<endl; 193 } 194 for(j=i;j<=L.length-1;j++) 195 { 196 L.elem[j-1]=L.elem[j]; 197 } 198 --L.length; 199 cout<<"删除顺序表中第"<<i<<"个数据元素成功"<<endl; 200 } 201 void fuzhi(Sqlist &L) 202 { 203 int n,m; 204 cout<<"请输入赋值的数据元素个数"<<endl; 205 cin>>n; 206 for(int i=0;i<n;i++) 207 { 208 cout<<"请输入第"<<i+1<<"的数据元素的值"<<endl; 209 cin>>m; 210 L.elem[i]=m; 211 L.length++; 212 } 213 cout<<"赋值成功"<<endl; 214 } 215 void print(Sqlist &L) 216 { 217 for(int i=0;i<L.length;i++) 218 { 219 cout<<"第"<<i+1<<"的数据元素的值:"; 220 cout<<L.elem[i]<<endl; 221 } 222 cout<<"顺序表输出完成"<<endl; 223 }