Describe the function that forms the list M1 - a copy of the list M and the list M2, which is the “inverted” list M. The function my_copy does not work. What's wrong? And I do not know how to make an inverted list ...
#include <stdlib.h> #include <stdio.h> #include <conio.h> typedef struct st { // элемент int info; // элемент хранения struct st *prev; // указатель на следующий элемент } ELEMENT; void add(ELEMENT **head, int item); // поместить item в void my_copy(ELEMENT** head); void print_list(ELEMENT *head); void clear(ELEMENT **head); int main(){ ELEMENT *head = NULL; ELEMENT *new_head = NULL; int i,value; for (i = 0; i< 4; i++){ printf("Vvedite element %d:", i+1); scanf("%d",&value); add(&head,value); } puts("Spisok1"); print_list(head); puts("\nSpisok2:"); while(head){ print_list(my_copy(head));} clear(&head); return 0; } void add(ELEMENT **head, int item){ // поместить item в список ELEMENT * new_item; new_item = (ELEMENT *) malloc( sizeof (ELEMENT) );//выделяем память под новый элемент new_item -> info = item; // присваиваем значение121345678939 новому элементу new_item -> prev = *head; // присоединяем новый элемент на вершину *head = new_item; } void print_list(ELEMENT *head){ while (head){ printf("%d ",head->info); head = head->prev; } } void clear(ELEMENT **head){ ELEMENT *element; while (*head){ element = *head; *head = (*head)-> prev; free(element); } } ELEMENT* my_copy(ELEMENT **head){ ELEMENT *i_ptr = *head; ELEMENT *new_new_item; new_new_item = (ELEMENT *) malloc( sizeof (i_ptr->info,i_ptr->prev) ); ELEMENT *new_head = new_new_item; while( i_ptr->prev != NULL ){ new_new_item->prev = (ELEMENT *) malloc( sizeof (i_ptr->prev->info,i_ptr->prev->prev) ); i_ptr = i_ptr->prev; new_new_item = new_new_item->prev; } return new_head; }
List_push_front()adds a new item to the top of the list. Example : List M1 = List_copy (M, NULL); List M2 = List_reverse_inplace (M); Some compilers are able to optimize the recursive call in the tail position. - jfs