/*************************************************************************
> File Name: list.h
> Author:
> Mail:
> Created Time: Thu 05 Sep 2024 02:10:41 PM CST
************************************************************************/#ifndef_LIST_H#define_LIST_H#include<stdio.h>#include<stdlib.h>#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2#defineNOEXIST-3typedefint Status;#defineLIST_INIT_SIZE100#defineLISTINCREMENT10typedefint ElemType;typedefstruct{
ElemType *elem;int length;int listsize;} List;#defineDATAFMT"%d"
Status InitList(List &L);
Status DestroyList(List &L);
Status ClearList(List &L);
Status ListEmpty(List L);intListLength(List L);
Status GetElem(List L,int i, ElemType &e);intequal(ElemType a, ElemType b);
Status LocateElem(List L, ElemType e,intequal(ElemType, ElemType));
Status PriorElem(List L, ElemType cur_e, ElemType &pre_e);
Status NextElem(List L, ElemType cur_e, ElemType &next_e);
Status ListInsert(List &L,int i, ElemType e);
Status ListDelete(List &L,int i, ElemType &e);
Status visit(ElemType e);
Status ListTraverse(List L, Status visit(ElemType));voidInputList(List &L,int n);voidunionList(List &La, List Lb);voidMergeList(List La, List Lb, List &Lc);#endif
list.c函数文件
/*************************************************************************
> File Name: list.c
> Author:
> Mail:
> Created Time: Thu 05 Sep 2024 02:16:38 PM CST
************************************************************************/#include<stdio.h>#include<stdlib.h>#include"list.h"
Status InitList(List &L){
L.elem =(ElemType *)malloc(LIST_INIT_SIZE *sizeof(ElemType));if(!L.elem)exit(OVERFLOW);
L.length =0;
L.listsize = LIST_INIT_SIZE;return OK;}//InitList
Status DestroyList(List &L){free(L.elem);return OK;}//DestroyList
Status ClearList(List &L){for(int i =0; i < L.listsize; i++){
L.elem[i]=0;}
L.length =0;return OK;}//ClearList
Status ListEmpty(List L){return L.length ==0? TRUE : FALSE;}//ListEmptyintListLength(List L){return L.length;}//ListLength
Status GetElem(List L,int i, ElemType &e){if(i <1|| i >ListLength(L)){return ERROR;}
e = L.elem[i-1];return OK;}//GetElemintequal(ElemType a, ElemType b){return a == b ? TRUE : FALSE;}//equal
Status LocateElem(List L, ElemType e,intequal(ElemType, ElemType)){for(int i =0; i <ListLength(L); i++){if(equal(e, L.elem[i])){return i +1;}}return FALSE;}//LocateElem
Status PriorElem(List L, ElemType cur_e, ElemType &pre_e){if(L.elem[0]== cur_e){return ERROR;}int i;for(i =0; i <ListLength(L); i++){if(L.elem[i]== cur_e){break;}}if(i ==ListLength(L)){return NOEXIST;}
pre_e = L.elem[i-1];return OK;}//PriorElem
Status NextElem(List L, ElemType cur_e, ElemType &next_e){if(L.elem[L.length-1]== cur_e){return ERROR;}int i;for(i =0; i <ListLength(L); i++){if(L.elem[i]== cur_e){break;}}if(i ==ListLength(L)){return NOEXIST;}
next_e = L.elem[i+1];return OK;}//NextElem
Status ListInsert(List &L,int i, ElemType e){if(i <1|| i > L.length +1){return ERROR;}if(L.length >= L.listsize){
ElemType *newbase =(ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT)*sizeof(ElemType));if(!newbase)exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;}
ElemType *q =&(L.elem[i-1]);for(ElemType *p =&(L.elem[L.length-1]); p >= q;--p){*(p +1)=*p;}*q = e;++L.length;return OK;}//ListInsert
Status ListDelete(List &L,int i, ElemType &e){if(i <1|| i > L.length){return ERROR;}
ElemType *p =&(L.elem[i-1]);
e =*p;
ElemType *q =&(L.elem[L.length -1]);for(++p; p <= q;++p){*(p -1)=*p;}--L.length;return OK;}//ListDelete
Status visit(ElemType e){if(!e)return ERROR;printf(DATAFMT, e);printf(" ");return OK;}//visit
Status ListTraverse(List L, Status visit(ElemType)){printf("List traverse: ");for(int i =0; i < L.length; i++){if(!visit(L.elem[i])){return FALSE;}}printf("\n");return OK;}//ListTraversevoidInputList(List &L,int n){if(n >= L.listsize){
ElemType *newbase =(ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT)*sizeof(ElemType));if(!newbase)exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;}printf("Enter %d List Elem: ", n);for(int i =0; i < n; i++){scanf(DATAFMT,&L.elem[i]);}
L.length = n;}//InputListvoidunionList(List &La, List Lb){int La_len =ListLength(La);int Lb_len =ListLength(Lb);int i;
ElemType e;for(i =1; i <= Lb_len; i++){GetElem(Lb, i, e);if(!LocateElem(La, e, equal)){ListInsert(La,++La_len, e);}}}//unionListvoidMergeList(List La, List Lb, List &Lc){InitList(Lc);int i =1, j =1, k =0;int La_len =ListLength(La);int Lb_len =ListLength(Lb);
ElemType ai, bj;while((i <= La_len)&&(j <= Lb_len)){GetElem(La, i, ai);GetElem(Lb, j, bj);if(ai <= bj){ListInsert(Lc,++k, ai);++i;}else{ListInsert(Lc,++k, bj);++j;}}while(i <= La_len){GetElem(La, i++, ai);ListInsert(Lc,++k, ai);}while(j <= Lb_len){GetElem(Lb, j++, bj);ListInsert(Lc,++k, bj);}}//MergeList
main.c主文件
/*************************************************************************
> File Name: main.c
> Author:
> Mail:
> Created Time: Thu 05 Sep 2024 02:18:11 PM CST
************************************************************************/#include<stdio.h>#include<stdlib.h>#include"list.h"#include"list.c"intmain(){
List L;//Initialize the listInitList(L);//Input the list and traverse itInputList(L,10);ListTraverse(L, visit);//Determine whether the list is emptyif(ListEmpty(L)){printf("List is empty!\n\n");}else{printf("List is not empty!\n\n");}//Clear the listprintf("Prepare clear the list...\n");if(ClearList(L)){printf("List is clear!\n");}else{printf("List is not clear!\n");}//After clearing the list, check whether the list is emptyif(ListEmpty(L)){printf("List is empty!\n\n");}else{printf("List is not empty!\n\n");}//Input the list againInputList(L,10);printf("\n");//Input the number of the element you want to get//Here is 3.int num1;printf("Enter the number of the element you want to get: ");scanf("%d",&num1);
ElemType e1;GetElem(L, num1, e1);printf("No.%d Elem is ", num1);printf(DATAFMT, e1);printf(".\n\n");//Input the location of the element you want to get//Here is 99
ElemType elem;printf("Enter the element you want to locate: ");scanf(DATAFMT,&elem);if(LocateElem(L, elem, equal)){printf("The position of the element ");printf(DATAFMT, elem);printf(" is %d\n\n",LocateElem(L, elem, equal));}else{printf("The list doesn't have the elem\n\n");}//Input the element for which you want to get the priority element//Here is 5
ElemType num2, e2;printf("Enter the element for which you want to get the priority element: ");scanf(DATAFMT,&num2);if(PriorElem(L, num2, e2)){printf("The prior elem of ");printf(DATAFMT, num2);printf(" is ");printf(DATAFMT, e2);printf(".\n\n");}elseif(PriorElem(L, num2, e2)==-3){printf("The elem ");printf(DATAFMT, num2);printf(" dosen't exist!\n\n");}else{printf("The elem %d doesn't have prior elem.\n\n", num2);}//Input the element for which you want to get the next element//Here is 9
ElemType num3, e3;printf("Enter the element for which you want to get the next element: ");scanf(DATAFMT,&num3);if(NextElem(L, num3, e3)){printf("The next elem of ");printf(DATAFMT, num3);printf(" is ");printf(DATAFMT, e3);printf(".\n\n");}elseif(NextElem(L, num3, e3)==-3){printf("The elem ");printf(DATAFMT, num3);printf(" dosen't exist!\n\n");}else{printf("The elem %d doesn't have next elem.\n\n", num3);}//Input the element and the location you want to insert//Here is 18 and 6int num4;
ElemType e4;printf("Enter the element you want to insert: ");scanf(DATAFMT,&e4);printf("Enter the location you want to insert: ");scanf("%d",&num4);printf("Insert elem %d to postion %d...\n", e4, num4);ListInsert(L, num4, e4);ListTraverse(L, visit);printf("\n");//Input the number of the element you want to delete//Here is 2int num5;printf("Enter the number of the element you want to delete: ");scanf("%d",&num5);
ElemType e5;printf("Prepare delete the No.%d elem...\n", num5);ListDelete(L, num5, e5);printf("The delete elem is ");printf(DATAFMT, e5);printf(".\n");ListTraverse(L, visit);printf("\n");//Destroy the listprintf("Prepare destroy the list...\n");if(DestroyList(L)){printf("List is destroyed!\n");}else{printf("List is not destroyed!\n");}//Use unionList Methods
List La1, Lb1;InitList(La1);InitList(Lb1);InputList(La1,5);ListTraverse(La1, visit);InputList(Lb1,5);ListTraverse(Lb1, visit);printf("\nUnion List La1 and Lb1...\n");unionList(La1, Lb1);ListTraverse(La1, visit);printf("\n");//Use MergeList Methods
List La2, Lb2, Lc;InitList(La2);InitList(Lb2);InputList(La2,5);ListTraverse(La2, visit);InputList(Lb2,5);ListTraverse(Lb2, visit);printf("\nMerge List La2 and Lb2...\n");MergeList(La2, Lb2, Lc);ListTraverse(Lc, visit);return0;}