Skip to content

Commit d30720c

Browse files
committed
valgrind error should be fixed
1 parent c9ff6f4 commit d30720c

File tree

6 files changed

+21
-79
lines changed

6 files changed

+21
-79
lines changed

LinkedList.hpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ LinkedList<T>::LinkedList(const LinkedList &obj)
7878

7979

8080
while(givenCurrent != obj.tail){
81-
// cout << "creating node for node with dadta: " << givenCurrent->element << endl;
8281
givenCurrent = givenCurrent->next;
8382
if(givenCurrent != obj.tail){
8483
newCurrent->next = new Node<T>(givenCurrent->element, newCurrent, NULL);
@@ -88,7 +87,6 @@ LinkedList<T>::LinkedList(const LinkedList &obj)
8887
}
8988
tail->prev = newCurrent;
9089
newCurrent->next = tail;
91-
// cout << "tail element?: " << tail->element << endl;
9290
}
9391
else{
9492
head->next = tail;
@@ -105,7 +103,9 @@ LinkedList<T>::~LinkedList()
105103
tmp = head->next;
106104
delete head;
107105
head = tmp;
108-
}
106+
}
107+
tail = NULL;
108+
head = NULL;
109109
}
110110

111111
template<class T>
@@ -214,6 +214,7 @@ void LinkedList<T>::removeNode(Node<T> *node)
214214
node->next = NULL;
215215
node->prev = NULL;
216216
delete node;//CHECK: is it right way?
217+
node = NULL;
217218
}
218219
}
219220

@@ -224,22 +225,17 @@ void LinkedList<T>::removeAllNodes()
224225
{
225226
/* TODO */
226227
Node<T> *current = head->next;
227-
Node<T> *deletable = NULL;
228228

229229
while(current != tail){
230-
current->prev->next = current->next;
231-
current->next->prev = current->prev;
232-
233-
deletable = current;
234-
current = current->next;
230+
head->next = current->next;
231+
current->next->prev = head;
235232

236-
deletable->prev = NULL;
237-
deletable->next = NULL;
238-
delete deletable;
233+
delete current;
234+
current = head->next;
239235

240236
}
237+
current = NULL;
241238
tail->prev = head;
242-
head->next = tail;
243239
}
244240

245241
template<class T>

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CC=g++
2-
CFLAGS=-c -ansi -Wall -pedantic-errors -O0
2+
CFLAGS=-c -g -ansi -Wall -pedantic-errors -O0
33

44
all: linkedlist browser
55

WebHistory.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ void WebHistory::printHistory()
6969
WebHistory::WebHistory(std::string url, int timestamp)
7070
{
7171
// TODO
72+
73+
history.insertAtTheEnd(Tab(url, timestamp));
7274
}
7375

7476
WebHistory::~WebHistory()
7577
{
7678
// TODO
79+
7780
}
7881

7982
void WebHistory::insertInOrder(Node<Tab> *newPage)

browser

74.4 KB
Binary file not shown.

linkedlist

13.6 KB
Binary file not shown.

main_linkedlist.cpp

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,25 @@ using namespace std;
77
int main() {
88
LinkedList<int> llist;
99

10-
// Default constructor
11-
cout << "--- Default constructor start---" << endl;
12-
llist.printAllNodes();
13-
cout << "--- Default constructor start---" << endl;
14-
cout << "##########" << endl;
15-
cout << "--- Print all nodes start ---" << endl;
16-
llist.printAllNodes();
17-
cout << "---Print all nodes end ---" << endl;
18-
19-
20-
cout << "##########" << endl;
21-
cout << "--- Insert at the end accordingly [1, 7, 4, 444] start ---" << endl;
2210
llist.insertAtTheEnd(1);
2311
llist.insertAtTheEnd(7);
2412
llist.insertAtTheEnd(4);
2513
llist.insertAtTheEnd(444);
26-
cout << "--- Insert at the end accordingly [1, 7, 4, 444] end ---" << endl;
14+
llist.insertAtTheFront(444);
15+
llist.insertAtTheFront(444);
2716

28-
cout << "##########" << endl;
29-
cout << "--- Print all nodes start ---" << endl;
3017
llist.printAllNodes();
31-
cout << "---Print all nodes end ---" << endl;
32-
33-
cout << "##########" << endl;
34-
cout << "--- Get number of nodes start ---" << endl;
35-
cout << llist.getNumberOfNodes()<< endl;
36-
cout << "--- Get number of nodes end ---" << endl;
37-
38-
39-
cout << "##########" << endl;
40-
cout << "--- print Reversed start ---" << endl;
41-
llist.printReversed();
42-
cout << "--- print Reversed end ---" << endl;
4318

4419

45-
cout << "##########" << endl;
46-
cout << "--- copy constructor start ---" << endl;
47-
LinkedList<int> newList(llist);
48-
cout << "---- created newList deep copying llist start ----" << endl;
49-
newList.printAllNodes();
50-
cout << "---- created newList deep copying llist endl ----" << endl;
51-
cout << "---- Print all nodes of llist start ----" << endl;
52-
llist.printAllNodes();
53-
cout << "---Print all nodes of llist end ---" << endl;
54-
cout << "---- changing llist and newList each ---" << endl;
55-
llist.insertAtTheFront(6554);
56-
newList.insertAtTheEnd(6677);
57-
cout << "---- changed newList start ----" << endl;
58-
newList.printAllNodes();
59-
cout << "---- created newList endl ----" << endl;
60-
cout << "---- llist start ----" << endl;
61-
llist.printAllNodes();
62-
cout << "---- llist end ---" << endl;
63-
cout << "--- copy constructor end ---" << endl;
64-
65-
cout << "##########" << endl;
66-
cout << "--- Remove node with data 4 from llist start ---" << endl;
67-
llist.removeNode(llist.findNode(4));
68-
llist.printAllNodes();
69-
cout << "--- Remove node with data 4 from llist end ---" << endl;
20+
// LinkedList<int> newList = llist;
7021

7122

72-
cout << "##########" << endl;
73-
cout << "------remove all nodes of llist start --------" << endl;
74-
llist.removeAllNodes();
75-
cout << llist.getNumberOfNodes()<< endl;
76-
cout << "------remove all nodes of llist end --------" << endl;
77-
78-
cout << "##########" << endl;
79-
cout << "--- insert after given node start ---" << endl;
80-
cout << "--- before insertion 9000 after 4 ---" << endl;
81-
newList.printAllNodes();
82-
newList.insertAfterGivenNode(9000, newList.findNode(4));
83-
cout << "--- after insertion 9000 after 4 ---" << endl;
84-
newList.printAllNodes();
23+
// llist.removeNode(llist.findNode(7));
24+
// llist.removeAllNodes();
25+
// llist.findNode(3);
26+
// llist.printAllNodes();
8527

28+
8629

8730
return 0;
8831
}

0 commit comments

Comments
 (0)