I have a project that i have to sort a Doubly linked list by name and sort by rating..
I have a insert function where i am adding all my data from main()
I have 2 pointers headByName and headByrating. One thread suppose to go through them by name, the other goes through them by rating.
My question is if i am sorting by name how do i sort by rating in the same function? Also, is my link list not a doubly linked list?
if not can you help me in what i am doing wrong?
Code:
class list
{
public:
list(void); // constructor
virtual ~list(void); // destructor
void displayByName(ostream& out) const;
void displayByRating(ostream& out) const;
void insert(const winery& winery);
winery * const find(const char * const name) const;
bool remove(const char * const name);
private:
struct node
{
node(const winery& winery); // constructor
winery item;
node * nextByName;
node * nextByRating;
};
node * headByName;
node * headByRating;
};
#endif // _LIST_
Code:
int main()
{
char mytry;
list *wineries = new list();
winery *wPtr;
cout << "\nCS260 - Lab1 - " << endl << endl;
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
wineries->insert(winery("Gallo", "Napa Valley", 200, 25));
wineries->insert(winery("Cooper Mountain", "Willamette Valley", 100, 47));
This is where i am doing a insertion sort on name.
The sort works fine, however, i do not think the linked list is done correctly..
I am having a hard time implementing two heads..
Code:
void list::insert(const winery& winery)
{
node* wineryNode = new node(winery);
node* prev = NULL;
node* check = headByName;
headByRating = check;
while (check != NULL && (strcmp(check->item.getName(), wineryNode->item.getName() ) < 0))
{
prev = check;
check = check->nextByName;
}
if (prev == NULL)
{
headByName = wineryNode;
headByRating = wineryNode;
}
else
{
prev->nextByName = wineryNode;
prev->nextByRating = wineryNode;
}
wineryNode->nextByName = check;
wineryNode->nextByRating = check;
I also have a function for display rating..
Code:
void list::displayByName(ostream& out) const
{
node *curr = headByName;
while ( curr )
{
//out << curr;
out << curr->item.getName()
<< curr->item.getLocation()
<< curr->item.getAcres()
<< curr->item.getRating()
<< endl;
curr = curr->nextByName;
}
Any help is very welcome..