Programming: Principles and Practice Using C++ (2nd Edition) by Bjarne Stroustrup

Programming: Principles and Practice Using C++ (2nd Edition) by Bjarne Stroustrup

Author:Bjarne Stroustrup [Stroustrup, Bjarne]
Language: eng
Format: epub, pdf
Publisher: Pearson Education
Published: 2014-06-02T00:00:00+00:00


17.10.1 More link use

Having dealt with the implementation issues, we can see how the use now looks:

Click here to view code image

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"});

That’s very much like before. As before, we correct our “mistakes.” Correct the name of the god of war:

Click here to view code image

Link* p = greek_gods–>find("Mars");

if (p) p–>value = "Ares";

Move Zeus into his correct Pantheon:

Click here to view code image

Link* p2 = norse_gods–>find("Zeus");

if (p2) {

if (p2==norse_gods) norse_gods = p2–>next();

p2–>erase();

greek_gods = greek_gods–>insert(p2);

}

Finally, let’s print out those lists:

Click here to view code image

void print_all(Link* p)

{

cout << "{ ";

while (p) {

cout << p–>value;

if (p=p–>next()) cout << ", ";

}

cout << " }";

}

print_all(norse_gods);

cout<<"\n";



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.