Skip to content

Commit 3fe31c5

Browse files
committed
21 out of 21
1 parent d30720c commit 3fe31c5

File tree

7 files changed

+240
-35
lines changed

7 files changed

+240
-35
lines changed

.vscode/settings.json

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
{
22
"files.associations": {
3-
"iostream": "cpp"
3+
"iostream": "cpp",
4+
"cctype": "cpp",
5+
"clocale": "cpp",
6+
"cmath": "cpp",
7+
"cstdarg": "cpp",
8+
"cstddef": "cpp",
9+
"cstdio": "cpp",
10+
"cstdlib": "cpp",
11+
"cwchar": "cpp",
12+
"cwctype": "cpp",
13+
"array": "cpp",
14+
"atomic": "cpp",
15+
"*.tcc": "cpp",
16+
"cstdint": "cpp",
17+
"deque": "cpp",
18+
"list": "cpp",
19+
"unordered_map": "cpp",
20+
"vector": "cpp",
21+
"exception": "cpp",
22+
"fstream": "cpp",
23+
"functional": "cpp",
24+
"initializer_list": "cpp",
25+
"iosfwd": "cpp",
26+
"istream": "cpp",
27+
"limits": "cpp",
28+
"new": "cpp",
29+
"ostream": "cpp",
30+
"numeric": "cpp",
31+
"sstream": "cpp",
32+
"stdexcept": "cpp",
33+
"streambuf": "cpp",
34+
"system_error": "cpp",
35+
"cinttypes": "cpp",
36+
"tuple": "cpp",
37+
"type_traits": "cpp",
38+
"utility": "cpp",
39+
"typeinfo": "cpp",
40+
"algorithm": "cpp",
41+
"iterator": "cpp",
42+
"memory": "cpp",
43+
"memory_resource": "cpp",
44+
"optional": "cpp",
45+
"random": "cpp",
46+
"string": "cpp",
47+
"string_view": "cpp"
448
}
549
}

