1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<malloc.h> 4 5 typedef int Elemtype; 6 7 typedef struct Node{ 8 int data; 9 struct Node* next; 10 }Node; 11 12 typedef struct Node* LinkList; 13 14 void InitLinkList(LinkList *L); 15 void DestoryList(LinkList* L); 16 void ClearList(LinkList *L); 17 void HeadCreateList(LinkList*,int); 18 void TailCreateList(LinkList*,int); 19 void DisplayList(LinkList); 20 21 //void InitLinkList(Node** L){ 22 // *L = (LinkList)malloc(sizeof(struct Node)); 23 // (*L)->next = NULL; 24 // //alternation 25 //} 26 void InitList(LinkList *L){ 27 *L = (LinkList)malloc(sizeof(struct Node)); 28 (*L)->next = NULL; 29 } 30 void DestoryList(LinkList* L){ 31 /* 32 LinkList p = *L,q; 33 printf("%d %d\n",p,*L); //p == *L == 7279912 34 while(p){ 35 q = p->next; 36 free(p); 37 p = q; //last step p = NULL 38 } 39 printf("%d %d",p,*L); //p = 0;*L = 7279912 40 free(*L); 41 printf("%d ",*L); //*L = 7279912 42 analise the reason for error by the result of "printf" 43 free(p) is release the block of memory pointed by P, 44 dose not change pointer p itself. NULL == 0 45 */ 46 LinkList q; 47 while(*L){ 48 q = (*L)->next; 49 free(*L); 50 *L = q; 51 } 52 } 53 void ClearList(LinkList *L){ 54 //ClearList(&L); 55 // LinkList p = *L; 56 // LinkList q; 57 // 58 // TailCreateList(&L,4); 59 // DisplayList(L); 60 // ClearList(&L); 61 // TailCreateList(&L,4); 62 63 LinkList p = (*L)->next; 64 LinkList q; 65 while(p){ 66 q = p -> next; 67 free(p); 68 p = q; 69 } 70 (*L)->next = NULL; 71 printf("LinkList has been clear!\n"); 72 } 73 void ListEmpty(LinkList L){ 74 if(L->next == NULL) 75 printf("List empty!\n"); 76 else 77 printf("Exit Element in List\n"); 78 } 79 int ListLength(LinkList L){ 80 LinkList p = L->next; 81 int count = 0; 82 while(p){ 83 count+=1; 84 p = p->next; 85 } 86 return count; 87 } 88 int GetElem(LinkList L,int index){ 89 int count = ListLength(L); 90 if(index < 1 || index > count+1){ 91 printf("index error!\n"); 92 exit(1); 93 } 94 int j = 0; 95 while(j<index){ 96 L = L->next; 97 j++; 98 } 99 return L->data; 100 } 101 void GetPriorElem(LinkList L,int current_elem,int *priorElement){ 102 /* 103 LinkList p = L->next; 104 LinkList q; 105 if(p == NULL){ 106 printf("List is Empty!\n"); 107 exit(1); 108 } 109 if(p->data == current_elem){ 110 printf("the current element is first!\n"); 111 exit(1); 112 } 113 q = p->next; 114 while(q && q->data != current_elem){ 115 p = q;q = q->next; 116 } 117 if(q == NULL){ 118 printf("there is no current in List!\n"); 119 exit(1); 120 } 121 else 122 *priorElement = p->data; 123 */ 124 /* 125 the promise of the following code is that the 126 current_element is not the first element of the 127 list and does not consider the case of the list 128 for NULL 129 */ 130 LinkList p = L->next; //p point first node 131 LinkList q; 132 while(p->next){ 133 q = p->next; 134 if(q->data == current_elem){ 135 *priorElement = p->data; 136 break; 137 } 138 p = q; 139 } 140 if(p->next == NULL) 141 printf("there is no current_element in List\n"); 142 } 143 void GetNextElem(LinkList L,int current_elem,int* next_elem){ 144 LinkList p = L->next; 145 LinkList q; 146 if(p == NULL){ 147 printf("List is Empty!\n"); 148 exit(1); 149 } 150 while(p->next){ 151 q = p->next; 152 if(p->data == current_elem){ 153 *next_elem = q->data; 154 return; 155 } 156 p = q; 157 } 158 printf("GetNextElement is Failed!\n"); 159 } 160 void HeadCreateList(LinkList* L,int n){ 161 LinkList s; 162 int i; 163 int e; 164 for(i = 1;i <= n; i++){ 165 printf("enter %d integer: ",i); 166 scanf("%d",&e); 167 s = (LinkList)malloc(sizeof(struct Node)); 168 s->data = e; 169 s->next = (*L)->next; 170 (*L)->next = s; 171 } 172 printf("\n"); 173 } 174 void TailCreateList(LinkList* L,int n){ 175 LinkList tail = *L; 176 LinkList s; 177 int i; 178 int e; 179 for(i = 1;i <= n; i++){ 180 printf("enter %d integer: ",i); 181 scanf("%d",&e); 182 s = (LinkList)malloc(sizeof(struct Node)); 183 s->data = e; 184 tail->next = s; 185 s->next = NULL; 186 tail = s; 187 } 188 printf("\n"); 189 } 190 void ListInsert(LinkList* L,int index,int e){ 191 /* 192 //insert element before index 193 LinkList q; 194 LinkList p = *L; 195 LinkList s; 196 int count = 0; 197 while(p->next){ 198 q = p->next; 199 count += 1; 200 if(count == index){ 201 s = (LinkList)malloc(sizeof(struct Node)); 202 s -> data = e; 203 p->next = s; 204 s->next = q; 205 break; 206 } 207 p = q; 208 } 209 if(p->next == NULL){ 210 printf("InsertList Index Error!\n"); 211 } 212 */ 213 //options 214 LinkList p = *L; 215 LinkList s; 216 int count = 0; 217 while(p && count<index-1){ 218 p = p->next; 219 count += 1; 220 } 221 if(p){ 222 s = (LinkList)malloc(sizeof(struct Node)); 223 s->data = e; 224 s->next = p->next; 225 p->next = s; 226 } 227 if(p == NULL){ 228 printf("InsertList Index Error!\n"); 229 } 230 } 231 void ListDelete(LinkList *L,int index,int* e){ 232 LinkList p = *L; 233 int count = 0; 234 while(p && count < index - 1){ 235 p = p->next; 236 count += 1; 237 } 238 if(p){ 239 if(p->next){ 240 *e = p->next->data; 241 p->next = p->next->next; 242 } 243 else 244 printf("DeleteList Index Error\n"); 245 } 246 if(p == NULL) 247 printf("DeleteList Index Error\n"); 248 } 249 void DisplayList(LinkList L){ 250 if(L == NULL){ 251 printf("List has been destory!"); 252 exit(1); 253 } 254 LinkList p = L; 255 while(p -> next){ 256 printf("%d ",p->next->data); 257 p = p->next; 258 } 259 printf("\n********displayList executed!\n"); 260 } 261 void main(){ 262 Node L; 263 LinkList L1 = &L; 264 InitLinkList(&L1); 265 266 LinkList L; 267 InitList(&L); 268 TailCreateList(&L,4); 269 DisplayList(L); 270 ClearList(&L); 271 DisplayList(L); 272 ClearList(&L); 273 DisplayList(L); 274 DestoryList(&L); 275 DisplayList(L); 276 277 ClearList(&L); 278 ListEmpty(L); 279 TailCreateList(&L,5); 280 ListEmpty(L); 281 printf("count of List is: %d\n",ListLength(L)); 282 DisplayList(L); 283 printf("getelement index=6 is : %d\n",GetElem(L,6)); 284 int r1; 285 GetPriorElem(L,3,&r1); 286 printf("the result of GetPriorElement(3) is: %d\n",r2); 287 288 int r2; 289 GetNextElem(L,3,&r2); 290 printf("the result of GetNextElement(3) is: %d\n",r2); 291 ListInsert(&L,5,999); 292 int r3; 293 ListDelete(&L,5,&r3); 294 printf("execute ListDelete index=5 is %d\n",r3); 295 DisplayList(L); 296 }
时间: 2024-11-03 20:52:00