1 #include "stdio.h" 2 #include "stdlib.h" 3 4 #define LIST_INIT_SIZE 1000 5 #define LISTINVRESEMENT 100 6 #define OK 1 7 #define ERROR 0 8 #define OVERFLOW -2 9 10 typedef int elemType; 11 typedef struct{ 12 elemType *List; 13 int length; 14 int listsize; 15 }SqList; 16 17 void AgainMalloc(SqList *L){ 18 elemType *newbase; 19 newbase = (elemType *)realloc(L->List,(L->listsize + LISTINVRESEMENT) * sizeof(elemType)); 20 if(!newbase) 21 exit(OVERFLOW); 22 L->List = newbase; 23 L->listsize += LISTINVRESEMENT; 24 } 25 26 int InitList_Sq(SqList *L){ 27 L->List = (elemType *)malloc(LIST_INIT_SIZE * sizeof(elemType)); 28 if(!L->List) 29 exit(OVERFLOW); 30 L->length = 0; 31 L->listsize = LIST_INIT_SIZE; 32 return OK; 33 } 34 35 int ListLength(SqList *L){ 36 return L->length; 37 } 38 39 void TraverseList(SqList *L){ 40 int i; 41 for(i = 0; i < L->length; i++){ 42 printf("%d",L->List[i]); 43 } 44 printf("\n"); 45 return; 46 } 47 48 void InsertFirst(SqList *L,elemType e){ 49 int i; 50 if(L->length >= L->listsize){ 51 AgainMalloc(L); 52 } 53 for(i = L->length-1; i >= 0; i++){ 54 L->List[i+1] = L->List[i]; 55 } 56 L->List[0] = e; 57 L->length++; 58 return; 59 } 60 61 void InsertLast(SqList *L,elemType e){ 62 if(L->length >= L->listsize){ 63 AgainMalloc(L); 64 } 65 L->List[L->length] = e; 66 L->length++; 67 return; 68 } 69 70 int Insert_Sq(SqList *L,elemType e,int pos){ 71 int i; 72 if(pos < i || pos > L->length+1) 73 return ERROR; 74 if(L->length >= L->listsize){ 75 AgainMalloc(L); 76 } 77 for(i = L->length-1; i >= pos-1; i++){ 78 L->List[i+1] = L->List[i]; 79 } 80 L->List[pos-1] = e; 81 L->length++; 82 return OK; 83 } 84 85 void Search(SqList *L,elemType e){ 86 int i; 87 for(i = 0; i < L->length; i++){ 88 if(L->List[i] == e){ 89 printf("%d在第%d个位置",e,i+1); 90 return; 91 } 92 } 93 printf("没有找到!\n"); 94 return; 95 } 96 97 elemType DeleteElem(SqList *L,int pos){ 98 int i; 99 elemType temp; 100 if(pos < 1 || pos > L->length){ 101 printf("pos越界\n"); 102 exit(1); 103 } 104 temp = L->List[pos - 1]; 105 for(i = pos; i < L->length; i++){ 106 L->List[i - 1] = L->List[i]; 107 } 108 L->length--; 109 return temp; 110 } 111 112 int isEmpty(SqList *L){ 113 if(L->length == 0) 114 return 1; 115 else 116 return 0; 117 } 118 119 void Inverse(SqList *L){ 120 int low = 0, high = L->length - 1; 121 elemType temp; 122 int i; 123 for(i = 0; i < L->length/2; i++){ 124 temp=L->List[low]; 125 L->List[low++]=L->List[high]; 126 L->List[high--]=temp; 127 } 128 } 129 130 //合并顺序表 131 void MergeList(SqList *La, SqList *Lb, SqList *Lc){ 132 Lc->listsize = Lc->length = La->length + Lb->length; 133 Lc->List = (elemType *)malloc(sizeof(elemType)); 134 if(!Lc->List) 135 exit(OVERFLOW); 136 int i = 0, j = 0, k = 0; 137 while(i < La->length && j < Lb->length){ 138 if(La->List[i] <= Lb->List[j]) 139 Lc->List[k++] = La->List[i++]; 140 else 141 Lc->List[k++] = Lb->List[j++]; 142 } 143 while(i < La->length) 144 Lc->List[k++] = La->List[i++]; 145 while(j < Lb->length) 146 Lc->List[k++] = Lb->List[j++]; 147 } 148 int main(){ 149 SqList list1; 150 InitList_Sq(&list1); 151 int length; 152 scanf("%d",&length); 153 int i; 154 elemType temp; 155 for(i=0; i<length; i++){ 156 scanf("%d",&temp); 157 InsertLast(&list1,temp); 158 } 159 printf("创建好的线性表La="); 160 TraverseList(&list1);//创建好的顺序表 161 int pos; 162 scanf("%d%d",&temp,&pos); 163 Insert_Sq(&list1,temp,pos); 164 printf("插入一个元素后的线性表La="); 165 TraverseList(&list1);//插入一个数字后的线性表 166 scanf("%d",&pos); 167 DeleteElem(&list1,pos); 168 printf("删除一个元素后的线性表La="); 169 TraverseList(&list1); 170 scanf("%d",&temp); 171 Search(&list1,temp);//查找元素 172 printf("逆置后的线性表La="); 173 Inverse(&list1); 174 TraverseList(&list1); 175 SqList list2; 176 InitList_Sq(&list2); 177 scanf("%d",&length); 178 for(i=0; i<length; i++){ 179 scanf("%d",&temp); 180 InsertLast(&list2,temp); 181 } 182 SqList list3; 183 MergeList(&list1,&list2,&list3); 184 printf("合并La和Lb后的线性表="); 185 TraverseList(&list3); 186 return 0; 187 }