/*algorithm-2.1.c*/ 1 #include<stdio.h> 2 #include<linear_list_ADT.h> 3 4 int main(void){ 5 List LA,LB; 6 int LA_len,LB_len; 7 InitList(&LA); 8 InitList(&LB); 9 /*********get LIST_A***********/ 10 printf("please input length of LA:"); 11 scanf("%d",&LA_len); 12 for(int i=0;i<LA_len;i++){ 13 printf("please input No%d Element in LA:", i+1); 14 ElemType Element; 15 scanf("%d",&Element); 16 ListInsert(&LA,i+1,Element); 17 } 18 printf("LA:"); 19 for (int i = 0; i < LA_len; i++) 20 { 21 ElemType Element; 22 GetElem(&LA,i+1,&Element); 23 printf(" %d", Element); 24 } 25 printf("\n"); 26 /*********get LIST_A***********/ 27 /*********get LIST_B***********/ 28 printf("please input length of LB:"); 29 scanf("%d",&LB_len); 30 for(int i=0;i<LB_len;i++){ 31 printf("please input No%d Element in LB:", i+1); 32 ElemType Element; 33 scanf("%d",&Element); 34 ListInsert(&LB,i+1,Element); 35 } 36 printf("LB:"); 37 for (int i = 0; i < LB_len; i++) 38 { 39 ElemType Element; 40 GetElem(&LB,i+1,&Element); 41 printf(" %d", Element); 42 } 43 printf("\n"); 44 /*********get LIST_B***********/ 45 46 /*********algorithm-2.1***********/ 47 int LC_len=LA_len+LB_len,i=0,j=0,k=1; 48 List LC; 49 ElemType Element_A,Element_B; 50 InitList(&LC); 51 52 while((i < LA_len) && (j < LB_len)) 53 { 54 GetElem(&LA,i+1,&Element_A); 55 GetElem(&LB,j+1,&Element_B); 56 if(Element_A >= Element_B){ 57 printf("%d vs %d\n", Element_A,Element_B); 58 ListInsert(&LC,k,Element_A); 59 k++; 60 i++; 61 } 62 else{ 63 printf("%d vs %d\n", Element_A,Element_B); 64 ListInsert(&LC,k,Element_B); 65 k++; 66 j++; 67 } 68 } 69 printf("i=%d\n", i); 70 printf("j=%d\n", j); 71 printf("k=%d\n", k); 72 if(i<=j){ 73 while(k<=LC_len){ 74 GetElem(&LA,i+1,&Element_A); 75 ListInsert(&LC,k,Element_A); 76 i++; 77 k++; 78 } 79 } 80 else{ 81 while(k<=LC_len){ 82 GetElem(&LB,j+1,&Element_B); 83 ListInsert(&LC,k,Element_B); 84 j++; 85 k++; 86 } 87 } 88 89 /*********algorithm-2.1***********/ 90 91 /***********result***************/ 92 printf("LC:"); 93 for (int i = 0; i < LC_len; i++) 94 { 95 ElemType Element_C; 96 GetElem(&LC,i+1,&Element_C); 97 printf(" %d", Element_C); 98 } 99 printf("\n"); 100 /***********result***************/ 101 return 0; 102 }
/*linear_list_ADT.h*/
1 #include<malloc.h> 2 3 #define LIST_INIT_SIZE 100 4 #define SUCCES 1 5 #define FAILED 0 6 #define ERROR -1 7 8 typedef int ElemType; 9 typedef int Status; 10 11 typedef struct{ 12 ElemType *data; 13 int length;}List; 14 15 Status InitList(List *L) 16 { 17 L->data = (ElemType *)(malloc(sizeof(ElemType)*LIST_INIT_SIZE)); 18 L->length = 0; 19 return SUCCES; 20 } 21 22 Status InitList_with_Len(List *L,int len) 23 { 24 L->data = (ElemType *)(malloc(sizeof(ElemType)*len)); 25 L->length = len; 26 return SUCCES; 27 } 28 29 Status DestroyList(List *L) 30 { 31 free(L->data); 32 L->length = 0; 33 return SUCCES; 34 } 35 36 Status ClearList(List *L) 37 { 38 39 L->length = 0; 40 return SUCCES; 41 } 42 43 Status ListEmpty(List L) 44 { 45 if(L.length==0){ 46 return SUCCES;} 47 else{ 48 return FAILED;} 49 } 50 51 int ListLength(List L) 52 { 53 //L中存的是线性表的地址 54 /* 55 int length = 0; 56 while() 57 { 58 length++; 59 } 60 */ 61 //return *L->length是肯定不对的。。。否则直接在调用时这样简单做就好了,干嘛还非要写一个函数呢 62 return L.length; 63 } 64 65 //返回第i个元素 66 Status GetElem(List *L,int i,ElemType *e)//e里面存放着参数地址 67 { 68 if(i>=1 && i<=L->length){ 69 *e = L->data[i-1]; 70 return SUCCES;} 71 else{ 72 return FAILED;} 73 } 74 75 Status LocateElem(List *L,ElemType e,int *i) 76 { 77 for(int j=0;j<L->length;j++) 78 { 79 if(L->data[j] == e){ 80 *i=j; 81 break;} 82 } 83 if(i!=0){ 84 return SUCCES;} 85 else{ 86 return FAILED;} 87 } 88 89 Status PriorElem(List *L,ElemType cur_e,ElemType *pre_e) 90 { 91 if(L->data[0]==cur_e){ 92 return FAILED; 93 } 94 else{ 95 for(int i=1;i<L->length;i++){ 96 if(L->data[i]==cur_e){ 97 *pre_e = L->data[i-1]; 98 break; 99 } 100 } 101 return SUCCES; 102 } 103 } 104 105 Status NextElem(List *L,ElemType cur_e,ElemType *next_e) 106 { 107 if(L->data[L->length-1]==cur_e){ 108 return FAILED;} 109 else{ 110 for(int i=0;i<L->length-1;i++){ 111 if(L->data[i]==cur_e){ 112 *next_e = L->data[i+1]; 113 break; 114 } 115 } 116 return SUCCES; 117 } 118 } 119 120 //第i个位置上插入新的元素 121 Status ListInsert(List *L,int i,ElemType e) 122 { 123 if(i>=1 && i<=L->length+1){ 124 if(i==L->length+1){ 125 L->data[i-1]=e; 126 ++L->length; 127 } 128 else{ 129 for(int j=i-1;j<L->length;j++) 130 { 131 L->data[j+1]=L->data[j]; 132 } 133 L->data[i-1]=e; 134 ++L->length; 135 } 136 return SUCCES; 137 } 138 else{ 139 return FAILED; 140 } 141 } 142 143 //删除第i个元素 144 Status ListDelete(List *L,int i,ElemType *e) 145 { 146 if(i>=0 && i<L->length){ 147 *e = L->data[i-1]; 148 for(int j=i;j<L->length;j++){ 149 L->data[i-1]=L->data[i]; 150 } 151 free(&L->data[i]); 152 L->length--; 153 return SUCCES; 154 } 155 else{ 156 return FAILED; 157 } 158 }
时间: 2024-10-07 07:39:50