顺序表的创建
下标法
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <cstring> 5 #define ERROR 0 6 #define OK 1 7 #define MaxSize 1000000 8 using namespace std; 9 typedef int Status; 10 typedef int ElemType; 11 typedef struct 12 { 13 ElemType *elem; 14 int length; 15 }SqList; 16 SqList L; 17 SqList list2; 18 19 Status InitList(SqList &L)//顺序表初始化 20 { 21 L.elem=new ElemType[MaxSize]; 22 if(!L.elem) return ERROR; 23 24 L.length=0; 25 return OK; 26 } 27 28 void Menu()//菜单选项 29 { 30 cout<<endl; 31 cout<<" 1.数据输入:数组a数据输入顺序表"<<endl; 32 cout<<" 2.遍历:依次输出顺序表中的元素"<<endl; 33 cout<<" 3.查找:查找顺序表中为30的元素,并输出该元素是第几个元素"<<endl; 34 cout<<" 4.插入:在第三个元素前插入元素25,然后遍历顺序表"<<endl; 35 cout<<" 5.删除:删除顺序表中的第3个元素,然后遍历顺序表"<<endl; 36 cout<<" 6.修改:将顺序表中元素4修改为45,然后遍历顺序表"<<endl; 37 cout<<" 7.数据输入:将数据文件data.txt读入顺顺序表list2,数据输出"<<endl; 38 cout<<" 8.数据输出:将顺序表list2中数据写入数据文件d2.txt"<<endl; 39 cout<<"请选择功能选项:"; 40 } 41 42 Status ReadFromArray(SqList &L,int a[],int len)//1.从数组中读入数据到顺序表 43 { 44 int k=L.length; 45 for(int i=0;i<=len-1;i++) 46 { 47 L.elem[k+i]=a[i]; 48 } 49 L.length+=len; 50 return OK; 51 } 52 53 Status ListPrint(SqList &L)//2.遍历顺序表输出 54 { 55 if(!L.length) return ERROR; 56 57 for(int i=0;i<=L.length-1;i++) cout<<L.elem[i]<<' '; 58 cout<<endl; 59 return OK; 60 } 61 62 int LocateElem(SqList &L,ElemType s)//3.查找,返回元素位置序号 63 { 64 for(int i=0;i<=L.length-1;i++) if(L.elem[i]==s) return i+1; 65 return 0; 66 } 67 68 Status ListInsert(SqList &L,int i,ElemType e)//4.插入元素 69 { 70 if(i<1 || i>L.length+1) return ERROR; 71 if(L.length==MaxSize) return OK; 72 for(int k=0;k<=L.length-1;k++) if(L.elem[k]==e) return ERROR; 73 74 for(int j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j]; 75 L.elem[i-1]=e; 76 L.length++; 77 return OK; 78 } 79 80 Status ListDelete(SqList &L,int i)//5.删除元素 81 { 82 if(i<1 || i>L.length) return ERROR; 83 84 i--; 85 for(int j=i;j<=L.length-1;j++) L.elem[j]=L.elem[j+1]; 86 L.length--; 87 return OK; 88 } 89 90 Status ListChange(SqList &L,ElemType s,ElemType e)//6.修改元素 91 { 92 int i=LocateElem(L,s); 93 if(!i) return ERROR; 94 95 i--; 96 L.elem[i]=e; 97 return OK; 98 } 99 100 Status ReadFromFile(SqList &L,char *filename)//7.从文件中读入数据到顺序表 101 { 102 ifstream infile(filename,ios::in); 103 if(!infile) return ERROR; 104 105 while(!infile.eof()) 106 { 107 infile>>L.elem[L.length++]; 108 } 109 infile.close(); 110 return OK; 111 } 112 113 Status WriteToFile(SqList &L,char *filename)//8.将顺序表中数据写入到文件 114 { 115 ofstream outfile(filename,ios::out); 116 if(!outfile) return ERROR; 117 118 for(int i=0;i<=L.length-1;i++) 119 { 120 outfile<<L.elem[i]; 121 outfile<<' '; 122 } 123 outfile<<endl; 124 outfile.close(); 125 return OK; 126 } 127 128 int main() 129 { 130 int f=InitList(L); 131 if(!f) 132 { 133 cout<<"顺序表list初始化失败!"<<endl; 134 return 0; 135 } 136 else cout<<"顺序表list初始化成功!"<<endl; 137 f=InitList(list2); 138 if(!f) 139 { 140 cout<<"顺序表list2初始化失败!"<<endl; 141 return 0; 142 } 143 else cout<<"顺序表list2初始化成功!"<<endl; 144 int a[6]={10,2,30,4,50,6}; 145 146 Menu(); 147 int choose; 148 while(cin>>choose) 149 { 150 if(choose==1) 151 { 152 int ans=ReadFromArray(L,a,6); 153 if(!ans) cout<<"输入数据失败!"<<endl; 154 else cout<<"输入数据成功!"<<endl; 155 } 156 if(choose==2) 157 { 158 int ans=ListPrint(L); 159 if(!ans) cout<<"顺序表中没有元素!"<<endl; 160 } 161 else if(choose==3) 162 { 163 ElemType s=30; 164 165 int ans=LocateElem(L,s); 166 if(!ans) cout<<"未找到该元素!"<<endl; 167 else cout<<"该元素位置序号为"<<ans<<endl; 168 } 169 else if(choose==4) 170 { 171 int pos=3; 172 ElemType e=25; 173 174 int ans=ListInsert(L,pos,e); 175 if(!ans) cout<<"元素插入失败,请检查插入位置是否合法,元素是否已经存在,或空间是否已满!"<<endl; 176 else cout<<"元素插入成功!"<<endl; 177 178 ans=ListPrint(L); 179 if(!ans) cout<<"顺序表中没有元素!"<<endl; 180 } 181 else if(choose==5) 182 { 183 int pos=3; 184 185 int ans=ListDelete(L,pos); 186 if(!ans) cout<<"元素删除失败,请检查该元素是否存在!"<<endl; 187 else cout<<"元素删除成功!"<<endl; 188 189 ans=ListPrint(L); 190 if(!ans) cout<<"顺序表中没有元素!"<<endl; 191 } 192 else if(choose==6) 193 { 194 ElemType s=4; 195 ElemType e=45; 196 197 int ans=ListChange(L,s,e); 198 if(!ans) cout<<"元素修改失败,请检查该元素是否存在!"<<endl; 199 else cout<<"元素修改成功!"<<endl; 200 201 ans=ListPrint(L); 202 if(!ans) cout<<"顺序表中没有元素!"<<endl; 203 } 204 else if(choose==7) 205 { 206 int ans=ReadFromFile(list2,"data.txt"); 207 if(!ans) cout<<"数据读入失败,文件不能被打开!"<<endl; 208 else 209 { 210 cout<<"数据读入成功!"<<endl; 211 ans=ListPrint(list2); 212 if(!ans) cout<<"顺序表中没有元素!"<<endl; 213 } 214 } 215 else if(choose==8) 216 { 217 int ans=WriteToFile(list2,"d2.txt"); 218 if(!ans) cout<<"数据写入失败,文件不能被打开!"<<endl; 219 else cout<<"数据写入成功!"<<endl; 220 } 221 222 Menu(); 223 } 224 225 return 0; 226 }
指针法
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <cstring> 5 #define ERROR 0 6 #define OK 1 7 #define MaxSize 1000000 8 using namespace std; 9 typedef int Status; 10 typedef int ElemType; 11 typedef struct 12 { 13 ElemType *elem; 14 int length; 15 }SqList; 16 SqList L; 17 SqList list2; 18 19 Status InitList(SqList &L)//顺序表初始化 20 { 21 L.elem=new ElemType[MaxSize]; 22 if(!L.elem) return ERROR; 23 24 L.length=0; 25 return OK; 26 } 27 28 void Menu()//菜单选项 29 { 30 cout<<endl; 31 cout<<" 1.数据输入:数组a数据输入顺序表"<<endl; 32 cout<<" 2.遍历:依次输出顺序表中的元素"<<endl; 33 cout<<" 3.查找:查找顺序表中为30的元素,并输出该元素是第几个元素"<<endl; 34 cout<<" 4.插入:在第三个元素前插入元素25,然后遍历顺序表"<<endl; 35 cout<<" 5.删除:删除顺序表中的第3个元素,然后遍历顺序表"<<endl; 36 cout<<" 6.修改:将顺序表中元素4修改为45,然后遍历顺序表"<<endl; 37 cout<<" 7.数据输入:将数据文件data.txt读入顺顺序表list2,数据输出"<<endl; 38 cout<<" 8.数据输出:将顺序表list2中数据写入数据文件d2.txt"<<endl; 39 cout<<"请选择功能选项:"; 40 } 41 42 Status ReadFromArray(SqList &L,int a[],int len)//1.从数组中读入数据到顺序表 43 { 44 ElemType *pLe=L.elem; 45 int *pa=a; 46 int k=L.length; 47 for(int i=0;i<=len-1;i++) 48 { 49 *(k+pLe)=*pa; 50 pLe++; 51 pa++; 52 } 53 L.length+=len; 54 return OK; 55 } 56 57 Status ListPrint(SqList &L)//2.遍历顺序表输出 58 { 59 if(!L.length) return ERROR; 60 61 ElemType *pLe=L.elem; 62 for(int i=0;i<=L.length-1;i++) 63 { 64 cout<<*pLe<<' '; 65 pLe++; 66 } 67 cout<<endl; 68 return OK; 69 } 70 71 int LocateElem(SqList &L,ElemType s)//3.查找,返回元素位置序号 72 { 73 ElemType *pLe=L.elem; 74 for(int i=0;i<=L.length-1;i++) 75 { 76 if(*pLe==s) return i+1; 77 pLe++; 78 } 79 return 0; 80 } 81 82 Status ListInsert(SqList &L,int i,ElemType e)//4.插入元素 83 { 84 if(i<1 || i>L.length+1) return ERROR; 85 if(L.length==MaxSize) return OK; 86 ElemType *pLe=L.elem; 87 for(int k=0;k<=L.length-1;k++) 88 { 89 if(*pLe==e) return ERROR; 90 pLe++; 91 } 92 93 pLe=L.elem+L.length-1; 94 for(int j=L.length-1;j>=i-1;j--) 95 { 96 *(pLe+1)=*pLe; 97 pLe--; 98 } 99 *(pLe+1)=e; 100 L.length++; 101 return OK; 102 } 103 104 Status ListDelete(SqList &L,int i)//5.删除元素 105 { 106 if(i<1 || i>L.length) return ERROR; 107 108 i--; 109 ElemType *pLe=L.elem+i; 110 for(int j=i;j<=L.length-1;j++) 111 { 112 *pLe=*(pLe+1); 113 pLe++; 114 } 115 L.length--; 116 return OK; 117 } 118 119 Status ListChange(SqList &L,ElemType s,ElemType e)//6.修改元素 120 { 121 int i=LocateElem(L,s); 122 if(!i) return ERROR; 123 124 i--; 125 ElemType *pLe=L.elem; 126 *(pLe+i)=e; 127 return OK; 128 } 129 130 Status ReadFromFile(SqList &L,char *filename)//7.从文件中读入数据到顺序表 131 { 132 ifstream infile(filename,ios::in); 133 if(!infile) return ERROR; 134 135 ElemType *pLe=L.elem+L.length; 136 while(!infile.eof()) 137 { 138 infile>>*pLe; 139 pLe++; 140 L.length++; 141 } 142 infile.close(); 143 return OK; 144 } 145 146 Status WriteToFile(SqList &L,char *filename)//8.将顺序表中数据写入到文件 147 { 148 ofstream outfile(filename,ios::out); 149 if(!outfile) return ERROR; 150 151 ElemType *pLe=L.elem; 152 for(int i=0;i<=L.length-1;i++) 153 { 154 outfile<<*pLe; 155 outfile<<' '; 156 pLe++; 157 } 158 outfile<<endl; 159 outfile.close(); 160 return OK; 161 } 162 163 int main() 164 { 165 int f=InitList(L); 166 if(!f) 167 { 168 cout<<"顺序表list初始化失败!"<<endl; 169 return 0; 170 } 171 else cout<<"顺序表list初始化成功!"<<endl; 172 f=InitList(list2); 173 if(!f) 174 { 175 cout<<"顺序表list2初始化失败!"<<endl; 176 return 0; 177 } 178 else cout<<"顺序表list2初始化成功!"<<endl; 179 int a[6]={10,2,30,4,50,6}; 180 181 Menu(); 182 int choose; 183 while(cin>>choose) 184 { 185 if(choose==1) 186 { 187 int ans=ReadFromArray(L,a,6); 188 if(!ans) cout<<"输入数据失败!"<<endl; 189 else cout<<"输入数据成功!"<<endl; 190 } 191 if(choose==2) 192 { 193 int ans=ListPrint(L); 194 if(!ans) cout<<"顺序表中没有元素!"<<endl; 195 } 196 else if(choose==3) 197 { 198 ElemType s=30; 199 200 int ans=LocateElem(L,s); 201 if(!ans) cout<<"未找到该元素!"<<endl; 202 else cout<<"该元素位置序号为"<<ans<<endl; 203 } 204 else if(choose==4) 205 { 206 int pos=3; 207 ElemType e=25; 208 209 int ans=ListInsert(L,pos,e); 210 if(!ans) cout<<"元素插入失败,请检查插入位置是否合法,元素是否已经存在,或空间是否已满!"<<endl; 211 else cout<<"元素插入成功!"<<endl; 212 213 ans=ListPrint(L); 214 if(!ans) cout<<"顺序表中没有元素!"<<endl; 215 } 216 else if(choose==5) 217 { 218 int pos=3; 219 220 int ans=ListDelete(L,pos); 221 if(!ans) cout<<"元素删除失败,请检查该元素是否存在!"<<endl; 222 else cout<<"元素删除成功!"<<endl; 223 224 ans=ListPrint(L); 225 if(!ans) cout<<"顺序表中没有元素!"<<endl; 226 } 227 else if(choose==6) 228 { 229 ElemType s=4; 230 ElemType e=45; 231 232 int ans=ListChange(L,s,e); 233 if(!ans) cout<<"元素修改失败,请检查该元素是否存在!"<<endl; 234 else cout<<"元素修改成功!"<<endl; 235 236 ans=ListPrint(L); 237 if(!ans) cout<<"顺序表中没有元素!"<<endl; 238 } 239 else if(choose==7) 240 { 241 int ans=ReadFromFile(list2,"data.txt"); 242 if(!ans) cout<<"数据读入失败,文件不能被打开!"<<endl; 243 else 244 { 245 cout<<"数据读入成功!"<<endl; 246 ans=ListPrint(list2); 247 if(!ans) cout<<"顺序表中没有元素!"<<endl; 248 } 249 } 250 else if(choose==8) 251 { 252 int ans=WriteToFile(list2,"d2.txt"); 253 if(!ans) cout<<"数据写入失败,文件不能被打开!"<<endl; 254 else cout<<"数据写入成功!"<<endl; 255 } 256 257 Menu(); 258 } 259 260 return 0; 261 }
完。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步