LinkedList.hpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,17 @@ template<class T>
200200
void LinkedList<T>::removeNode(Node<T> *node)
201201
{
202202
/* TODO */
203-
if(node != NULL){
204-
Node<T> *prev = node->prev;
205-
Node<T> *next = node->next;
206-
207-
if(prev != NULL){
208-
prev->next = next;
209-
}
210-
if(next != NULL){
211-
next->prev = prev;
203+
if(node != NULL && node != head && node != tail){
204+
Node<T> *current = head->next;
205+
while(current != tail){
206+
if(current == node){
207+
node->prev->next = node->next;
208+
node->next->prev = node->prev;
209+
delete node;
210+
break;
211+
}
212+
current = current->next;
212213
}
213-
214-
node->next = NULL;
215-
node->prev = NULL;
216-
delete node;//CHECK: is it right way?
217-
node = NULL;
218214
}
219215
}
220216

WebHistory.cpp

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,47 +69,175 @@ void WebHistory::printHistory()
6969
WebHistory::WebHistory(std::string url, int timestamp)
7070
{
7171
// TODO
72-
73-
history.insertAtTheEnd(Tab(url, timestamp));
72+
Tab newTab(url, timestamp);
73+
history.insertAtTheEnd(newTab);
74+
7475
}
7576

7677
WebHistory::~WebHistory()
7778
{
7879
// TODO
79-
80+
history.~LinkedList<Tab>();
8081
}
8182

8283
void WebHistory::insertInOrder(Node<Tab> *newPage)
8384
{
8485
// TODO
86+
Node<Tab> *head = history.getHead();
87+
Node<Tab> *tail = history.getTail();
88+
int controller = 0;
89+
90+
Node<Tab> *current = head->next;
91+
92+
if(history.isEmpty()){
93+
head->next = newPage;
94+
newPage->prev = head;
95+
96+
tail->prev = newPage;
97+
newPage->next = tail;
98+
controller = 99;
99+
}else{
100+
while(current != tail){
101+
if(newPage->element.getTimestamp() > current->element.getTimestamp()){
102+
newPage->next = current;
103+
newPage->prev = current->prev;
104+
105+
current->prev->next = newPage;
106+
current->prev = newPage;
107+
108+
controller = 99;
109+
110+
break;
111+
}
112+
current = current->next;
113+
}
114+
115+
}
116+
117+
if(controller != 99){
118+
newPage->next = tail;
119+
newPage->prev = tail->prev;
120+
121+
tail->prev->next = newPage;
122+
tail->prev = newPage;
123+
124+
}
125+
126+
85127
}
86128

87129
void WebHistory::goToPage(std::string url, int timestamp)
88130
{
89131
// TODO
132+
Tab newTab(url, timestamp);
133+
Node<Tab> *newPage = new Node<Tab>(newTab, NULL, NULL);
134+
insertInOrder(newPage);
90135
}
91136

92137
void WebHistory::clearHistory()
93138
{
94139
// TODO
140+
history.removeAllNodes();
95141
}
96142

97143
void WebHistory::clearHistory(int timestamp)
98144
{
99145
// TODO
146+
Node<Tab> *head = history.getHead();
147+
Node<Tab> *tail = history.getTail();
148+
Node<Tab> *current = head->next;
149+
150+
while(current != tail){
151+
if(current->element.getTimestamp() == timestamp){
152+
current->next->prev = current->prev;
153+
current->prev->next = current->next;
154+
155+
delete current;
156+
break;
157+
}
158+
current = current->next;
159+
}
100160
}
101161

102162
WebHistory WebHistory::operator+(const WebHistory &rhs) const
103163
{
104164
// TODO
165+
Node<Tab> *rhsHead = rhs.history.getHead();
166+
Node<Tab> *rhsTail = rhs.history.getTail();
167+
168+
Node<Tab> *lhsHead = history.getHead();
169+
Node<Tab> *lhsTail = history.getTail();
170+
171+
WebHistory newWebHistory;
172+
173+
if(rhsHead->next != rhsTail){
174+
Node<Tab> *current = rhsHead->next;
175+
176+
while(current != rhsTail){
177+
Node<Tab> *newPage = new Node<Tab>(current->element, NULL, NULL);
178+
newWebHistory.insertInOrder(newPage);
179+
current = current->next;
180+
}
181+
}
182+
183+
if(lhsHead->next != lhsTail){
184+
Node<Tab> *current = lhsHead->next;
185+
186+
while(current != lhsTail){
187+
Node<Tab> *newPage = new Node<Tab>(current->element, NULL, NULL);
188+
newWebHistory.insertInOrder(newPage);
189+
current = current->next;
190+
}
191+
}
192+
193+
return newWebHistory;
105194
}
106195

107196
int WebHistory::timesVisited(std::string pageName)
108197
{
109198
// BONUS
199+
int counter = 0;
200+
Node<Tab> *head = history.getHead();
201+
Node<Tab> *tail = history.getTail();
202+
203+
Node<Tab> *current = head->next;
204+
205+
while(current != tail){
206+
if(current->element.getWebSite() == pageName){
207+
counter++;
208+
}
209+
current = current->next;
210+
}
211+
212+
return counter;
110213
}
111214

112215
std::string WebHistory::mostVisited()
113216
{
114217
// BONUS
218+
Node<Tab> *head = history.getHead();
219+
Node<Tab> *tail = history.getTail();
220+
221+
Node<Tab> *current = head->next;
222+
223+
224+
int currentWebsiteVisitNumber = 0;
225+
int max=0;
226+
std::string websiteName;
227+
228+
// cout << "Before loop" << endl;
229+
230+
while(current != tail){
231+
// cout << "Inside loop: " << current->element.getUrl() << endl;
232+
currentWebsiteVisitNumber = timesVisited(current->element.getWebSite());
233+
if (max < currentWebsiteVisitNumber){
234+
max = currentWebsiteVisitNumber;
235+
websiteName = current->element.getWebSite();
236+
237+
// cout << current->element.getWebSite() << ":" << timesVisited(current->element.getWebSite()) << endl;
238+
}
239+
current = current->next;
240+
}
241+
242+
return websiteName;
115243
}

browser

-74.4 KB
Binary file not shown.

linkedlist

-13.6 KB
Binary file not shown.

main_browser.cpp

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,59 @@
33
#include "WebHistory.hpp"
44

55
int main() {
6-
WebHistory p1("www.youtube.com 4 | www.ceng.metu.edu.tr 8 | www.youtube.com/watch?v=4lYDblvvxE4 5 | https://www.youtube.com/watch?v=0lR1tt24mo4 9 | www.ntv.com.tr 10 | https://www.instagram.com/megadeth 1");
6+
7+
WebHistory wh1("www.google.com", 99);
8+
Tab nTab("akyptan.com", 5);
9+
10+
Node<Tab> *page = new Node<Tab>(nTab, NULL, NULL);
11+
wh1.insertInOrder(page);
12+
13+
WebHistory wh2("www.metu.edu.tr", 100);
14+
15+
WebHistory wh3 = wh2+wh1;
16+
wh3.printHistory();
17+
18+
WebHistory wh4("www.metu.edu.tr", 1);
19+
WebHistory wh5("www.metu.edu.tr", 2);
20+
WebHistory wh6("www.metu.edu.tr", 3);
21+
WebHistory wh7("www.metu.edu.tr", 11);
22+
23+
wh3 = wh3+wh4;
24+
wh3 = wh3+wh5;
25+
wh3 = wh3+wh6;
26+
wh3 = wh3+wh7;
27+
28+
29+
wh3.printHistory();
30+
31+
cout << "most visited page: " << wh3.mostVisited() << endl;
32+
33+
34+
35+
36+
37+
// Default given inputs
38+
//WebHistory p1("www.youtube.com 4 | www.ceng.metu.edu.tr 8 | www.youtube.com/watch?v=4lYDblvvxE4 5 | https://www.youtube.com/watch?v=0lR1tt24mo4 9 | www.ntv.com.tr 10 | https://www.instagram.com/megadeth 1");
739

8-
WebHistory p2("www.instagram.com 1 | www.facebook.com 7 | https://www.youtube.com/watch?v=s9pZxnJAwQI 4 | https://www.youtube.com/watch?v=-gDinVAmtA0 8 | www.ntv.com.tr 9 | https://www.instagram.com/pinkfloyd/ 99");
40+
// WebHistory p2("www.instagram.com 1 | www.facebook.com 7 | https://www.youtube.com/watch?v=s9pZxnJAwQI 4 | https://www.youtube.com/watch?v=-gDinVAmtA0 8 | www.ntv.com.tr 9 | https://www.instagram.com/pinkfloyd/ 99");
941

10-
p1.printHistory();
11-
std::cout << std::endl;
12-
p2.printHistory();
42+
//p1.printHistory();
43+
//std::cout << std::endl;
44+
// p2.printHistory();
1345

14-
p1.goToPage("https://www.youtube.com/watch?v=7v8zxoEoA_Q", 91);
15-
p2.goToPage("https://www.youtube.com/watch?v=dhpwBPuWyKg", 6);
46+
// p1.goToPage("https://www.youtube.com/watch?v=7v8zxoEoA_Q", 91);
47+
// p2.goToPage("https://www.youtube.com/watch?v=dhpwBPuWyKg", 6);
1648

17-
p1.printHistory();
18-
std::cout << std::endl;
19-
p2.printHistory();
49+
// p1.printHistory();
50+
// std::cout << std::endl;
51+
// p2.printHistory();
2052

21-
p1.clearHistory();
22-
p2.clearHistory();
53+
// p1.clearHistory();
54+
// p2.clearHistory();
2355

24-
p1.printHistory();
25-
std::cout << std::endl;
26-
p2.printHistory();
56+
// p1.printHistory();
57+
// std::cout << std::endl;
58+
// p2.printHistory();
2759

2860
return 0;
2961
}

main_linkedlist.cpp

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

10-
llist.insertAtTheEnd(1);
10+
1111
llist.insertAtTheEnd(7);
1212
llist.insertAtTheEnd(4);
1313
llist.insertAtTheEnd(444);
1414
llist.insertAtTheFront(444);
1515
llist.insertAtTheFront(444);
1616

17+
Node<int> *dNode = llist.findNode(4);
18+
1719
llist.printAllNodes();
1820

21+
llist.removeNode(dNode);
22+
llist.removeNode(dNode);
1923

2024
// LinkedList<int> newList = llist;
2125

2226

23-
// llist.removeNode(llist.findNode(7));
27+
2428
// llist.removeAllNodes();
29+
// llist.printAllNodes();
2530
// llist.findNode(3);
2631
// llist.printAllNodes();
2732

0 commit comments

Comments
 (0)