Linked list operator and clear function error?

I am having an error in my linked list in which once I use the overloaded + operator to add link lists together it kind of uh doesn't work, sort of? By default it gives me a runtime error due to unable to read certain memory when trying to clear it because of the clearAll function it calls. However even when I remove the clearAll function it works although not liked intented. This is because list1 is equal to list3. Although they were added together it wasn't the intended effect. Also I think this is because linked list are pointers and thus they are pointing to the same things I believe?

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
#include <string>
#include <iostream>
#include "save.h"
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template<typename STORAGE> class linkedlist
{
private:
	struct component {
		component* next;
		STORAGE info;
	};
	component* head;
	component* tail;
	std::string file;
	short unsigned int items_amount;
	bool saveInfo;

public:
	linkedlist();
	linkedlist(std::string txt);
	~linkedlist();
	STORAGE use(unsigned int index);
	STORAGE ret_info(unsigned int index);
	void insertItem(unsigned int index, STORAGE item);
	void insertItem(STORAGE arg_info[], int size, int start);

	void pop_front();
	void pop_end();
	void erase(unsigned int start, unsigned int end);
	void removeItem(unsigned int index);

	void addItem_end(STORAGE item);
	void addItem_front(STORAGE item);
	void addItem_end(STORAGE arg_info[], int size);
	void addItem_front(STORAGE arg_info[], int size);

	void save_items(std::string txt);

	int size() const { return items_amount; }
	int length() const { return items_amount; }
	bool is_empty() const;

	void remove(STORAGE type);
	void swap(linkedlist<STORAGE>& swap);
	void swap(linkedlist<STORAGE>* swap);
	void resize(unsigned int new_size);
	void clearAll();
	void sort();
	linkedlist<STORAGE> operator+(const linkedlist<STORAGE>& list) {
		linkedlist<STORAGE> copy; //MAKE A COPY TO RETURN
		copy.head = this->head; //SET HEAD = TO CURRENT HEAD
		copy.items_amount = this->items_amount + list.items_amount;//ADD THE ITEMS AMOUNT TOGETHER
		copy.saveInfo = this->saveInfo;
		copy.file = this->file;
		this->tail->next = list.head;
		this->tail = list.tail;
		copy.tail = this->tail;
		return copy;
	}
friend std::ostream& operator<<(std::ostream& output, const linkedlist<STORAGE>& list) {
		component* cycle = list.head;
		do { 
			output << cycle->info;
			cycle = cycle->next;
		} while (cycle->next != nullptr);
		output << cycle->info;
		return output;
	}
};
#endif
template<typename STORAGE>
inline void linkedlist<STORAGE>::addItem_end(STORAGE item) {
	component* link = new component;
	items_amount++;
	if (head == nullptr) {
		head = link;
		tail = link;
		head->info = item;
		head->next = nullptr;
	}
	else {
		tail->next = link;
		tail = link;
		tail->next = nullptr;
		tail->info = item;
	}
}
template<typename STORAGE>
inline void linkedlist<STORAGE>::addItem_front(STORAGE item)
{
	items_amount++;
	component* link = new component;
	link->info = item;
	link->next = head;
	head = link;
}
template<typename STORAGE>
inline bool linkedlist<STORAGE>::is_empty() const {
	if (head != nullptr) {
		return false;
	}
	else {
		return true;
	}
}
template<typename STORAGE>
inline void linkedlist<STORAGE>::insertItem(unsigned int index, STORAGE item) {
	try {
		if (index > items_amount) { throw std::string("ERROR CANNOT INSERT AN ITEM OUTSIDE ARRAY BOUNDS"); }
		items_amount++;
		component* link = new component;
		component* cycle = head;
		for (int argIN = 0; argIN < (index); argIN++) {
			cycle = cycle->next;
		}
		link->info = item;
		link->next = cycle->next;
		cycle->next = link;
	}
	catch (std::string& exception) {
		std::cerr << exception << '\n';
	}
}
template<typename STORAGE>
inline void linkedlist<STORAGE>::clearAll() {
	if (is_empty() != true) { 
		component* de_allocate;
		while (head->next != nullptr) {
			de_allocate = head;
			head = head->next; //GETTING ERRORS IN HERE.
			delete head;
		}
		items_amount = 0;
		tail = nullptr;
		head = nullptr;
	}
}

Of course there are more functions but I couldn't include them all, plus some of them are being worked on. Anwyas here is my main.cpp in which I am testing it.

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
#include <iostream>
#include "linkedlist.h"

using std::cin;
using std::cout;
using std::string;
using std::endl;
int main() {
        char response;
	linkedlist<int> list, list2, list3;
	for (int i = 0; i < 10; i++) {
		list.addItem_end(i);
		list.addItem_front(i);
		list2.addItem_end(i);
		list2.addItem_front(i);
	}
	cout << list << endl;
	cout << list2 << endl;
	list.insertItem(1, 5);
	list2.insertItem(1, 5);
	list3 = list + list2; //THROWS THE ERROR
	cout << "1st: " << list << endl;
	cout << "2nd: " << list2 << endl;
	cout << "3rd: "<< list3 << endl; 
        cin >> response;
}
Last edited on
Also I think this is because linked list are pointers and thus they are pointing to the same things I believe?
Yes, if you copy pointer you make a shallow copy. Two or more pointer pointing to the same object is bad because as with the clearAll() function the objects might be deleted but nonetheless a pointer to the invalid data exists.

What you need is deep copy:

http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

Further more: On line 21 you use the default operator=. You need to implement that operator in order to deep copy the content.
Topic archived. No new replies allowed.