각 반복에 head
및 head_next
변화의 값이 함수의 작동 방법을 이해하는 데 도움이 방법을 검토. 반복의
시작 :
*head: NULL
*head_next: a -> b -> c -> d -> NULL
먼저 전화 stack_reverse()
에 :
temp = (*head_next)->next; // temp: b -> c -> d -> NULL
(*head_next)->next = (*head); // *head_next: a -> NULL
*head = *head_next; // *head: a -> NULL
*head_next = temp; // *head_next: b -> c -> d -> NULL
두 번째 호출 stack_reverse()
에 :
temp = (*head_next)->next; // temp: c -> d -> NULL
(*head_next)->next = (*head); // *head_next: b -> a -> NULL
*head = *head_next; // *head: b -> a -> NULL
*head_next = temp; // *head_next: c -> d -> NULL
세 번째 호출 stack_reverse()
에 :
-
temp = (*head_next)->next; // temp: d -> NULL
(*head_next)->next = (*head); // *head_next: c -> b -> a -> NULL
*head = *head_next; // *head: c -> b -> a -> NULL
*head_next = temp; // *head_next: d -> NULL
넷째 전화 : 재귀의
temp = (*head_next)->next; // temp: NULL
(*head_next)->next = (*head); // *head_next: d -> c -> b -> a -> NULL
*head = *head_next; // *head: d -> c -> b -> a -> NULL
*head_next = temp; // *head_next: NULL
끝.
어떻게 작동하는지 알아볼 가치가 없습니다. 그것은 나쁘고 지나치게 복잡한 코드입니다. – Gene