-
Notifications
You must be signed in to change notification settings - Fork 4
/
linked_list_use.cpp
56 lines (45 loc) · 1.26 KB
/
linked_list_use.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.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 = insert(norse_gods, new Link("Odin"));
norse_gods = insert(norse_gods, new Link("Zeus"));
norse_gods = insert(norse_gods, new Link("Freia"));
Link* greek_gods = new Link("Hera");
greek_gods = insert(greek_gods, new Link("Athena"));
greek_gods = insert(greek_gods, new Link("Mars"));
greek_gods = insert(greek_gods, new Link("Poseidon"));
if (Link* p = find(greek_gods, "Mars"))
p->value = "Ares";
if (Link* p = find(norse_gods, "Zeus")) {
if (p == norse_gods) norse_gods = p->succ;
erase(p);
greek_gods = insert(greek_gods, 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->succ) cout << ", ";
}
cout << " }";
}
void destroy(Link* p) {
while (p) {
Link* next = p->succ;
delete p;
p = next;
}
}