Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Programmer16

Linked List class problem

This topic is 5304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to make a linked list class, and this is the first time I''ve tried to use a template. I know I should use a vector, blah blah. I just want to understand why this isn''t working for me.
template<class T>
class LinkedList
{
public:
	T* m_pNext;
};

class Test
{
public:
	int m_iData;
	
	void SetData(int iData)
	{
		m_iData = iData;
	}
};

LinkedList<Test> MyLL;

int main()
{
	MyLL.m_pNext->SetData(10);
/*
I also tried to use straight assignment:
MyLL.m_pNext->m_iData = 10;
And it still doesn''t work.
*/
	return 0;
}
Thanks in advance. /* I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition) */

Share this post


Link to post
Share on other sites
Advertisement
Probably the problem is that the pointer you''re trying to use is pointing into nothingness, so when you try to call a member function on it, you get an access violation. You need to allocate an object and assign it''s address to the pointer before you can use the pointer like that.

As an aside, why is your next pointer pointing a member of the contained type rather than to another list node?

Share this post


Link to post
Share on other sites
Hey Indeterminatus, long time no see
It would probably help if I fixed that.
The reason my next pointer isn't pointing to the next is I just wanted to try out templates (I guess I could've done it without the second class).I got it working now Thanks for the help guys.

EDIT
Okay, I had it working, but now I'm having a different problem (I changed the class):

template<class T>
class LinkedList
{
public:
LinkedList* m_pNext;
T m_pName;

void SetData(T data){ m_pName = data; }
void AddNode(T data){ m_pNext = new LinkedList; m_pNext->SetData(data); }
int GetSize(LinkedList<T>* pHead)
{
LinkedList* pTemp = pHead;
int iSize = 0;

while(pTemp != NULL)
{
iSize++;
if(pTemp != NULL)
pTemp = pTemp->m_pNext;
}
return iSize;
}
};

int main()
{
LinkedList<char*> LinkedList;
LinkedList.SetData("First Char");
LinkedList.AddNode("Last Char");

cout<<LinkedList.GetSize(&LinkedList);

return 0;
}


/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/


[edited by - Programmer16 on April 13, 2004 6:12:28 PM]

Share this post


Link to post
Share on other sites
Now I can''t get my AddNode function to work. I''ve modified the class again:

template<class T>
class Node
{
public:
T m_pName;
Node* m_pNext;

Node()
{
m_pNext = NULL;
}

void SetData(T data)
{
m_pName = data;
}
};

template<class T>
class LinkedList
{
public:
Node<T>* m_pParent;

LinkedList()
{
m_pParent = NULL;
}

void AddNode(T data)
{
Node<T>* pTemp = m_pParent;

while(pTemp != NULL)
{
if(pTemp != NULL)
pTemp = pTemp->m_pNext;
}

pTemp = new Node<T>;
pTemp->SetData(data);
}

int GetSize()
{
Node<T>* pTemp = m_pParent;
int iSize = 0;

while(pTemp != NULL)
{
iSize++;
if(pTemp != NULL)
pTemp = pTemp->m_pNext;
}
return iSize;
}

void DisplayList()
{
Node<T>* pTemp = m_pParent;
int iSize = 0;
while(pTemp != NULL)
{
cout<<pTemp->m_pName<<endl;

if(pTemp!= NULL)
pTemp = pTemp->m_pNext;
}
}
};


int main()
{
LinkedList<char*> LinkedList;
LinkedList.AddNode("First");
LinkedList.AddNode("Middle");
LinkedList.AddNode("Last");

cout<<LinkedList.GetSize()<<endl;
LinkedList.DisplayList();

return 0;
}


Thanks again!

/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

Share this post


Link to post
Share on other sites
Try changing the while loop conditional to while (pTemp->m_pNext != NULL) and modifying the guts of the loop appropriately.

Share this post


Link to post
Share on other sites
I did that, and now I''m getting an access violation on that line (while(pTemp->m_pNext != NULL)). I changed it to this:
while(pTemp->m_pNext != NULL)
{
pTemp = pTemp->m_pNext;
}

/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

Share this post


Link to post
Share on other sites
Okay. To do this I would just turn my AddNode function to this right:
void AddNode(T data)
{
if(m_pParent == NULL)
{
m_pParent = new Node;
m_pParent->SetData(data);
return;
}

Node* pTemp = m_pParent;

while(pTemp->m_pNext != NULL)
{
if(pTemp != NULL)
pTemp = pTemp->m_pNext;
}

pTemp->m_pNext = new Node;
pTemp->SetData(data);
}

I changed it to this, but Then it doesn't display right (it skips the first node). Thanks

Edit:
Ok, I use three class to AddNode() (using "First", "Middle", and "Last"), but when I call DisplayList() it displays:
3 (This is from call to GetSize())
Middle
Last
Press enter to continue...

If I use cout to display m_pParent's m_pName it displays "Middle" instead of "First", so I must be doing something wrong in either AddNode() or DisplayList()

void AddNode(T data)
{
if(m_pParent == NULL)
{
m_pParent = new Node<T>;
m_pParent->SetData(data);
return;
}

Node<T>* pTemp = m_pParent;

while(pTemp->m_pNext != NULL)
{
pTemp = pTemp->m_pNext;
}

pTemp->m_pNext = new Node<T>;
pTemp->SetData(data);
}

void DisplayList()
{
Node<T>* pTemp = m_pParent;
int iSize = 0;

while(pTemp->m_pNext != NULL)
{
cout<<pTemp->m_pName<<endl;
pTemp = pTemp->m_pNext;
}
}

/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/


[edited by - Programmer16 on April 13, 2004 8:55:20 PM]

[edited by - Programmer16 on April 13, 2004 8:56:36 PM]

[edited by - Programmer16 on April 13, 2004 8:58:00 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!