Jump to content
  • Advertisement
Sign in to follow this  
Si0n

Help wth my link list

This topic is 5395 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 commented the lines that are giving me the error (it is a run error by the wya, those suck =( )
#ifndef LIST_H
#define LIST_H

#include <iostream>
using namespace std;

struct s_StNodes
{
	int age;

	s_StNodes* next;
};

class c_Class
{
public:
	c_Class(): m_pHead(0), m_pTail(0) {}
	~c_Class() { freeMem(); }

	void addStudent(int age);
	void freeMem();
	
	friend ostream& operator<<(ostream& os, const c_Class& a);

private:
	s_StNodes* m_pTail;
	s_StNodes* m_pHead;
};

#endif
#include "List.h"

void c_Class::addStudent(int age)
{	
	s_StNodes* std = new s_StNodes;
	std->age = age;

	if(m_pHead == 0)
	{
		m_pHead = std;
	}
	else
	{
		m_pTail = std;
	}
	
	m_pTail = m_pTail->next; // can't do this
	
}

void c_Class::freeMem()
{
	while(m_pHead != 0)
	{
		delete m_pHead;
	}
}

ostream& operator<<(ostream& os, const c_Class& a)
{
	s_StNodes* first = a.m_pHead;
	
	if(first == 0)
	{
		os << "Lobby is empty\n";
	}

	while(first!=a.m_pTail)
	{
			os << first-> age;
			first = first -> next; // also gives me a problem
	}

	return os;
}
#include "List.h"


int main()
{
	c_Class myC;

	myC.addStudent(12);

	cout << myC;

	return 0;
}
btw, can anyone teach me a way to go back in the link list? and how important is knowing how to build your own list?

Share this post


Link to post
Share on other sites
Advertisement
It's not particularly important these days, though most use 'making a linked list' as their avenue to becoming comfortable with pointers, which is important.

Share this post


Link to post
Share on other sites
Firstly, C++ has a linked list class, so you are reinventing the wheel a bit here.

That said, you have a few problems with adding an object to the list, you dont seem to be linking the elements at all.

Havn't checked this, but your addStudent should look something like this.


void c_Class::addStudent(int age)
{
s_StNodes* std = new s_StNodes;
std->age = age;
std->next = 0;

if(m_pHead == 0)
{
m_pHead = std;
}
else
{
m_pTail->next = std;
}

m_pTail = std;

}



Your free mem function is also a potential infinite loop.


void c_Class::freeMem()
{
s_StNodes* std = m_pHead;
while(std != 0)
{
s_StNodes* next = std->next;
delete std;
std = next;
}
}



That should fix the error in the stream output function too.

You should change the while(first!=a.m_pTail) loop to while(first!=0), as you will miss outputting the tail node if you dont.

Not checked any of that code, so hope it works :).



To go backwards, you could add another member into the node class, one which points to the previous node.

Share this post


Link to post
Share on other sites
Its pretty simple really.

When you add a new element to the list, the new element becomes the new tail of the list, no matter whether the list was empty or not.

To add the new element to the end of the list, you need to link the element to the previous tail of the list. This means setting the previous tails next member to the new tail node(i.e. m_pTail->next = std;).

Then you need to set the std node as the new tail element, (i.e. m_pTail = std;). The new nodes next pointer is set to zero, which provides a way to find when you have reached the end of the list.

When deleting the elements, you have to be careful not to use the memory that you just deleted. This means storing a temporary pointer to the next element before deleting the current element, hence the need for

s_StNodes* next = std->next;
delete std;
std = next;

When you reach the tail, the next pointer will be zero, and so the loop will terminate.

Hope that helps :). Always seems hard when you are new to it, but it gets easier as you go along.

Share this post


Link to post
Share on other sites
Thanks that helps a lot =)

Can you explain how Iwould go backwards? I'd have to have a pointer in my struct right?

Share this post


Link to post
Share on other sites
Yes, you would extend the node class to something like..


struct s_StNodes
{
int age;
s_StNodes* prev;
s_StNodes* next;
};




Then you would need to change how you link up the new elements.


void c_Class::addStudent(int age)
{
s_StNodes* std = new s_StNodes;
std->age = age;
std->next = 0;

if(m_pHead == 0)
{
m_pHead = std;
std->prev = 0; //no previous element so set to zero
}
else
{
m_pTail->next = std;
std->prev = m_pTail; //set previous node to the previous tail
}

m_pTail = std;

}




No to go through the list backwards, you would start at the tail and finish at 0 (the head). For example, to free the memory backwards you would use.


void c_Class::freeMem()
{
s_StNodes* std = m_pTail;
while(std != 0)
{
s_StNodes* prev = std->prev;
delete std;
std = prev;
}
}





Pretty easy really :).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!