1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define LIST_INIT_SIZE 100 5 #define LISTINCREMENT 10 6 #define OVERFLOW -2 7 #define OK 1 8 #define ERROR 0 9 typedef int ElemType; 10 typedef struct { 11 ElemType *elem; 12 int length; 13 int listsize; 14 }SqList; 15 16 int InitList_Sq(SqList *L); 17 int ListInsert_Sq(SqList *L,int i,ElemType e); 18 void ListInput_Sq(SqList *L); 19 void ListOutput_Sq(SqList *L); 20 int ListLength_Sq(SqList *L); 21 int ListDelete_Sq(SqList *L,int i,ElemType *e); 22 int LocateElem_Sq(SqList *L,ElemType e,int (*compare)(ElemType,ElemType)); 23 int compare(ElemType a,ElemType b); 24 void MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc); 25 void GetElem(SqList *L,ElemType i,ElemType *e); 26 int EmptyList(SqList *L); 27 void ListInverse(SqList *L); 28 29 int main() 30 { 31 SqList sq,sq1,sq2; 32 int len,e,loc,e1,f; 33 e1 = 1; 34 //顺序表初始化 35 InitList_Sq(&sq); 36 37 //输入顺序表中的元素 38 ListInput_Sq(&sq); 39 40 //输出顺序表中的元素 41 ListOutput_Sq(&sq); 42 43 //向顺序表中插入元素 44 ListInsert_Sq(&sq,3,5); 45 ListOutput_Sq(&sq); 46 47 //输出顺序表的长度 48 len = ListLength_Sq(&sq); 49 printf("该线性表的长度为:%d ",len); 50 51 //删除顺序表中某一位置的元素,并将被删除的元素返回 52 ListDelete_Sq(&sq,4,&e); 53 printf("被删除的元素为:%d ",e); 54 ListOutput_Sq(&sq); 55 56 //输出顺序表中某一元素的位序 57 loc = LocateElem_Sq(&sq,3,&compare); 58 printf("该元素位序为:%d ",loc); 59 60 //将两个顺序表合并 61 InitList_Sq(&sq1); 62 ListInput_Sq(&sq1); 63 MergeList_Sq(&sq,&sq1,&sq2); 64 ListOutput_Sq(&sq2); 65 66 //输出顺序表是否非空 67 f = EmptyList(&sq2); 68 if(f==0) { 69 printf("该顺序表是否为空? false "); 70 } else { 71 printf("该顺序表是否为空? true "); 72 } 73 74 //输出顺序表某一位置的元素 75 GetElem(&sq2,4,&e); 76 printf("第i个元素为:%d ",e); 77 78 //将顺序表逆置 79 ListInverse(&sq2); 80 ListOutput_Sq(&sq2); 81 return 0; 82 } 83 //初始化线性表 84 int InitList_Sq(SqList *L) { 85 L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); 86 if(!L->elem) exit(OVERFLOW); 87 L->length = 0; 88 L->listsize = LIST_INIT_SIZE; 89 return OK; 90 } 91 //向线性表中输入元素 92 void ListInput_Sq(SqList *L) { 93 int i,m,n; 94 printf("请输入线性表中元素个数: "); 95 scanf("%d",&n); 96 printf("请输入元素: "); 97 for(i=0; i<n; i++) { 98 scanf("%d",&m); 99 L->elem[i] = m; 100 L->length++; 101 } 102 103 return; 104 }//从线性表输出元素 105 void ListOutput_Sq(SqList *L) { 106 int i; 107 if(L->length == 0) { 108 printf("该线性表中没有元素! "); 109 } else { 110 printf("该线性表中的元素为:"); 111 for(i=0; i<L->length; i++) { 112 printf("%d ",L->elem[i]); 113 } 114 } 115 printf(" "); 116 return; 117 } 118 //返回线性表中元素的个数 119 int ListLength_Sq(SqList *L) { 120 return L->length; 121 } 122 //向线性表中插入元素 123 int ListInsert_Sq(SqList *L,int i,ElemType e) { 124 SqList *newbase; 125 //int *p,*q; 126 int m; 127 if(i<1 || i>L->length+1) return ERROR; 128 if(L->length >= L->listsize) { 129 newbase = (ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType)); 130 if(!newbase) exit(OVERFLOW); 131 L->elem = newbase; 132 L->listsize += LISTINCREMENT; 133 } 134 /*q = &(L->elem[i-1]); 135 for(p=&(L->elem[L->length-1]);p>=q;--p) { 136 *(p+1) = *p; 137 } 138 *q = e;*/ 139 for(m=L->length-1; m>=i-1; --m) { 140 L->elem[m+1] = L->elem[m]; 141 } 142 L->elem[i-1] = e; 143 ++L->length; 144 return OK; 145 } 146 //删除线性表中的元素 147 int ListDelete_Sq(SqList *L,int i,ElemType *e) { 148 int m; 149 //int *p,*q; 150 if(i<1 || i>L->length+1) return ERROR; 151 *e = L->elem[i-1]; 152 for(m=i-1;m<L->length-1;++m) { 153 L->elem[m] = L->elem[m+1]; 154 } 155 156 /*p = &(L->elem[i-1]); 157 *e = *p; 158 q = L->elem+L->length-1; 159 for(++p; p<=q; ++p) { 160 *(p-1) = *p; 161 }*/ 162 --L->length; 163 return OK; 164 } 165 //寻找某一元素在线性表里的位置 166 int LocateElem_Sq(SqList *L,ElemType e,int (*compare)(ElemType,ElemType)) { 167 int i; 168 i = 1; 169 while(i<=L->length && !(*compare)(L->elem[i-1],e)) { 170 ++i; 171 } 172 if(i<=L->length) return i; 173 else return 0; 174 } 175 int compare(ElemType a,ElemType b) { 176 if(a == b) { 177 return 1; 178 } else { 179 return 0; 180 } 181 } 182 //归并排序将两个顺序表合并(这两个顺序表都是有序的) 183 void MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc) { 184 int i,j,k; 185 i=0;j=0;k=0; 186 Lc->listsize = Lc->length = La->length + Lb->length; 187 Lc->elem = (ElemType*)malloc(Lc->listsize*sizeof(ElemType)); 188 if(!Lc->elem) exit(OVERFLOW); 189 while(i<La->length && j<Lb->length) { 190 if(La->elem[i] <= Lb->elem[j]) Lc->elem[k++] = La->elem[i++]; 191 else Lc->elem[k++] = Lb->elem[j++]; 192 } 193 while(i<La->length) Lc->elem[k++] = La->elem[i++]; 194 while(j<Lb->length) Lc->elem[k++] = Lb->elem[j++]; 195 return; 196 } 197 //返回顺序表中第i个元素的值 198 void GetElem(SqList *L,ElemType i,ElemType *e) { 199 *e = L->elem[i-1]; 200 } 201 //判断顺序表是否非空,是空的,返回true;否则,返回false。 202 int EmptyList(SqList *L) { 203 if(L->length==0) { 204 return 1; 205 } else { 206 return 0; 207 } 208 } 209 //顺序表逆置 210 void ListInverse(SqList *L) { 211 int i,temp; 212 for(i=0; i<L->length/2;i++) { 213 temp = L->elem[i]; 214 L->elem[i] = L->elem[L->length-1-i]; 215 L->elem[L->length-1-i] = temp; 216 } 217 return; 218 }