1 #include<stdio.h>
2 #include<stdlib.h>
3
4 typedef int Elemtype;
5 #define MAXSIZE 20
6
7 typedef struct List{
8 Elemtype data[MAXSIZE];
9 int length;
10 }List;
11
12 void InitList(List *L){
13 L->length = 0;
14 }
15 void ClearLsit(List *L){
16 L->length = 0;
17 }
18 void ListEmpty(List L){
19 if(L.length == 0) printf("Empty!");
20 else printf("No Empty!\n");
21 }
22 int ListLength(List *L){
23 return L->length;
24 }
25 void ListInsert(List *L,int index,Elemtype e){
26 if(L->length >= MAXSIZE){
27 printf("List is Full");
28 exit(1);
29 }
30 if(index < 1 || index > L->length + 1)
31 printf("index error!\n");
32 int i;
33 for(i = L->length; i>= index; i--){
34 L->data[i] = L->data[i-1];
35 }
36 L->data[index - 1] = e;
37 L->length += 1;
38 }
39 void ListDelete(List *L,int index,Elemtype *e){
40 if(index < 1 || index > L->length)
41 printf("index Errot!\n");
42 int i;
43 *e = L->data[index -1 ];
44 for(i = index -1 ;i<L->length-1;i++)
45 L->data[i] = L->data[i+1];
46 L->length -= 1;
47 }
48 void GetElem(List *L,int index,Elemtype *e){
49 if(index < 1 || index > L->length)
50 printf("index error!\n");
51 *e = L->data[index - 1];
52 }
53 int locateElem(List *L,Elemtype e){
54 int i;
55 for(i = 0;i<L->length;i++){
56 if(L->data[i] == e){
57 return i+1;
58 }
59 }
60 return 0;
61 }
62 void unionList(List *LA,List LB){
63 int La_length = ListLength(LA);
64 int Lb_length = ListLength(&LB);
65 int i;
66 for(i = 0;i<Lb_length;i++){
67 if(!(locateElem(LA,LB.data[i])))
68 ListInsert(LA,++La_length,LB.data[i]);
69 }
70 LA->length = La_length;
71 }
72 void ShowList(List L){
73 int i;
74 printf("[ ");
75 for(i = 0; i<L.length ; i++)
76 printf("%d ",L.data[i]);
77 printf("]\n");
78 }
79
80 void main(){
81 List L;
82 InitList(&L);
83 ListInsert(&L,1,1);
84 ListInsert(&L,2,2);
85 ListInsert(&L,3,3);
86 ListInsert(&L,4,4);
87 ListInsert(&L,2,5);
88 ShowList(L);
89
90 int r;
91 ListDelete(&L,4,&r);
92 printf("Delete 4: %d\n",r);
93 ShowList(L);
94
95 GetElem(&L,2,&r);
96 printf("GetElem 2: %d\n",r);
97
98 printf("Locate 5 = %d\n",locateElem(&L,5));
99 printf("Locate 3 = %d\n",locateElem(&L,3));
100
101 //union
102 List LB;
103 InitList(&LB);
104 ListInsert(&LB,1,3);
105 ListInsert(&LB,2,5);
106 ListInsert(&LB,3,37);
107 ListInsert(&LB,4,4);
108 ListInsert(&LB,2,9);
109 ListInsert(&LB,5,109);
110 ListInsert(&LB,3,2);
111 ListDelete(&LB,4,&r);
112
113 printf("ListA is= ");
114 ShowList(L);
115 printf("ListB is= ");
116 ShowList(LB);
117 unionList(&L,LB);
118 printf("after nuion =");
119 ShowList(L);
120 printf("LA's current length is=%d\n",ListLength(&L));
121 }
2016-02-0417:32:12
