Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the Link class from 17.10.1 to hold a value of a class God. Class God sho

ID: 3690425 • Letter: M

Question

Modify the Link class from 17.10.1 to hold a value of a class God. Class God should have members of type string: name, mythology, vehicle, weapon. For example God("Zeus", "Greek", "", "lightning") and God("Odin","Norse", "Eight-tagged flying horse called Sleipner",""). Write a print_all() function that lists gods with their attributes one per line. Add a member function add_ordered() that places its new element in its correct lexicographical position. Using the Links with the values of type Gods, 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. ( C++ )

The function add_ordered() that you are supposed to write should put the Link in its proper position with respect to the alphabetical ordering of the "name" member. To summarize:  
1. Modify the Link class as indicated, including adding the add_ordered() member function.   2. Write a (free-standing) print_all() function as indicated.  

Write main() function that:  
1. Uses repeated calls to add_ordered() to make a single list of gods that includes ones from three different sports   2. Prints out the list using the print_all() function 3. Separate the list into three different lists, one per mythology, again using add_ordered() 4. Prints out the three lists using the print_all() function.

Here is the already modified Link class.

#include "std_lib_facilities.h"

class Link {

public:

string name;
string myth;
string vehicle;
string weapon;

Link(const string& a_name, const string& a_myth, const string& a_vehicle, const string& a_weapon, Link* p = nullptr, Link* s = nullptr)
  :name{ a_name }, sport{ a_myth }, position{ a_vehicle }, team{ a_weapon }, prev{ p }, succ{ s } { }

Link* insert(Link* n);
Link* add(Link* n);
Link* erase();
Link* find(const string& s);
const Link* find(const string& s) const;
Link* advance(int n);

Link* next() const { return succ; }
Link* previous() const { return prev; }

private:
Link* prev;
Link* succ;

};

Not all Link functions listed may be needed.

I've already got a good bit done. Just having trouble with putting things into lexicographical order.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

class Link {
public:
string name;
string myth;
string vehicle;
string weapon;
Link(const string& a_name, const string& a_myth, const string& a_vehicle, const string& a_weapon, Link* p = nullptr, Link* s = nullptr){
name = a_name;
myth = a_myth;
vehicle = a_vehicle;
weapon = a_weapon;
}
Link* next() const { return succ; }
Link* previous() const { return prev; }
void setNext(Link* next){
succ = next;
}
void setPrev(Link* prev){
prev = prev;
}
private:
Link* prev;
Link* succ;
};

class List{
public:
Link* head;
List(){
head = nullptr;
}
Link* insert(Link* n){
if (head == nullptr){
n->setNext(head);
head = n;
}
else{
n->setNext(head);
head->setPrev(n);
head = n;
}
return head;
}
Link* add_order(Link*& h,Link* n){
if (h == nullptr){
n->setNext(head);
head = n;
return head;   
}
else{
if (h->name > n->name){
n->setNext(h);
n->setPrev(h->prev());
h->setPrev(n);
}
else{
h->setNext(add_order(h->next(),n));
return h;
}
}
}

};

int main(){
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote