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.
#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.
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.