Ordering of LinkedList code..

Trying to figure out why my list() class pointers are being overwritten with the third node. What happens in the insert function (posted) is that the second time the insert function is called my node pointers are overwritten when the debugger has not even rolled over it!


The attributes of the winery object are passed through the winery ctor. And then the list::insert function is called, where I begin to create the list:

1
2
3
4
5
//main.cpp
//the attributes of the winery object are the paramaters:
list *listPtr = new list();
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
wineries->insert(winery("Gallo", "Napa Valley", 200, 25));


Then the Ctor is called where I allocate the private pointer memembers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//winery.cpp
winery::winery(const char * const name, const char * const location, const int acres, const int rating)
  : m_acres( acres ), m_rating( rating )
{
	if (name)
	{
		size_t len = strlen( name ) +1;
		m_name = new char[len];
		strcpy_s( m_name, len, name );
	}
	else
		m_name = NULL;

	if (location)
	{
		size_t len = strlen( location ) +1;
		m_location = new char[len];
		strcpy_s( m_location, len, location );
	}
	else
		m_location = NULL;
}


And then to the issue at hand: If you can imagine this function has already been called then new_node SHOULD contain the second objects attributes, Gallo, and Nappa Valley. Also, headByName and headByRating do have the winery object's address so they are not null and the first if statement is untrue, therefore NOT being overwritten.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// list.cpp
void list::insert(const winery& winery)
{
	node *new_node	= new node( winery ); // on second call it has new info!
	node *temp_node = NULL;
	do
	{
// so this condition fails because we have already transfered data
		if ( headByName == NULL || headByRating == NULL ) 
		{
			new_node->item  = winery;
			headByName	= new_node; 
			headByRating	= new_node; 
		} 
		else // we are here then, keep in mind its the second call still
		{
			temp_node  = new_node; // transfer ownership but why??
			temp_node->item = winery; // Now we are switching.
			headByName->nextByName	   = temp_node;
			headByRating->nextByRating = temp_node;
		}
	} while ( headByName == NULL || headByRating == NULL );
new_node = NULL; // I just added this....	
}


Could someone help clarify why it is working like this?
I know I dont need a few things with this code, line 16 and line 9.
Its' pretty close, i just goofed something..<takes a walk>

oh yeah, and the pointers that are available to me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct node
{
    winery  item;
    node *  nextByName;
    node *  nextByRating;
};

class list
{
    ...
private:
    node * headByName;
    node * headByRating;
};


this might be of some importance:
1
2
3
4
5
6
7
8
//list.cpp
list::node::node(const winery &winery) :
nextByName( NULL ), nextByRating( NULL ), item( winery.getName(),
winery.getLocation(), winery.getAcres(), winery.getRating() )
{
    // where the item paramters are all pub-mem functions of the winery class.	

}
Last edited on
This will sound mean but stl will make this a non issue. Use std::string for certain.
Also use std::list or std::vector.

Honestly the code here is confusing. And thats bad news for a basic core algorithm.
You may want to look at wikipedia or some other places to get pseudo code for what the algorithm really should be doing.
Topic archived. No new replies allowed.