顺序表: sqlist.h: /* * sqlist.h */ #ifndef SQLIST_H_ #define SQLIST_H_ #define OK 1 #define ERROR -1 #define MAXSZ 50 typedef int ElemType; typedef struct { ElemType elem[MAXSZ]; int length; } sqlist; int InitSqlist(sqlist *L); int ListEmpty(const sqlist *L); int ListLength(const sqlist *L); int PrintList(const sqlist *L); int GetElem(const sqlist *L, const int i, ElemType *e); int LocateElem(const sqlist *L, ElemType x); int ListInsert(sqlist *L, const int i, const ElemType e); int ListDelete(sqlist *L, const int i, ElemType *e); int PriorElem(const sqlist *L, const ElemType x, ElemType *e); int NextElem(const sqlist *L, const ElemType x, ElemType *e); int ReverseSqlist(sqlist *L); #endif /* SQLIST_H_ */ sqlist.c: /* * sqlist.c */ #include "sqlist.h" #include <stdio.h> int InitSqlist(sqlist *L) { if (L != NULL) { L->length = 0; return OK; } else { return ERROR; } } int ListEmpty(const sqlist *L) { if (L != NULL) { return (0 == L->length); } else { return ERROR; } } int ListLength(const sqlist *L) { if (L != NULL) { return (L->length); } else { return ERROR; } } int PrintList(const sqlist *L) { if (NULL == L || ListEmpty(L)) { printf("ERROR: The list is NULL or empty!/n"); return ERROR; } else { printf("====================/n"); int i; for (i = 0; i < L->length; i++) { printf("%d/n", L->elem[i]); } printf("====================/n"); return OK; } } int GetElem(const sqlist *L, const int i, ElemType *e) { if (NULL == L || i > L->length || i < 1) { printf("ERROR: The list is NULL/empty or the position is invalid!/n"); return ERROR; } else { *e = L->elem[i - 1]; return OK; } } int LocateElem(const sqlist *L, ElemType x) { if (NULL == L || ListEmpty(L)) { printf("ERROR: The list is NULL or empty!/n"); return ERROR; } else { int i = 0; while (i < L->length && L->elem[i] != x) { i++; } if (i >= L->length) { printf("ERROR: The elem can't be found/n"); return ERROR; } else { return i+1; } } } int ListInsert(sqlist *L, const int i, const ElemType e) { if (NULL == L || i < 1 || i > L->length + 1) { printf("ERROR: The list is NULL or the position is invalid!/n"); return ERROR; } else { int j; for (j = L->length; j > i - 1; j--) { L->elem[j] = L->elem[j - 1]; } L->elem[i - 1] = e; L->length++; return OK; } } int ListDelete(sqlist *L, const int i, ElemType *e) { if (NULL == L || i < 1 || i > L->length) { printf("ERROR: The list is NULL or the position is invalid!/n"); return ERROR; } else { *e = L->elem[i - 1]; int j; for (j = i; j < L->length; j++) { L->elem[j - 1] = L->elem[j]; } L->length--; return 0; } } int PriorElem(const sqlist *L, const ElemType x, ElemType *e) { if (NULL == L) { printf("ERROR: The list is NULL!/n"); return ERROR; } else { int i = 0; while (i < L->length && L->elem[i] != x) { i++; } if (i < 1 || i >= L->length) { printf("ERROR: The elem can't be found!/n"); return ERROR; } else { *e = L->elem[i - 1]; return OK; } } } int NextElem(const sqlist *L, const ElemType x, ElemType *e) { if (NULL == L) { printf("ERROR: The list is NULL!/n"); return ERROR; } else { int i = 0; while (i < L->length && L->elem[i] != x) { i++; } if (i < 1 || i >= L->length - 1) { printf("ERROR: The elem can't be found!/n"); return ERROR; } else { *e = L->elem[i + 1]; return OK; } } } int ReverseSqlist(sqlist *L) { if (NULL == L) { printf("Error: The list is NULL!/n"); return ERROR; } else { int i,j; ElemType temp; for(i=0,j=L->length-1;i<j;i++,j--) { temp = L->elem[i]; L->elem[i]=L->elem[j]; L->elem[j]=temp; } return OK; } } 测试的主程序文件(main函数): SeqList.c: /* ============================================================================ Name : SeqList.c Author : Version : Copyright : Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include "sqlist.h" int main(void) { sqlist L; printf("@@@@@@@@@@@Test Insert@@@@@@@@@@/n"); InitSqlist(&L); ListInsert(&L,1,1); ListInsert(&L,2,2); ListInsert(&L,3,3); PrintList(&L); ListInsert(&L,2,22); PrintList(&L); ListInsert(&L,1,11); PrintList(&L); printf("@@@@@@@@@@@Test Delete@@@@@@@@@@/n"); int e=0; ListDelete(&L,1,&e); PrintList(&L); printf("*******/ne=%d/n******/n",e); ListDelete(&L,L.length,&e); PrintList(&L); printf("*******/ne=%d/n******/n",e); ListDelete(&L,2,&e); PrintList(&L); printf("*******/ne=%d/n******/n",e); ListInsert(&L,3,3); ListInsert(&L,1,0); PrintList(&L); printf("@@@@@@@@@@@Test locate@@@@@@@@@@/n"); LocateElem(&L, 3); printf("========/npostion is:%d/n========/n",LocateElem(&L, 3)); printf("@@@@@@@@@@@Test Piror@@@@@@@@@@/n"); PriorElem(&L, 3, &e); PriorElem(&L, 1, &e); PriorElem(&L, 2, &e); printf("========/nprior is:%d/n========/n", e); PriorElem(&L, 4, &e); printf("========/nprior is:%d/n========/n", e); PriorElem(&L, 0, &e); printf("@@@@@@@@@@@Test Next@@@@@@@@@@/n"); NextElem(&L, 3, &e); e=0; NextElem(&L, 1, &e); printf("========/nnext is:%d/n========/n", e); NextElem(&L, 2, &e); printf("========/nnext is:%d/n========/n", e); NextElem(&L, 4, &e); printf("@@@@@@@@@@@Test Reverse@@@@@@@@@@/n"); PrintList(&L); ReverseSqlist(&L); PrintList(&L); return EXIT_SUCCESS; }