Sign in to follow this  
Trillian

Linked list with templates problem

Recommended Posts

Hello! This is my first time making a linked list and using templates. However it works quite well except I get a bug when calling a function. The Linked list is supposed to have the following functionnality: -Supports any var types (templates) -Can always add new nodes -Each node has an index starting from 0 -Data can be accessed with the index Here's my class code:
[source lang=cpp]
template<class T>
class CD3DYXarray
{
protected:
	T * m_pData;
	CD3DYXarray * m_pNextNode;
	int m_nArrayPos;

public:
	CD3DYXarray()
	{
		m_nArrayPos=0;
		m_pData=NULL;
		m_pNextNode=NULL;
	}

	CD3DYXarray(int num)
	{
		m_nArrayPos=num;
		m_pData=NULL;
		m_pNextNode=NULL;
	}

	~CD3DYXarray()
	{
		m_nArrayPos=0;
		SAFE_DELETE(m_pData);
		SAFE_DELETE(m_pNextNode);
	}


	void SetData(T * tData) { if(tData) m_pData = tData; }

	void AddNode(T * tData)
	{
		if(m_pNextNode)
			m_pNextNode->AddNode(tData);
		else
		{
			m_pNextNode = new CD3DYXarray(m_nArrayPos+1);
			m_pNextNode->SetData(tData);
		}
	}

	T * at(int num)
	{
		if(m_nArrayPos == num) return m_pData;
		if(m_pNextNode == NULL) return NULL;
		return m_pNextNode->at(num-1);
	}

	void OutputContent()
	{
		printf("Array[%i] = %i\n",m_nArrayPos,*m_pData);
		if(m_pNextNode)
			m_pNextNode->OutputContent();
	}
};

All functions work well with an unlimited number of nodes (i've tryed 3). EXCEPT the at() function. the at() function wont work, if the argument is 0, it outputs "0" if the argument is anything else, it bugs, runtime error. I am testing it with "int*" var types. please help!

Share this post


Link to post
Share on other sites
I would highly recommend that rather than try to fix that [it is poorly written in several different ways], you just use std::list. Or, since you clearly want random access, std::vector.

However, the problem you are encountering is almost certainly that you're decreasing num when you recursively call at().

CM

Share this post


Link to post
Share on other sites
I haven't looked too closely so maybe I missed something. But shouldn't:


return m_pNextNode->at(num-1);


Be simply

return m_pNextNode->at(num);


since you are still looking for the same index. As for the other. I'm not sure why it's behaving the way you described. As for the returning "0". 0(Assuming you are talking about the number not a string) is NULL so it may have something to do with your pointers getting mixed up or your indexes(not being able to find the node).

Share this post


Link to post
Share on other sites
From my understanding of your at function, you have an int passed in and you're looking for the data at that number. First you check if the position you're at is that number, if it is its found so return the data. If not check if the next node is null, if it's null you didn't find that index so return null. and lastly if there is something pass num-1 into the next node?

If this is how it works, why do you pass num-1? If you're searching for a specific index, why modify it? But if I have it completely wrong please explain what you're trying to do with the at function.

Hope this helps!
darkweb

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this