-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinked_list_use_v2.cpp
56 lines (45 loc) · 1.27 KB
/
linked_list_use_v2.cpp
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
#include <iostream>
#include "doubly_linked_list_v2.h"
using namespace std;
void print_all(Link* p);
void destroy(Link* p);
// linked list example: print names of Norse gods and Greek gods
int main() {
Link* norse_gods = new Link("Thor");
norse_gods = norse_gods->insert(new Link("Odin"));
norse_gods = norse_gods->insert(new Link("Zeus"));
norse_gods = norse_gods->insert(new Link("Freia"));
Link* greek_gods = new Link("Hera");
greek_gods = greek_gods->insert(new Link("Athena"));
greek_gods = greek_gods->insert(new Link("Mars"));
greek_gods = greek_gods->insert(new Link("Poseidon"));
if (Link* p = greek_gods->find("Mars"))
p->value = "Ares";
if (Link* p = norse_gods->find("Zeus")) {
if (p == norse_gods) norse_gods = p->next();
p->erase();
greek_gods = greek_gods->insert(p);
}
print_all(norse_gods);
cout << '\n';
print_all(greek_gods);
cout << '\n';
destroy(norse_gods);
destroy(greek_gods);
return 0;
}
void print_all(Link* p) {
cout << "{ ";
while (p) {
cout << p->value;
if (p = p->next()) cout << ", ";
}
cout << " }";
}
void destroy(Link* p) {
while (p) {
Link* next = p->next();
delete p;
p = next;
}
}