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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
// Osman Zakir
// 9 / 7 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 17 Section 17.9.4
// Doubly Linked Lists
#include <iostream>
#include "../../cust_std_lib_facilities.h"
struct God
{
std::string m_name;
std::string m_myth;
std::string m_vehicle;
std::string m_weapon;
God(const std::string &name, const std::string &myth, const std::string &vehicle, const std::string &weapon)
: m_name{ name }, m_myth{ myth }, m_vehicle{ vehicle }, m_weapon{ weapon } { }
};
class Link
{
public:
std::string name;
std::string myth;
std::string vehicle;
std::string weapon;
God god;
Link(const God &god, Link *p = nullptr, Link *s = nullptr);
Link *insert(Link *n); // insert n before this object
Link *add(Link *n); // insert n after this object
Link *erase(); // remove this object from list
Link *find(const God &god); // find values in list
const Link *find(const God &god) const; // find values in const list
Link *advance(int n) const; // advance n positions in list
Link *next() const { return succ; }
Link *previous() const { return prev; }
private:
Link *prev;
Link *succ;
};
void print_all(Link *p);
int main()
{
Link *norse_gods = new Link{ God{ "Thor", "Norse", "Chariot pulled by goats Tanngrisnir and Tanngnjostr", "Mjolnir" } };
norse_gods = norse_gods->insert(new Link{ God{ "Odin", "Norse", "Eight-legged flying horse called Sleipnir", "Spear called Gungnir" } });
norse_gods = norse_gods->insert(new Link{ God{ "Zeus", "Greek", "", "Lightning" } });
norse_gods = norse_gods->insert(new Link{ God{ "Freyja", "Norse", "Chariot pulled by two cats", "Magic called seior" } });
Link *greek_gods = new Link{ God{ "Hera", "Greek", "", "Her cleverness" } };
greek_gods = greek_gods->insert(new Link{ God{"Athena", "Greek", "", "Two swords and a spear; is allowed to use Zeus's thunderbolt"} });
greek_gods = greek_gods->insert(new Link{ God{ "Mars", "Roman", "", "a spear stained in blood" } });
greek_gods = greek_gods->insert(new Link{ God{"Poseidon", "Greek", "", "Three-pronged spear called Trident"} });
Link* p = greek_gods->find({ "Mars", "Roman", "", "a spear stained in blood" });
if (p)
{
p->name = "Ares";
p->myth = "Greek";
p->vehicle = "";
p->weapon = "a spear stained in blood";
}
Link* p2 = norse_gods->find({ "Zeus", "Greek", "", "Lightning" });
if (p2)
{
if (p2 == norse_gods)
{
norse_gods = p2->next();
}
p2->erase();
greek_gods = greek_gods->insert(p2);
}
print_all(norse_gods);
std::cout << '\n';
print_all(greek_gods);
std::cout << '\n';
keep_window_open();
}
Link::Link(const God &god, Link *p, Link *s)
: god{ god }, prev{ p }, succ{ s }
{
}
Link *Link::insert(Link *n)
{
if (n == nullptr)
{
return this;
}
n->succ = this;
if (prev)
{
prev->succ = n;
}
n->prev = prev;
prev = n;
return n;
}
Link *Link::add(Link *n)
{
if (n == nullptr)
{
return this;
}
n->prev = this;
if (succ)
{
succ->prev = n;
}
n->succ = succ;
succ = n;
return n;
}
Link *Link::erase()
{
Link *trav = this;
if (trav == nullptr)
{
return nullptr;
}
if (trav->succ)
{
trav->succ->prev = trav->prev;
}
if (trav->prev)
{
trav->prev->succ = trav->succ;
}
return trav->succ;
}
Link *Link::find(const God &god)
{
Link *trav = this;
while (trav != nullptr)
{
if (trav->god.m_name == god.m_name)
{
return trav;
}
trav = trav->succ;
}
return nullptr;
}
const Link *Link::find(const God &god) const
{
const Link *c_trav = this; // const pointer
Link *nc_trav = const_cast<Link*>(c_trav); // non-const pointer
while (c_trav != nullptr && nc_trav != nullptr)
{
if (c_trav->god.m_name == god.m_name)
{
return c_trav;
}
nc_trav = nc_trav->succ;
}
return nullptr;
}
Link *Link::advance(int n) const
{
Link *trav = const_cast<Link *>(this);
if (trav == nullptr)
{
return nullptr;
}
if (n > 0)
{
while (n--)
{
if (trav->succ == nullptr)
{
return nullptr;
}
trav = trav->succ;
}
}
else if (n < 0)
{
while (n++)
{
if (trav->prev == nullptr)
{
return nullptr;
}
trav = trav->prev;
}
}
return trav;
}
void print_all(Link *p)
{
std::cout << "{ ";
while (p)
{
std::cout << "Name: " << p->name << '\n'
<< "Myth: " << p->myth << '\n';
if (p->vehicle != "")
{
std::cout << "Vehicle: " << p->vehicle << '\n';
}
else
{
std::cout << "Vehicle: N/A\n";
}
std::cout << "Weapon: " << p->weapon << '\n';
if ((p = p->next()))
{
std::cout << "\n";
}
}
std::cout << " }";
}
|