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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
// Osman Zakir
// 9 / 7 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 17 Exercises 11-13
#include <iostream>
#include "../../cust_std_lib_facilities.h"
struct God
{
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 } { }
const std::string &name() const { return m_name; }
const std::string &myth() const { return m_myth; }
const std::string &vehicle() const { return m_vehicle; }
const std::string &weapon() const { return m_weapon; }
void set_name(const std::string name) { m_name = name; }
void set_myth(const std::string myth) { m_myth = myth; }
void set_vehicle(const std::string vehicle) { m_vehicle = vehicle; }
void set_weapon(const std::string weapon) { m_weapon = weapon; }
private:
std::string m_name;
std::string m_myth;
std::string m_vehicle;
std::string m_weapon;
};
class Link
{
public:
Link(const God &god, Link *p = nullptr, Link *s = nullptr)
: m_god{ god }, m_prev{ p }, m_succ{ s } { }
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 std::string &name); // find values in list
const Link *find(const std::string &name) const; // find values in const list
Link *advance(int n) const; // advance n positions in list
Link *add_ordered(Link *n);
Link *next() const { return m_succ; }
Link *previous() const { return m_prev; }
God god() const { return m_god; }
private:
God m_god;
Link *m_prev;
Link *m_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->add_ordered(new Link{ God{ "Odin", "Norse",
"Eight-legged flying horse called Sleipnir", "Spear called Gungnir" } });
norse_gods = norse_gods->add_ordered(new Link{ God{ "Zeus", "Greek", "", "Lightning" } });
norse_gods = norse_gods->add_ordered(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->add_ordered(new Link{ God{ "Athena", "Greek", "",
"Two swords and a spear; is allowed to use Zeus's thunderbolt" } });
greek_gods = greek_gods->add_ordered(new Link{ God{ "Mars", "Greek", "", "A spear stained in blood" } });
greek_gods = greek_gods->add_ordered(new Link{ God{ "Poseidon", "Greek", "",
"Three-pronged spear called Trident" } });
Link* p = greek_gods->find("Mars");
if (p)
{
p->god().set_name("Ares");
}
Link* p2 = norse_gods->find("Zeus");
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::insert(Link *n)
{
if (n == nullptr)
{
return this;
}
n->m_succ = this;
if (m_prev)
{
m_prev->m_succ = n;
}
n->m_prev = m_prev;
m_prev = n;
return n;
}
Link *Link::add(Link *n)
{
if (n == nullptr)
{
return this;
}
n->m_prev = this;
if (m_succ)
{
m_succ->m_prev = n;
}
n->m_succ = m_succ;
m_succ = n;
return n;
}
Link *Link::erase()
{
Link *trav = this;
if (trav == nullptr)
{
return nullptr;
}
if (trav->m_succ)
{
trav->m_succ->m_prev = trav->m_prev;
}
if (trav->m_prev)
{
trav->m_prev->m_succ = trav->m_succ;
}
return trav->m_succ;
}
Link *Link::find(const std::string &name)
{
Link *trav = this;
while (trav != nullptr)
{
if (trav->m_god.name() == name)
{
return trav;
}
trav = trav->m_succ;
}
return nullptr;
}
const Link *Link::find(const std::string &name) 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->m_god.name() == name)
{
return c_trav;
}
nc_trav = nc_trav->m_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->m_succ == nullptr)
{
return nullptr;
}
trav = trav->m_succ;
}
}
else if (n < 0)
{
while (n++)
{
if (trav->m_prev == nullptr)
{
return nullptr;
}
trav = trav->m_prev;
}
}
return trav;
}
/**
* This function's specs are as follows:
* Add a member function add_ordered() that places its new element in its correct lexicographical position.
* Using the Links with the values of type God, make a list of gods from three mythologies; then move the
* elements (gods) from that list to three lexicographically ordered lists — one for each mythology.
*/
Link *Link::add_ordered(Link *n)
{
Link *trav = this;
while (trav != nullptr)
{
const std::string key = n->m_god.name();
if (key < trav->m_god.name())
{
trav = trav->advance(-1);
trav = trav->insert(n);
return trav;
}
else if (key > trav->m_god.name())
{
trav = trav->advance(1);
trav = trav->insert(n);
return trav;
}
trav = trav->m_succ;
}
return nullptr;
}
void print_all(Link *p)
{
std::cout << "{\n";
while (p)
{
std::cout << "Name: " << p->god().name() << '\n'
<< "Myth: " << p->god().myth() << '\n';
if (p->god().vehicle() != "")
{
std::cout << "Vehicle: " << p->god().vehicle() << '\n';
}
else
{
std::cout << "Vehicle: N/A\n";
}
std::cout << "Weapon: " << p->god().weapon() << '\n';
if ((p = p->next()))
{
std::cout << "\n";
}
}
std::cout << "}";
}
|