Чтобы понять причину, давайте разберем проблему.
основная функция
void main(){
...
PtrToNode list = CreateList(data1, 10); // this will create the linked list and list will point to the list, let's say ** list contains the address x **.
...
ReverseList(list);// list is passed to the function, ** list has address x **
printf("revsered list: ");PrintList(list); // list is being printed again, ** what would list contains, ofcourse, x **.
}
обратная функция
void ReverseList( PtrToNode L1){ // L1 is pointing to list, so far so good.
...
L1 = CreateList(array, 10); // L1 is overitten with address of new list. Now L1 and list in main() are not same anymore.
printf("revsered list: ");PrintList(L1); // since L1 is new list i.e. a list created with reverse data, hence it gave the correct output. Thing to note here is that new reverse list L1 and old list(create in main()) has no link and they are independent list.
}
Как получить желаемый результат?
необходимые изменения в основной функции
void main(){
...
PtrToNode list = CreateList(data1, 10);
...
list = ReverseList(list); // Update the list pointer with the reverse list.
}
Требуются изменения в функции реверса
PtrToNode ReverseList( PtrToNode L1); // change funtion to return a list pointer instead of being void.
PtrToNode ReverseList( PtrToNode L1){
...
L1 = CreateList(array, 10);
...
return L1; // return reverse list pointer
}
Предупреждение о спойлере!
Приведенный выше код подвержен утечкам памяти !!!