So I've got to implement doubly linked list. I've got constructor, destructor, many other methods like inserting too. But as I would like to apply to The Rule of Three
I would need to implement copy assignment operator and a copy constructor. I've seen many solutions on the internet, but all of them required std::swap
and std::move
or std::copy
. I cannot use any of those as all my algorithms have to be done without STL
or boost::
. I know I have to start like this:
list & list::operator=(const list & that)
{
if (this != &that)
{
}
return *this;
}
Then I would to something like this:
if (this != &that)
{
this->clear();
node *t = this->head;
node *o = that.head;
while (o->next)
{
t.append(o.value);
o = o.next;
t = t->next
}
}
return *this;
@Edit:
void menu_list()
{
char opt;
int *t;
list myList = list();
do {
display_menu("--- LISTA ---");
opt = _getche();
std::cout << '\n';
switch (opt) {
case '1': //reading a list from a text file
{
int elements;
std::ifstream file_stream;//
file_stream.open("data.txt"); //
if (!file_stream.good()) //
{
std::cout << "Nie udalo sie wczytac pliku!" << '\n';
break;
}
file_stream >> elements; //
t = new int[elements]; // stworzenie listy
for (int i = 0; i<elements; i++)
{
file_stream >> *(t + i); //
}
myList = list(t, elements); //zainicjalizowanie listy
delete[] t; //
std::cout << "\nObecny stan listy: "; std::cout << myList.to_string() << '\n';
break;
}
case '2': //deleting an element from a list
{
if (myList.is_empty())
{
std::cout << "Lista jest pusta! \n"; break;
}
int index;
std::cout << "Podaj indeks elementu do usuniecia: ";
scanf_s("%d", &index);
if (myList.size() <= index)
{
std::cout << "Lista nie ma tylu elementow!\n"; break;
}
else if (index < 0)
{
std::cout << "Indeks nie moze byc mniejszy od 0!\n"; break;
}
myList.remove(index);
std::cout << "\nObecny stan listy: "; std::cout << myList.to_string() << '\n';
break;
}
list.cpp
(implementation of list header):
#include "list.h"
#include <ctime>
list::list() : head(nullptr), tail(nullptr), n(0)
{
}
list::list(int* t, int n) : head(nullptr), tail(nullptr), n(0)
{
if (!this->is_empty())
{
this->clear();
}
for (int i = 0; i < n; i++)
{
this->append(t[i]);
}
}
list::list(const list& obj)
{
}
clear()
is a method that clears the whole list (and head
and tail
= nullptr
) and append()
is a method that appends a value at the end of a list.
Now, I guess this is wrong, but I don't have any clue how to make it work and I'd like to ask you guys how can I do it