Singly linked list --> Doubly linked list

Jan 2, 2021 at 9:13pm
Hello. First of all, I would like to apologize for my poor English. I am a beginner at C++ and programming in general and I have some problems with "translating" a singly linked list into a doubly linked list. This is what the program does:

'a': a new element gets inserted at the beginning of the list
'e‘: a new element gets inserted at the end of the list
's‘: the entire list gets sorted in ascending order with BubbleSort
'i‘: a new element gets inserted sorted into an already available list
'q ‘: exit the program

In the following, you'll find my code for the singly linked list. I think it should work properly, but I'm not so sure. Maybe you'll find something I can improve.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdlib>
#include <ctime>
#include <iostream>

struct Node {
    int value;
    Node *next;

    Node(int value, Node *next = nullptr)
    : value{ value },
      next{ next }
    {}

    Node(Node const&) = delete;
    ~Node() { delete next; }
};

void printList(Node *node)
{
    for(; node; node = node->next)
        std::cout << node->value << ' ';
}

void InsertAtBegin(Node **root, int value)
{
    *root = new Node{ value, *root };
    printList(*root);
}

void InsertAtEnd(Node **root, int value)
{
    if (!*root) {
        *root = new Node{ value };
        return;
    }

    auto last = *root;
    while (last->next)
        last = last->next;

    last->next = new Node{ value };
    printList(*root);
}

void insertSorted(Node **root, int value)
{
    if (!*root) {
        InsertAtBegin(root, value);
        return;
    }

    auto current = *root;
    while (current->next && current->next->value < value)
        current = current->next;

    current->next = new Node{ value, current->next };
    printList(*root);
}

void bubbleSort(Node *root)
{
    for (auto a = root; a; a = a->next) {
        for (auto b = a->next; b; b = b->next) {
            if (a->value > b->value) {
                auto temp = a->value;
                a->value = b->value;
                b->value = temp;
            }
        }
    }
    printList(root);
}

int main()
{
    Node* root = nullptr;
    std::srand(static_cast<unsigned>(std::time(nullptr)));

    char choice;
    do {
        std::cout << "\na\ne\ns\ni\nq\nYour choice: ";
        std::cin >> choice;
        int value = 1 + rand() % 999;
        switch (choice) {
        case 'a':
            InsertAtBegin(&root, value);
            break;
        case 'e':
            InsertAtEnd(&root, value);
            break;
        case 's':
            bubbleSort(root);
            break;
        case 'i':
            insertSorted(&root, value);
            break;
        }
    } while (choice != 'q');

    delete root;
}


In the next step I want to "translate" it into a doubly formed list. But honestly, I do not really know how to do it and I can't find a good tutorial that fits to my knowledge. Maybe you have some usefull tips, or a link to a good tutorial for me or maybe you could even do it with me step by step. I'm thankful for what I get.
Last edited on Jan 2, 2021 at 9:17pm
Jan 3, 2021 at 1:45am
all you have to do is, every time you move, insert, delete, etc a node into the list, you have to assign a second pointer that goes the other direction, apart from head which has no previous (set to nullptr) like tail has no next.

you have something like
new temp node
temp.next = null
tail.next = temp; //inserted at the end

instead you have
new temp node
temp.next = null
temp.previous = tail;
tail.next = temp; //inserted at the end

you also track both head and tail, instead of only head. Because the point of double linkage is to iterate in reverse sometimes.
Last edited on Jan 3, 2021 at 1:46am
Topic archived. No new replies allowed.