Accessing a string from

I am taking a C++ class in which I am suppose to implement a linked list where I insert a node alphabetically by name. I am suppose to create a Winery class with name of the winery, location, acreage, and rating, and then create a list class that implements a linked list that sorts the wineries alphabetically by name. The definition of the winery class is followed by the definition of the list class. In the insert method in the list class, I am suppose to insert the new winery into the list in alphabet order by name. I made an attempt but I know that what my code currently looks like is incorrect. How can I improve the code, or at least make the pointer access correct? I am asking about the code in the insert method after the //******.

I know that newNode->item->name is incorrect. What would be the correct syntax? I also know that (headByName + idx)->item->name is incorrect but what is the correct syntax for that? Or is there a better way to do the comparison? Does the while statement work? Yes, I have a lot to learn about C++ and pointers that is why I am in this class trying to learn. Thank you so much for your help.


class Winery
{
public:

Winery(const char * const name, const char * const location, const int acres, const int rating);
virtual ~Winery(void);

// complete implementations for the following 4 functions, nothing needed in the .cpp file
const char * const getName() const { return name; }
const char * const getLocation() const { return location; }
const int getAcres() const { return acres; }
const int getRating() const { return rating; }

// print out column headings for lists of wineries, as specified by lab1output.txt
static void displayColumnHeadings(std::ostream& out);

// print out a winery, as specified by lab1output.txt
friend std::ostream& operator<<(std::ostream& out, Winery *w);

private:
char *name;
char *location;
int acres;
int rating;
};


class List
{
public:
List(void); // constructor
virtual ~List(void); // destructor

// Print out the wineries in alphabetical order by name,
// by calling winery's operator<< for each winery.
void displayByName(ostream& out) const;

// Print out the wineries from highest to lowest rating,
// by calling winery's operator<< for each winery.
void displayByRating(ostream& out) const;

// Insert a winery into both the names and ratings threads.
// The names thread should be in alphabetical order by name.
// The ratings thread should be in order from highest rating
// to lowest rating.
void insert(const Winery& winery);

// Return a const pointer to the winery instance it finds in
// the list, or 0 if it didn't find a winery with that name.
// Because the pointer is declared const, there is no danger
// that find's caller will be able to use the returned pointer
// to change the instance of winery.
Winery * const find(const char * const name) const;

// Remove the winery with the specified name from both the name
// thread and the ratings thread. Returns true if it found and
// removed the winery, false if it did not find the winery.
bool remove(const char * const name);

private:
// defines each node in the doubly-threaded linked list.
struct Node
{
Node(const Winery& winery); // constructor
Winery item; // an instance of winery
// (NOT a pointer to an instance)
Node *nextByName; // next node in the name thread
Node *nextByRating; // next node in the rating thread
};

Node *headByName; // first node in the name thread
Node *headByRating; // first node in the rating thread
};


void List::insert(const Winery& winery)
{
Node *newNode(new Node(winery));
int idx = 0;

if (headByName == nullptr)
headByName = nextByName= newNode;
else //******
while (*(headByName + idx)) {
if (strcmp(newNode->item->name,(headByName + idx)->item->name)){
}
}
}
Topic archived. No new replies allowed.