-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPP_0.cpp
More file actions
158 lines (129 loc) · 4.65 KB
/
Copy pathPP_0.cpp
File metadata and controls
158 lines (129 loc) · 4.65 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
// Website node definition
class Website{
public:
std::string name; // Name of the website
std::string url; // URL of the website
std::string type; // Type of resource provided by the website
Website(){}; // Constructor with no arguments
Website(std::string new_name, std::string new_url, std::string new_type){ // Constructor with arguments
name = new_name;
url = new_url;
type = new_type;
}
};
// Node definition
template <typename T>
class Node{
private:
T data;
Node<T> *next; // Next website in the list
Node<T> *back; // Previous website in the list
public:
Node(){}; // No argument constructor
Node(T new_data){ // Constructor when website is known
data = new_data;
next = nullptr;
back = nullptr;
}
template <typename E> friend class LinkedList;
};
// Linked List definition
template <typename T>
class LinkedList{
private:
Node<T> *header;
Node<T> *trailer;
int size;
public:
LinkedList(){ // Linked List constructor
header = new Node<T>;
trailer = new Node<T>;
size = 0;
header->next = trailer;
trailer->back = header;
}
~LinkedList(){ // Linked List destructor
while(size != 0){
removeFront();
}
delete header;
delete trailer;
}
void remove(Node<T> *toRemove){ // Remove a given website from the list
toRemove->back->next = toRemove->next;
toRemove->next->back = toRemove->back;
}
void removeFront(){ // Remove the first website
remove(header->next);
size--;
return;
}
void removeBack(){ // Remove the last website
remove(trailer->back);
size--;
return;
}
void add(Node<T> *beforeMe, T data){ // General add function
// Create new website
Node<T> *newNode = new Node<T>(data);
// Add website to linked list
beforeMe->back->next = newNode;
newNode->back = beforeMe->back;
beforeMe->back = newNode;
newNode->next = beforeMe;
return;
}
void addFront(T data){ // Adding to the front of the list
add(header->next,data);
size++;
return;
}
void addBack(T data){ // Adding to the end of the list
add(trailer,data);
size++;
return;
}
void printList(){ // Function to print entire list
Node<T> *curNode = header->next;
std::cout << "\n";
std::cout << "PRINTING LIST... \n\n";
std::cout.width(20); std::cout << std::left << "Name";
std::cout.width(80); std::cout << std::left << "URL";
std::cout.width(20); std::cout << std::left << "Resource type";
std::cout << "\n===========================================================================================================================\n";
while (curNode != trailer){
std::cout.width(20); std::cout << std::left << curNode->data.name;
std::cout.width(80); std::cout << std::left << curNode->data.url;
std::cout.width(20); std::cout << std::left << curNode->data.type;
std::cout << "\n";
curNode = curNode->next;
}
std::cout << "\n";
}
};
// Main function
int main(){
// Testing all functions of the list
/*
Website site = Website("geeksforgeeks","https://www.geeksforgeeks.org/c-plus-plus/?ref=shm","Guide");
Node<Website> node = Node<Website>(site);
LinkedList<Website> list;
list.addFront(site);
list.printList();
site = Website("w3schools", "https://www.w3schools.com/cpp/" ,"Guide");
list.addFront(site);
list.printList();
list.removeFront();
list.printList();
list.addBack(site);
list.printList();
list.removeBack();
list.printList();
*/
return EXIT_SUCCESS;
}