#include<stdio.h>#include<stdlib.h>#define TRUE 1#define FALSE 0#define MAXSIZE 11typedef int elemType;typedef struct { elemType *list; int length;}sqList;//initvoid initList(sqList *L){ L -> length = 0; L -> list = malloc(MAXSIZE * sizeof(elemType)); //printf("%d", sizeof(elemType)); //printf("%d", L -> list); if(!L->list){ printf("failure!\n");exit(0); } printf("init success!\n"); return;}//destroyvoid destroyList(sqList *L){ //printf("%d", L -> list); if(!L -> list){ printf("not exit!\n"); exit(0); } free(L -> list); L -> list = NULL; L -> length = 0; printf("destroy successful ! \n"); return;}//clearvoid clearList(sqList *L){ if(L -> list != NULL){ L -> length = 0; } return;}//isEmptyint listEmpty(sqList *L){ return L -> length == 0 ? TRUE : FALSE;}//return the count of Lint listLength(sqList *L){ return L -> length;}//return element of the positionelemType getElem(sqList *L, int i){ if(i<1 || i > listLength(L)){ printf("out of bound!"); exit(0); } return L -> list[i-1];}//return index of the element, if not exit return 0int locateElem(sqList *L, elemType e){ elemType *p = L -> list; int i = 0; while(i < listLength(L) && *p != e){ p++; i++; } if(i == listLength(L)){i=-1; } return i+1;}//return previous elementelemType preElem(sqList *L, elemType cur_e){ int i; elemType *p = L -> list; for(i=1; i<listLength(L); i++, p++){if(*p == cur_e){ break; } } if(i == listLength(L)){printf("not exit in this sqList"); exit(0); } if(i == 1){printf("no previous element\n"); exit(0); } return L -> list[i-2];}//return next elementelemType nextElem(sqList *L, elemType cur_e){ int i; elemType *p = L -> list; for(i=1; i<listLength(L); i++, p++){if(*p == cur_e){ break; } } if(i == listLength(L)){printf("not exit in this sqList\n"); exit(0); } if(i == listLength(L)){printf("no next element\n"); exit(0); } return L -> list[i];}//insert elementvoid listInsert(sqList *L, int i, elemType e){ if(i < 1 || i > listLength(L) + 1){printf("out of bound, insert failure !\n");exit(0); } if(listLength(L) >= MAXSIZE){printf("full, insert failure !\n");exit(0); } int j = listLength(L); while(j >= i){L -> list[j] = L -> list[j-1];j--; } L -> list[i-1] = e; L -> length++; return;}//delete element and return this elementelemType listDelete(sqList *L, int i, elemType e){ if(i < 1 || i > listLength(L)){printf("out of bound delete failure !\n");exit(0); } e = L -> list[i-1]; while(i < listLength(L) + 1){L -> list[i-1] = L -> list[i];i++; } L -> length--; return e;}//traverse sqListvoid listTraverse(sqList *L){ int i; for(i=0; i<listLength(L); i++){printf("%d ", L -> list[i]); } printf("\n"); return;}main(){ sqList L; initList(&L); listInsert(&L, 1, 23); listInsert(&L, 1, 12); listInsert(&L, 1, 01); printf("共有%d個元素\n", listLength(&L)); listTraverse(&L); elemType e; e = listDelete(&L, 1, e); printf("被刪除元素是:%d\n", e); printf("共有%d個元素\n", listLength(&L)); listTraverse(&L); e = L.list[0]; printf("元素%d下一個元素是:%d\n", e, nextElem(&L, e)); //printf("元素%d上一個元素是:%d\n", e, preElem(&L, e)); printf("元素%d的位置是:%d\n", e, locateElem(&L, e)); e = getElem(&L, 1); printf("1號元素是:%d\n", e); destroyList(&L); return 0; }
如有錯誤,請指出。謝謝。