I am having a hard time grasping ADT using linked list.
I have a struct node in a class where i have a node constructor.
I cant create a link list without using the constructor. But thats where i am lost. This is what i have
#include <ostream>
#include "winery.h"
using namespace std;
//const int SIZE = 100;
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_
int main()
{
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));
#ifndef _WINERY_
#define _WINERY_
#include <ostream>
class winery
{
public:
winery(const char * const name, const char * const location, const int acres, const int rating);
virtual ~winery(void);
const char * const getName() const;
const char * const getLocation() const;
const int getAcres() const;
const int getRating() const;
// display headings for lists of wineries
static void displayHeadings(std::ostream& out);
friend std::ostream& operator<<(std::ostream& out, winery *w);
private:
char *name;
char *location;
int acres;
int rating;
};
#endif // _WINERY_
not sure if the initializer list for this class function is correct?
Do i need to create memory for the linked list in the node constructor?
in the insert function i try to create my linked list however this crashes
node* newNode = new node(winery);
#include "list.h"
#include "winery.h"
list::node::node(const winery &winery) : item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating())
{
//item = winery;
nextByName = NULL;
nextByRating = NULL;
}
list::list()
{
int acres = 0;
int rating = 0;
}
void list::insert(const winery& winery)
{
node *p;
p = new node(winery);
//node* newNode = new node(winery); // CRASHES wont let me create memory
node * prev = NULL;
node * curr = headByName;
//traverse to find the position to insert
//while (curr!=NULL && curr->item < winery)
//{
// prev = curr;
// curr = curr->next;
//}
//node *newNode = new node(winery);
//while (curr!=NULL && curr->item < winery)
//{
// prev = curr;
// curr = curr->next;
//}
}
It wont let me create memory in the insert function without calling the node constructor. however i do not know what to do with the winery object being passed in to it.
Any help is very appeciative since i have been banging my head trying to get this for over a week now.