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
| #include "table.h"
int main(){ int i,add=0,elem=0; table t1=initTable();
for (i=1; i<=Size; i++) { t1.head[i-1]=i; t1.length++; } printf("原顺序表:\n"); displayTable(t1); printf("当前顺序表长度为:%d\n",TableLength(t1)); if (fullTable(t1)){ printf("当前顺序表已满\n"); } printf("\n");
printf("请输入要删除的元素:"); scanf("%d",&elem); t1=delTable(t1, elem); printf("删除%d元素后,顺序表为:",elem); displayTable(t1); printf("\n");
printf("请输入要插入元素的位置和元素的值:"); scanf("%d %d",&add,&elem); printf("在第%d的位置插入元素%d后,顺序表为:\n",add,elem); t1=addTable(t1, elem, add); displayTable(t1); printf("\n");
printf("请输入要查找元素的值:"); scanf("%d",&elem); printf("查找元素%d的位置为:\n",elem); add=selectTable(t1, elem); printf("%d\n",add);
printf("请输入要修改的元素:"); scanf("%d",&elem); printf("请输入修改后的值:"); scanf("%d",&add); printf("将元素%d改为%d:\n",elem,add); t1=amendTable(t1, elem, add); displayTable(t1); printf("\n");
printf("请输入要查找元素的位置:\n"); scanf("%d",&add); printf("位置%d的元素为:",add); printf("%d\n",getTable(t1,add));
printf("有序顺序表如下\n"); t1.length=0; for (i=1; i<=Size; i++) { t1.head[i-1]=i; t1.length++; } displayTable(t1); printf("请输入要插入的元素值:\n"); scanf("%d",&elem); t1=insertTable(t1,elem); displayTable(t1);
t1.length=0; for (i=1; i<=t1.size; i++) { t1.head[i-1]=i; t1.length++; } printf("顺序表A的值为:\n"); displayTable(t1);
table t2=initTable(); for (i=t1.head[t1.size-1]+1; i<=t1.size+5; i++) { t2.head[i-t1.head[t1.size-1]-1]=i; t2.length++; } printf("顺序表B的值为:\n"); displayTable(t2); table t3=initTable(); t3=mergeTable(t1,t2,t3); printf("合并后的顺序表为:"); displayTable(t3); return 0; }
table initTable(){ table t; t.head=(int*)malloc(Size*sizeof(int)); if (!t.head) { printf("初始化失败\n"); exit(0); } t.length=0; t.size=Size; return t; }
int TableLength(table t){ return t.length; }
int emptyTable(table t){ if (!t.length) return 1; return 0; }
int fullTable(table t){ int temp=0; if (t.length==t.size) temp=1; return temp; }
table addTable(table t,int elem,int add) { if (add>t.length+1||add<1) { printf("插入位置有问题\n"); return t; } if (fullTable(t)) { t.head=(int *)realloc(t.head, (t.size+1)*sizeof(int)); if (!t.head) { printf("存储分配失败\n"); return t; } t.size++; } for (int i=t.length-1; i>=add-1; i--) { t.head[i+1]=t.head[i]; } t.head[add-1]=elem; t.length++; return t; }
table insertTable(table t,int elem){ int i=0; while(i < t.length && elem >= t.head [i]) { i++; } t=addTable(t,elem,++i); return t; }
table mergeTable(table t1,table t2,table t3){ int i=0,j=0,k=0; while(i<t1.length || j<t2.length){
if(j == t2.length || i<t1.length && t1.head[i]<t2.head[j])
t3.head[k++] = t1.head[i++]; else t3.head[k++] = t2.head[j++]; } t3.length = k; return t3; }
table delTable(table t,int add){ if (add>t.length || add<1) { printf("被删除元素的位置有误\n"); return t; } for (int i=add; i<t.length; i++) { t.head[i-1]=t.head[i]; } t.length--; return t; }
int selectTable(table t,int elem){ for (int i=0; i<t.length; i++) { if (t.head[i]==elem) { return i+1; } } return -1; }
int getTable(table t,int add){ if (add>t.length || add<1) { printf("被查找元素的位置有误\n"); return 0; } return t.head[add-1]; }
table amendTable(table t,int elem,int newElem){ int add=selectTable(t, elem); t.head[add-1]=newElem; return t; }
void displayTable(table t){ for (int i=0;i<t.length;i++) { printf("%d ",t.head[i]); } printf("\n"); }
|