1 #include <stdio.h> 2 #include <stdlib.h> 3 #define LIST_INIT_LEN 10 4 #define LISTINCREMENT 1 5 #define OK 1 6 #define NO 0 7 8 typedef struct{ 9 int *elem; 10 int length; //当前位置 11 int listsize; //序列长度 12 }SqList; 13 14 //初始化序列表 15 int InitList(SqList *L) 16 17 { 18 L->elem = (int *)malloc(LIST_INIT_LEN * sizeof(int)); 19 if(!(L->elem)) 20 { 21 exit(NO); 22 } 23 L->length = 0; 24 L->listsize = LIST_INIT_LEN; 25 26 return OK; 27 } 28 29 //是否为空 30 int IsEmpty(SqList *L) 31 { 32 if(L->length == 0) 33 { 34 return OK; 35 } 36 else 37 return NO; 38 } 39 40 //插入num进入i元素前面,如果i存在 41 int ListInsert_Sq(SqList *L, int i, int num) 42 { 43 if(i < 1 || i > L->listsize + 1) //如果给的i不在范围内返回 44 { 45 return NO; 46 } 47 if(L->length >= L->listsize) //如果序列已满,就realloc分配空间 48 { 49 int *base; 50 base = (int *)realloc(L->elem,(L->listsize + LISTINCREMENT) * sizeof(int)); 51 if(!base) 52 { 53 exit(NO); //分配失败 54 } 55 L->elem = base; 56 L->listsize = L->listsize + LISTINCREMENT; //增加序列长度 57 } 58 59 60 int *start, *end; 61 62 end = &(L->elem[i - 1]); //第i个位置的地址 63 for(start = &(L->elem[L->length - 1]); start >= end; --start) //注意要移动到第i个位置需要将i和i之后的元素后移,所以start >= end 64 { 65 *(start + 1) = *start; //把第i个位置后面的元素后一位 66 } 67 68 *end = num; 69 (L->length)++; //位置前进一位 70 71 return OK; 72 } 73 74 //删除元素把该元素后续元素前移 75 int Delete_SqList(SqList *L, int i, int num) 76 { 77 if(i < 0 || i > L->length) //元素坐标不在范围内 78 { 79 return NO; 80 } 81 82 int *pos, *q; 83 pos = & (L->elem[i - 1]); //待删除元素坐标 84 num = *pos; 85 86 q = L->elem + L->length -1; //最后一个元素位置 87 for(++pos; pos <= q; ++pos) //所以元素前移一位 88 { 89 *(pos - 1) = *pos; 90 } 91 L->length--; 92 93 return num; 94 } 95 96 int compare(int num1, int num2) 97 { 98 if((num1 - num2) == 0) 99 { 100 return OK; 101 } 102 return NO; 103 } 104 105 int LocateElem_Sq(SqList *L, int num, int (*pfunction)(int num1, int num2)) 106 { 107 int *p; 108 int i; 109 110 i = 1; 111 p = L->elem; 112 113 while(i <= (L->length) && !(*compare)(num, *p++)) 114 { 115 ++i; 116 } 117 118 if(i <= L->length) 119 { 120 return i; 121 } 122 123 return NO; 124 } 125 126 int MergeList_Sq(SqList *L, SqList *L2, SqList *C) 127 { 128 int *pL, *pL2, *pL_last, *pL2_last, *ptr; 129 130 pL = L->elem; 131 pL2 = L2->elem; 132 133 C->listsize = L->listsize + L2->listsize; 134 C->elem = (int *)realloc(C->elem, C->listsize * sizeof(int)); 135 136 ptr = C->elem; 137 if(!(ptr)) 138 { 139 exit(NO); 140 } 141 142 pL_last = pL + L->listsize - 1; 143 pL2_last = pL2 + L2->listsize - 1; 144 145 while(pL <= pL_last && pL2 <= pL2_last) 146 { 147 148 if(*pL >= *pL2) 149 { 150 *ptr= *pL++; 151 C->length++; 152 ptr++; 153 } 154 else 155 { 156 *ptr= *pL2++; 157 C->length++; 158 ptr++; 159 } 160 } 161 while(pL <= pL_last) 162 { 163 *ptr = *pL++; 164 C->length++; 165 ptr++; 166 } 167 while(pL2 <= pL2_last) 168 { 169 *ptr = *pL2++; 170 C->length++; 171 ptr++; 172 } 173 174 return OK; 175 } 176 177 int main(void) //序列函数传递的都是指针这样才能对序列做出变动 178 { 179 int i; 180 int num; 181 SqList *example; 182 SqList *example2; 183 SqList *example3; //example指向SqList序列指针 184 185 example = (SqList *)malloc(sizeof(SqList)); 186 example2 =( SqList *)malloc(sizeof(SqList)); 187 example3 = (SqList *)malloc(sizeof(SqList)); //分配空间 188 InitList(example); 189 InitList(example2); 190 InitList(example3); 191 192 ListInsert_Sq(example, 1,12); //测试插入函数 193 ListInsert_Sq(example, 1,10); 194 ListInsert_Sq(example, 1,25); 195 196 ListInsert_Sq(example2, 1,11); 197 ListInsert_Sq(example2, 1,14); //测试插入函数 198 ListInsert_Sq(example2, 1,23); 199 200 int go; 201 MergeList_Sq(example, example2, example3); 202 for(go = 0; go < example3->length; go++) 203 { 204 printf("loc: %-12d value: %d\n", go + 1, example3->elem[go]); 205 } 206 207 208 209 while(getchar() != 'q') 210 ; 211 return 0; 212 }