Jump to content
  • Advertisement

Archived

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

Zorbfish

Linked List not linking

This topic is 5639 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

Alright I''m copying this from a book and for some reason the linked list example is not working! When I try to do a simple iteration through the list it becomes infinite meaning that a NULL end pointer is not being assigned to the last element. What is wrong? Did I make a typo or is this the most worthless book example ever?!
  
class Link
{
	public:

	// Constructors / Destructors


		Link	();
		Link	(int);
		Link	(const Link &);
		~Link	();

	// Mutators


		void	SetValue(int);

	// Operations


		void	operator= (const Link &);

	// Data


		Link * next;
		int data
};
  

  
void
Link::operator =(const Link & copyfrom)
{
	next = copyfrom.next;
	SetValue(copyfrom.data);
}

Link::Link(const Link & copy)
{
	next = copy.next;
	SetValue(copyfrom.data);
}
  

  
class myList
{
	public:

	// Constructors / Destructors


		myList	();
		~myList	();

	// Methods


		void	 Add			(Link &);
		void	 Delete			(int);
		bool	 Empty			();

		void	 Print			(ostream &);

	// Data


		typedef Link *	Iterator;

		Link *	firstLink;
};
  

  
myList::myList()
{
	firstLink = 0;
}

void
myList::Add(Link & add)
{
	Link * newLink = &add;

	if(Empty())
	{
		firstLink = newLink;
	}
	else
	{
		newLink->next = firstLink;
		firstLink = newLink;
	}
}

// below causes a infinite loop because no null is found!

myList::Print(ostream & out)
{
	for(Link * i = firstLink; i != 0; i->next)
	{
		out << i->data << endl;
	}
}
  
I''ve included only the implementations that I think have the error but the code looks fine. What is wrong? I appreciate anyone who took the time to read this lengthy post. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
in that for loop in the last code section, wouldn''t you want to increment i->next?

-noix-

In this world gone mad, we won''t spank the monkey; the monkey will spank us.

Share this post


Link to post
Share on other sites
Don't you have to set NULL to the tail of the linked lists so you can find it and insert it before the NULL....


To initialize the linked list it should be like

//Global vars
node *head;
node *tail;


void init()
{
head = new node;
tail = new node;
head->next = tail;
tail->next = NULL;
}

//to insert it, it would look like
void insert()
{
node *temp;
node *cur;
node *prev;

temp = new node;
temp = //whatever you want it to be
prev = head;
cur = prev->next;
while(cur != NULL)
{
/cycle till we reach NULL
prev = cur;
cur = prev->next;
}
//insert it
prev->next = temp;
temp->next = cur;
}

//Thats what i do, if you want my program for Linked Lists just reach me at SumDude@cox.net

while(



[edited by - SumDude on April 18, 2003 2:52:35 AM]

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!