Linked list with templates problem

Started by
3 comments, last by Trillian 18 years, 5 months ago
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!
Advertisement
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
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).
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
Alright forget about this i'll try to use vectors

This topic is closed to new replies.

Advertisement