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
| #define maxsize 100 #define OK 1 #define ERROR 0 typedef int elemtype; #include<stdio.h>
typedef struct { elemtype *elem; int length; }sqlist;
int intlist(sqlist &L) { L.elem = new elemtype[maxsize]; if (!L.elem) return ERROR; L.length=0; return OK; }
int listinsert(sqlist &L,int i,elemtype e) { if((i<1)||(i>L.length+1)) return ERROR; if(L.length==maxsize) return ERROR; for(int j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j]; L.elem[i-1]=e; ++L.length; printf("插入结束\n"); return OK; }
int listinsert1(sqlist &L,int i,elemtype e) { if((i<1)||(i>L.length+1)) return ERROR; if(L.length==maxsize) return ERROR; for(int j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j]; L.elem[i-1]=e; ++L.length; return OK; }
int getrlme(sqlist &L,int i,elemtype &e) { if(i<1||i>L.length) return ERROR; e=L.elem[i-1]; printf("取值成功\n"); return OK; }
int locatelem(sqlist L,elemtype e) { for(int i=0;i<L.length;i++) if(L.elem[i]==e) return i+1; return 0; }
int listdelete(sqlist &L,int i,elemtype &e) { if(i<1||i>L.length) {printf("删除错误"); return ERROR;} e=L.elem[i-1]; for(int j=i;j<=L.length-1;j++) L.elem[j-1]=L.elem[j]; --L.length; return OK; }
int appear(sqlist L) { for(int i=0;i<L.length;i++) {printf("%d",L.elem[i]); printf(" ");} printf("\n显示结束\n"); return OK; }
void MergeList(sqlist la, sqlist lb, sqlist &lc) { printf("la表内的值为:"); appear(la); printf("lb表内的值为:"); appear(lb); for(int ii=0;ii<la.length;ii++) {listinsert1(lc,ii+1,la.elem[ii]);} for(int i=0;i<lb.length;i++) {int why=locatelem(lc,lb.elem[i]); if(why==0) listinsert1(lc,lc.length+1,lb.elem[i]);} printf("lc表内的值为:"); appear(lc); }
void inverse(sqlist &lc) { printf("原来的lc为"); appear(lc); int swidth=lc.length/2,h,l=lc.length; for(int zhou=0;zhou<swidth;zhou++) {h=lc.elem[zhou]; lc.elem[zhou]=lc.elem[l-1]; lc.elem[l-1]=h; l--;} printf("逆置后的lc为"); }
int main() { int i,j,k,l,w,hhh=1; int lawidth,lbwidth; int laa[30],lbb[30]; elemtype e,f,g; sqlist Q; sqlist la,lb,lc; intlist (Q); intlist (la); intlist (lb); intlist (lc); sqlist law; intlist(law);
printf("请输入顺序表新建顺序表长度:"); scanf("%d",&law.length); printf("请输入该顺序表的值:"); hhh=hhh+law.length; for(i=0;i<law.length;i++) { scanf("%d",&law.elem[i]); } for(int kk=0;kk<law.length;kk++) { listinsert1(Q,kk+1,law.elem[kk]); } printf("\n");
printf("说明:0为退出\n1为插入元素\n2为取值\n3为查找\n4为删除\n5为显示顺序表\n6为建立la,lb并且合并为lc\n7为就地逆置lc\n"); do{ printf("\n请输入你的操作数字\n"); scanf("%d",&w); if (w==1) {printf("\n请输入你想插入的位置(1~%d)\n",hhh); scanf("%d",&i); while(i>hhh||i<1) { printf("超范围了,请重新输入操作数字\n"); printf("\n请输入你想插入的位置(1~%d)\n",hhh); scanf("%d",&i); } ++hhh; printf("请输入你想插入元素的值\n"); scanf("%d",&e); listinsert(Q,i,e); appear(Q); printf("\n");} else if(w==2) {printf("\n请输入你想取值的元素位置(1~%d)\n",(hhh-1)); scanf("%d",&j); if(j>hhh-1) {printf("超范围了,请重新输入操作数字\n");continue;} getrlme(Q,j,f); printf("该元素为%d\n",f); printf("\n");} else if(w==3) {printf("\n请输入查找元素的值\n"); scanf("%d",&k); int lc=locatelem(Q,k); if(lc) printf("元素的位置为%d\n",lc); else printf("该元素不存在"); printf("\n");} else if(w==4) {printf("\n请输入要删除元素的位置(1~%d)\n",hhh-1); scanf("%d",&l); if(l>hhh-1) {printf("超范围了,请重新输入操作数字\n");continue;} listdelete(Q,l,g); printf("删除成功,删除的元素为%d\n",g); hhh--; printf("\n");} else if(w==5) {printf("\n显示顺序表\n"); appear(Q);} else if(w==6) {printf("请输入la的数据长度(1~30):\n"); scanf("%d",&lawidth); printf("请输入la内的数据:"); for(int cun=0;cun<lawidth;cun++) {scanf("%d",&laa[cun]);} for(int ruler=0;ruler<lawidth;ruler++) {listinsert1(la,ruler+1,laa[ruler]);} printf("请输入lb的数据长度(1~30):\n"); scanf("%d",&lawidth); printf("请输入lb内的数据:"); for(int cun1=0;cun1<lawidth;cun1++) {scanf("%d",&lbb[cun1]);} for(int ruler1=0;ruler1<lawidth;ruler1++) {listinsert1(lb,ruler1+1,lbb[ruler1]);} MergeList(la,lb,lc); } else if(w==7) {inverse(lc); appear(lc);} else break; }while(w); printf("谢谢使用\n"); }
|