Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node {
- int value;
- int next;
- };
- void print_list(Node list[], int index = 0) {
- if(index == -1) return;
- std::cout << list[index].value << " ";
- print_list(list, list[index].next);
- }
- int store_list(Node list[], int copy[], int index = 0, int iteration = 0) {
- if(index == -1) return iteration;
- copy[iteration] = list[index].value;
- return store_list(list, copy, list[index].next, iteration + 1);
- }
- void write_on_list(Node list[], int copy[], int index, int iteration) {
- if(index == -1 || iteration == -1) return;
- list[index].value = copy[iteration - 1];
- write_on_list(list, copy, list[index].next, iteration - 1);
- }
- void reverse_list(Node list[], int length) {
- int* copy = new int[length];
- int real_len = store_list(list, copy);
- write_on_list(list, copy, 0, real_len);
- }
- int main(void) {
- Node list[5] = {
- {1, 4}, { 9, -1 }, {3, 1}, {}, {5, 2},
- };
- print_list(list);
- reverse_list(list, 5);
- print_list(list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement