Update cv4/a_train.c
This commit is contained in:
		
							parent
							
								
									76864bd125
								
							
						
					
					
						commit
						8897fa16b1
					
				| @ -3,11 +3,16 @@ | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| struct car | ||||
| { | ||||
|     char value[SIZE]; // Cieľová stanica
 | ||||
|     struct car* next; // Ďaľší záznam, môže byť NULL
 | ||||
| }; | ||||
| 
 | ||||
| struct car* add_car(struct car* first,const char* target) | ||||
| { | ||||
|     struct car* ret = first; | ||||
|     struct car* nc=(struct car*) malloc(sizeof(struct car)); | ||||
|     struct car* ret=first; | ||||
|     struct car* nc=(struct car*) malloc(sizeof(struct car));  | ||||
|     strcpy(nc->value, target); | ||||
|     nc->next=NULL; | ||||
| 
 | ||||
| @ -30,6 +35,7 @@ void print_train(struct car* first) | ||||
| { | ||||
|     if(first==NULL) | ||||
|     { | ||||
|          | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
| @ -37,9 +43,7 @@ void print_train(struct car* first) | ||||
|     { | ||||
|         printf("%s", first->value); | ||||
|         first=first->next; | ||||
|         if(first){ | ||||
|             printf("->"); | ||||
|         } | ||||
|         if(first) printf("->"); | ||||
|     }while(first!=NULL); | ||||
| 
 | ||||
| } | ||||
| @ -57,43 +61,45 @@ void cancel_train(struct car* first) | ||||
| 
 | ||||
| struct car* clear_train(struct car* first, const char* target) | ||||
| { | ||||
|     struct car* t;  //змінна для збереження кого видаляти
 | ||||
|     struct car* t;   | ||||
|     struct car* ptr;   | ||||
| 
 | ||||
|     if(first==NULL || target==NULL) | ||||
|     { | ||||
|         return first; | ||||
|     } | ||||
| 
 | ||||
|     //голова == таргет - видаляємо лише перший
 | ||||
|     while(strcmp(first->value, target)==0) //поки рядки однакові
 | ||||
|     | ||||
|     while(strcmp(first->value, target)==0)  | ||||
|     { | ||||
|         t=first; | ||||
|         first=first->next; | ||||
|         free(t); | ||||
|          | ||||
|         if(first==NULL) | ||||
|         { | ||||
|              | ||||
|            return NULL; | ||||
|         } | ||||
|         }         | ||||
|     } | ||||
| 
 | ||||
|     //голова не  таргет
 | ||||
|        | ||||
|     ptr=first; | ||||
|     | ||||
|     do | ||||
|     { | ||||
|         if(strcmp(first->next->value, target)==0) | ||||
|         if(strcmp(ptr->next->value, target)==0) | ||||
|         { | ||||
|             t=first; | ||||
|             first=first->next; | ||||
|             t=ptr->next;; | ||||
|             ptr->next = ptr -> next->next; | ||||
|             free(t); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             first=first->next; | ||||
|             ptr = ptr -> next; | ||||
|         } | ||||
|     } | ||||
|     while(first); | ||||
|     }while(ptr->next); | ||||
| 
 | ||||
| 
 | ||||
| 	return first; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user