【數據結構】44_遞歸的思想與應用 (中)

單鏈表的轉置

image.png

image.png

編程實驗:單鏈表的轉置

#include <iostream>

using namespace std;

struct Node
{
    int value;
    Node *next;
};

Node *create_list(int v, int len)
{
    Node *ret = nullptr;
    Node *slider = nullptr;

    for (int i=0; i<len; ++i)
    {
        Node *n = new Node();

        n->value = v++;
        n->next = nullptr;

        if (slider == nullptr)
        {
            slider = n;
            ret = n;
        }
        else
        {
            slider->next = n;
            slider = n;
        }
    }

    return ret;
};

void destroy_list(Node *list)
{
    while (list)
    {
        Node *del = list;

        list = list->next;

        delete del;
    }
}

void print_list(Node *list)
{
    while (list)
    {
        cout << list->value << "->";
        list = list->next;
    }

    cout << "NULL" << endl;
}

Node *reverse(Node *list)
{
    if ((list == nullptr) || (list->next == nullptr))
    {
        return list;
    }
    else
    {
        Node *guard = list->next;
        Node *ret = reverse(list->next);
        guard->next = list;
        list->next = nullptr;

        return ret;
    }
}

int main()
{
    Node *list = create_list(1, 5);

    print_list(list);

    list = reverse(list);

    print_list(list);

    destroy_list(list);

    return 0;
}

輸出:ios

1->2->3->4->5->NULL
5->4->3->2->1->NULL

單向排序鏈表的合併

image.png

image.png

編程實驗:單向排序鏈表的合併

#include <iostream>

using namespace std;

struct Node
{
    int value;
    Node *next;
};

Node *create_list(int v, int len)
{
    Node *ret = nullptr;
    Node *slider = nullptr;

    for (int i=0; i<len; ++i)
    {
        Node *n = new Node();

        n->value = v++;
        n->next = nullptr;

        if (slider == nullptr)
        {
            slider = n;
            ret = n;
        }
        else
        {
            slider->next = n;
            slider = n;
        }
    }

    return ret;
};

void destroy_list(Node *list)
{
    while (list)
    {
        Node *del = list;

        list = list->next;

        delete del;
    }
}

void print_list(Node *list)
{
    while (list)
    {
        cout << list->value << "->";
        list = list->next;
    }

    cout << "NULL" << endl;
}

Node *merge(Node *list1, Node *list2)
{
    if (list1 == nullptr)
    {
        return list2;
    }
    else if (list2 == nullptr)
    {
        return list1;
    }
    else
    {
        if (list1->value < list2->value)
        {
            return (list1->next = merge(list1->next, list2), list1);
        }
        else
        {
            return (list2->next = merge(list1, list2->next), list2);
        }
    }
}

int main()
{
    Node *list1 = create_list(1, 5);
    Node *list2 = create_list(2, 6);

    print_list(list1);
    print_list(list2);

    Node *list = merge(list1, list2);

    print_list(list);

    destroy_list(list);

    return 0;
}

輸出:編程

1->2->3->4->5->NULL
2->3->4->5->6->7->NULL
1->2->2->3->3->4->4->5->5->6->7->NULL

漢諾塔問題

將木塊藉助 B 柱由 A 柱移動到 C 柱
每次只能移動一個木塊
只能出現小木塊在大木塊之上

image.png

漢諾塔問題分解

  • 將 n-1 個木塊藉助 C 柱由 A 柱移動到 B 柱
  • 將最底層的惟一木塊直接必定到 C 柱
  • 將 n-1 個木塊藉助 A 柱移動到 C 柱

編程實驗:漢諾塔問題求解

#include <iostream>

using namespace std;

void hanoiTower(int n, char a, char b, char c)
{
    if (n == 1)
    {
        cout << a << "->" << c << endl;
    }
    else
    {
        hanoiTower(n-1, a, c, b);
        hanoiTower(1, a, b, c);
        hanoiTower(n-1, b, a, c);
    }
}

int main()
{
    hanoiTower(3, 'a', 'b', 'c');

    return 0;
}

輸出:ide

a->c
a->b
c->b
a->c
b->a
b->c
a->c

全排列問題

image.png

編程實驗:全排列的遞歸解法

#include <iostream>
#include <cstring>

using namespace std;

void permutation(char *s, char *e)
{
    if (*s == '\0')
    {
        cout << e << endl;
    }
    else
    {
        size_t len = strlen(s);
        for (size_t i=0; i<len; ++i)
        {
            if ((i == 0) || (s[0] != s[i]))
            {
                swap(s[0], s[i]);
                permutation(s + 1, e);
                swap(s[0], s[i]);
            }
        }
    }
}

int main()
{
    char s1[] = "abc";
    permutation(s1, s1);

    cout << "----------" << endl;
    char s2[] = "aac";
    permutation(s2, s2);

    return 0;
}

輸出:spa

abc
acb
bac
bca
cba
cab
----------
aac
aca
caa

以上內容整理於狄泰軟件學院系列課程,請你們保護原創!code

相關文章
相關標籤/搜索