Linked List not linking

Started by
2 comments, last by Zorbfish 21 years ago
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.
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.
In this world gone mad, we won't spank the monkey; the monkey will spank us.
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]
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
Shouldn''t the for loop say:
i = i->next
instead of
i->next

This topic is closed to new replies.

Advertisement