I need help, because I've been trying to solve this all day, but I really don't know how to do it. The problem is that I made a copy- constructor; it copies well, but when I want to modify the copy by adding an element at the end it doesn't work, the elements aren't printed.
It works when I add an element at the beginning and when I try to add an element by using the method add_at
, it add the element in the both lists. I really don't know where the problem is; is it in the constructor or in the methods?
You can think that my code is strange but I had to make add methods like this...
Here's the code:
.h file
class CList
{
SElement* first;
SElement* last;
int lenght;
int value;
public:
CList();
CList(CList const& clist);
~CList();
// add methods
void add(int value);
void add_first(int value);
void add_at(int at, int value);
void addObject(void* object);
void addObject_first(void* object);
void addObject_at(int at, void* object);
// delete methods
void del_last();
void del_first();
void del_index(int index);
void del_element(void* value);
void del_ielement(int& value);
// getery
int iget_lenght();
int iget_element(int const index);
SElement* sget_element(int index);
void* get_element(int index);
void set_element(int const index, int& value);
void pop();
void push(int value);
void printCList();
private:
void priv_push(int & value);
};
methods concerned in the .cpp
CList::CList()
{
first = new SElement;
last = new SElement;
first = last = NULL;
lenght = 0;
value = 0;
}
CList::CList(CList const& clistcopy)
{
this->lenght = clistcopy.lenght;
this->value = clistcopy.value;
first = new SElement;
last = new SElement;
// pointer = last = NULL;
*first = *clistcopy.first;
*last = *clistcopy.last;
}
CList::~CList()
{
SElement* el;
while (first)
{
el = first->next;
delete first;
first = el;
}
delete last;
}
// ADD FIRST
void CList::addObject_first(void* object)
{
SElement* el = new SElement;
el->next = first;
el->previous = NULL;
el->object = object;
if (first != NULL)
first->previous = el;
first = el;
if (!last) last = first;
lenght++;
}
void CList::add_first(int wartosc)
{
value = wartosc;
int* wsk = &value;
int* pnt = new int;
*pnt = *wsk;
addObject_first(pnt);
}
// ADD LAST
void CList::addObject(void* object)
{
if (!last)
{
SElement* el = new SElement;
el->object = object;
el->next = NULL;
el->previous = NULL;
first = last = el;
}
else
{
SElement* el = last;
el->next = new SElement;
el = el->next;
el->next = NULL;
el->previous = last;
el->object = object;
last = el;
el = NULL;
}
}
void CList::add(int wartosc)
{
value = wartosc;
int* wsko = &value;
int* pnt = new int;
*pnt = *wsko;
addObject(pnt);
}
// ADD AT
void CList::addObject_at(int index, void* value)
{
SElement* el2 = new SElement;
if (index - 1 == 0)
addObject_first(value);
else if (index - 1 == lenght)
addObject(value);
else if (index < 0 || index > lenght)
cout << "blad : nie mozesz dodac na indeks poza lista" << endl;
else
{
SElement* el = sget_element(index - 1);
el2->next = el->next;
el2->previous = el;
el2->object = value;
el->next = el2;
el->next->previous = el2;
if (!(el2->next)) last = el2;
lenght++;
}
}
void CList::add_at(int at, int wartosc)
{
value = wartosc;
int* wsko = &value;
int* pnt = new int;
*pnt = *wsko;
addObject_at(at, pnt);
}
void CList::printCList()
{
if (first == NULL)
cout << "Lista jest pusta" << endl;
else
{
SElement* el;
el = first;
while (el->next != NULL)
{
cout << el->get_value();
cout << " ";
el = el->next;
}
cout << el->get_value();
}
cout << endl;
}
the main
int main()
{
CList list = CList();
list.add_first(1);
list.add_first(0);
list.add_first(2);
list.add_first(4);
list.add(5);
list.add(10);
CList list2 = CList(list);
list.printCList();
list2.printCList();
list2.add(100);
list2.add(150);
cout << list.iget_lenght() << endl;
cout << list2.iget_lenght() << endl;
list.printCList();
list2.printCList();
system("pause");
return 0;
}