linked lists and inheritance

can someone help me figure this out?

Page 1 of 1

8 Replies - 2166 Views - Last Post: 02 December 2009 - 03:43 PM Rate Topic: -----

#1 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

linked lists and inheritance

Posted 01 December 2009 - 06:17 PM

So, I made a linked list, and then an inherited linked list that stores linked lists.
But, it seems to be messing up when I'm trying to add a linked list to a node.
	Node *ptr=start; //make ptr point to the beginning of the linked list to help transverse the list
	if (ptr==NULL)
		cout<<"uh-oh";
	while (ptr != NULL) //while it's not the end of the list
	{
		cout<<"helloooooo";


The cout's are to see where the problem is. It prints the "uh-oh" - which is very bad. And it's not printing the "hellooo" (well, obviously, but I tried the hello first and didn't think of the possibility of it being NULL until awhile later).
So, I'm trying to figure out why start would be NULL, and that's what I'm wanting to ask. See, I have the addNode function in the parent linked list, and I thought the parent would take care of that for this linked list. It does, so the start pointer should be at the end of the list. But, since I have a different constructor for this linked list, that sets start to NULL until more nodes are added, does that mean that I have to copy and past the addNode function to get the start pointer moving? (If I'm going to have all the same functions of the parent linked list, does that mean I shouldn't even have it inherit from it?)

This post has been edited by asj127: 01 December 2009 - 06:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: linked lists and inheritance

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: linked lists and inheritance

Posted 01 December 2009 - 06:20 PM

How are we supposed to know anything about your start variable??? You've not shown us where you've declared it, initialized it, or modified it, and you KNOW that's the crux of the problem.
Was This Post Helpful? 0
  • +
  • -

#3 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: linked lists and inheritance

Posted 01 December 2009 - 06:26 PM

ah, sorry. I was trying to figure out how to go about this... I'm going to attach a zip file with everything because I don't know how to separate the stuff and have everything that could be necessary to figure it out. But I'll try to find the stuff about start.

This is from the child linked list
private:

	struct Node //each node of the linked list, held together by the next pointer
	{
		string token;
		bool second_word; //tells whether or not there is a space within the token (a two-word keyword)
		LinkList secondaryList; //keeps a list of all related words
		Node *next;
	};

	Node *start; //pointer to the beginning of the linked list


LinkListList::LinkListList()
{
	start=NULL; //NULL will always be at the end of the linked list. An important line for other functions
	ptrNode=NULL; //there is no need for ptrNode to point to any node at the moment
}


and this is from the parent linked list:
void LinkList::addToken(string token)
{
	Node *newNode=new Node; //new node created to hold token
	newNode->token=token;
	if (twoWordToken(token)) //if the token has a space,
		newNode->second_word=true;
	else
		newNode->second_word=false;
	newNode->next=start; //put the node at the beginning of the linked list
	start=newNode;
}

Attached File(s)

  • Attached File  CS380.zip (8.56K)
    Number of downloads: 72

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: linked lists and inheritance

Posted 01 December 2009 - 06:43 PM

That's a lot of code to go through...I'd suggest you start to figure out how to use a debugger, rather than sprinkling your code with couts. There is this however:
bool LinkListList::addSubList(LinkList subList, string commandWord) //add the subList to the node containing the commandWord
{
	setPtrNode(commandWord); //finds the node which the subList should be in and saves to ptrNode


That subList argument to the function? A copy of that is being made when you enter the function, and you do not have a copy constructor defined for the LinkList class. That will likely be a problem, as the pointers contained therein will point who-knows-where.
Was This Post Helpful? 0
  • +
  • -

#5 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: linked lists and inheritance

Posted 01 December 2009 - 06:50 PM

Did I forget to include main.cpp in the zip?

None of my teachers ever mentioned debugging, except to say to click, "start without debugging".

hmm... copy constructor... that sounds familiar. so should the copy constructor be the way to go, or is there an easier way of doing what I want it to do? And could that be the problem, or is there still something way wrong with start? Because I'm afraid that the start pointer in the parent isn't transferring to the start pointer in the ListList child and I don't know how I could fix that, unless there's a way of setting start equal to the parent start...

here's main, if it's not in the zip and you want to see it.
//main function
//used for testing

#include "consumer.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string verbs="go to, jump over, open";
	string jumpOverNouns="that candlestick, tree";
	string openNouns="door, jar, refrigerator";

	bool found=false;
	bool found2=false;

	string newVerbs="";
	string newJumpOverNouns="";
	string newOpenNouns="";
	string newGoToNouns="";
	string command="";

	Consumer c(verbs);

	cout<<"verbs: "<<verbs<<endl;
	cout<<"nouns: "<<jumpOverNouns<<", "<<openNouns<<endl;

	//c.addSubList(jumpOverNouns, "jump over");
	c.addSubList(openNouns, "open");

	cout<<"hello"<<endl;

	newVerbs=c.returnCList();
	cout<<"verbs from cList: "<<newVerbs<<endl;

	newJumpOverNouns=c.returnIList("jump over");
	cout<<"nouns for \"jump over\" verb from subList: "<<newJumpOverNouns<<endl;

	newOpenNouns=c.returnIList("open");
	cout<<"nouns for \"open\" verb from subList: "<<newOpenNouns<<endl;

	newGoToNouns=c.returnIList("go to");
	cout<<"nouns for \"go to\" verb from subList: "<<newGoToNouns<<endl;

	cout<<"Enter a command: "<<endl;
	getline(cin,command);
	c.sendToServer(command);

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: linked lists and inheritance

Posted 01 December 2009 - 08:47 PM

You could also pass by reference instead of value. It could be the problem, but again, in a large program it's hard to say for sure. Which is why learning to debug is important. I would set a breakpoint on the c.addSubList() call in main and step into the code line by line. In the debugger you can add watches -- watch a particular variable -- and see how the state of your program changes as you progress through it.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: linked lists and inheritance

Posted 01 December 2009 - 10:03 PM

An example of a copy constructor (code structure taken from my linked list blog post, found here):

template<class T>
LinkedList<T>::LinkedList(const LinkedList& rhs){
	Node<T>* temp = rhs.head, *handle;
	this->head = new Node<T>(temp->getData());//set the head node of the new list
	this->head->setNext(NULL);
	this->curNode = head;
	this->tailNode = curNode;
	cout << "In copy constructor, copying..." << endl;
	temp = temp->getNext();
	size = 1;
	while(temp != NULL){
		handle = new Node<T>(temp->getData());
		curNode->setNext(handle);
		curNode = curNode->getNext();
		tailNode = curNode;
		temp = temp->getNext();
		size++;
	}
}




I could clean it up a bit, just wanted to throw that out here for perusal.

This post has been edited by KYA: 01 December 2009 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: linked lists and inheritance

Posted 02 December 2009 - 07:19 AM

so could I do this instead of making a copy constructor? It'd probably be best not to use up too much memory...
addSubList(LinkList &subList, string commandWord);


So, there's something important I'd really like to know...

in the parent linked list I have this:
	LinkList();
	~LinkList();
	void addToken(string token);
	string showList();
	bool findToken(string token);
	string getToken(string word);
	void deleteList();


in the child linked list I have this:
	LinkListList(); //modified contructor initiates the pointers start and ptrNode
	~LinkListList(); //modified destructor deletes all nodes and secondaryList nodes
	bool addSubList(LinkList &subList, string commandWord); //calls setPtrNode, then adds the given subList to that node
	bool findSubToken(string commandWord, string token); //calls setPtrNode, then calls on that node's secondaryList's findToken function
														 //returns true if the findToken function returns true, else returns false
	string showSubList(string commandWord); //returns a string of tokens, separated by commas and spaces, representing the subList of the given token
	string getSubToken(string word, string commandWord);

I thought the child linked list would inherit the other functions. But, I have start in the private section of the child and if I delete it then it gives me an error about not being able to convert from linklist node* to linklistlist node* - does this mean I can't use the parent functions for the child linked list because I use a different type of node? And I'm afraid that having the start pointer there is making two separate start pointers, one for the parent functions and one for the child functions, but it should be the same start pointer for all of them.
Was This Post Helpful? 0
  • +
  • -

#9 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: linked lists and inheritance

Posted 02 December 2009 - 03:43 PM

I think I need the copy constructor for something...

But, actually, I'm thinking maybe it's all wrong. Is there a way that I can make 'token' within a node be anything? Like, where I can have one linked list and one node, and pass in either a linked list or a string to be 'token'?
I could probably do that with a template, right? But I don't want to go through trying to translate everything over to that and getting hundreds of error messages. Is there an easy way to make it work? Because I really need to get this done as soon as possible...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1