#include<stdio.h> #include<stdlib.h> typedef int Elemtype; #define MAXSIZE 20 typedef struct List{ Elemtype data[MAXSIZE]; int length; }List; void InitList(List *L){ L->length = 0; } void ClearLsit(List *L){ L->length = 0; } void ListEmpty(List L){ if(L.length == 0) printf("Empty!"); else printf("No Empty!\n"); } int ListLength(List *L){ return L->length; } void ListInsert(List *L,int index,Elemtype e){ if(L->length >= MAXSIZE){ printf("List is Full"); exit(1); } if(index < 1 || index > L->length + 1) printf("index error!\n"); int i; for(i = L->length; i>= index; i--){ L->data[i] = L->data[i-1]; } L->data[index - 1] = e; L->length += 1; } void ListDelete(List *L,int index,Elemtype *e){ if(index < 1 || index > L->length) printf("index Errot!\n"); int i; *e = L->data[index -1 ]; for(i = index -1 ;i<L->length-1;i++) L->data[i] = L->data[i+1]; L->length -= 1; } void GetElem(List *L,int index,Elemtype *e){ if(index < 1 || index > L->length) printf("index error!\n"); *e = L->data[index - 1]; } int locateElem(List *L,Elemtype e){ int i; for(i = 0;i<L->length;i++){ if(L->data[i] == e){ return i+1; } } return 0; } void unionList(List *LA,List LB){ int La_length = ListLength(LA); int Lb_length = ListLength(&LB); int i; for(i = 0;i<Lb_length;i++){ if(!(locateElem(LA,LB.data[i]))) ListInsert(LA,++La_length,LB.data[i]); } LA->length = La_length; } void ShowList(List L){ int i; for(i = 0; i<L.length ; i++) printf("%d ",L.data[i]); printf("\n"); } void main(){ List L; InitList(&L); ListInsert(&L,1,1); ListInsert(&L,2,2); ListInsert(&L,3,3); ListInsert(&L,4,4); ListInsert(&L,2,5); ShowList(L); int r; ListDelete(&L,4,&r); printf("Delete 4: %d\n",r); ShowList(L); GetElem(&L,2,&r); printf("GetElem 2: %d\n",r); printf("Locate 5 = %d\n",locateElem(&L,5)); printf("Locate 3 = %d\n",locateElem(&L,3)); //union List LB; InitList(&LB); ListInsert(&LB,1,3); ListInsert(&LB,2,5); ListInsert(&LB,3,37); ListInsert(&LB,4,4); ListInsert(&LB,2,9); ListInsert(&LB,5,109); ListInsert(&LB,3,2); ListDelete(&LB,4,&r); printf("\nList_A is= "); ShowList(L); printf("List_B is= "); ShowList(LB); unionList(&L,LB); printf("after nuion ListA and ListB is="); ShowList(L); printf("LA's current length is=%d\n",ListLength(&L)); } </span>