GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Oct-2008, 21:56
mxb1145 mxb1145 is offline
New Member
 
Join Date: Oct 2008
Posts: 3
mxb1145 is on a distinguished road

Traverse Function in a Linked List


Hey everyone,

I'm hoping one of you can help me figure out what's wrong with this code. I'm writing a traverse function that will print out the data for a linked list. For some reason I can't get it to print anything but the first thing in the list. I'm not sure what I'm doing wrong.

CPP / C++ / C Code:
template <class T>
	void List <T>::traverse(void(*visit)(T&))
	{
		Node<T> *temp1 = head;
		int number;
		while(temp1 != NULL)
		{
		       number = temp1->data;
		       temp1 = temp1->next;
		       (*visit)(number);
	              }
	}
Last edited by admin : 08-Oct-2008 at 00:54. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 07-Oct-2008, 22:34
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Traverse Function in a Linked List


Quote:
Originally Posted by mxb1145
CPP / C++ / C Code:
template <class T>
	void List <T>::traverse(void(*visit)(T&))
	{
		Node<T> *temp1 = head;
		int number;
		while(temp1 != NULL)
		{
		       number = temp1->data;
		       temp1 = temp1->next;
		       (*visit)(number);
	              }
	}
The general traversal code looks correct. If you are only able to process the first node, you need to determine whether the next field is indeed pointing to the next node in the linked list. I suspect it doesn't.

When posting code, place a [cpp] code tag before the code, & [/cpp] afterward to both preserve formating & color code keywords & identifiers.
  #3  
Old 08-Oct-2008, 04:18
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Traverse Function in a Linked List


Just need one line of code in while for traverse.
  #4  
Old 08-Oct-2008, 09:21
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Traverse Function in a Linked List


Quote:
Originally Posted by Peter_APIIT
Just need one line of code in while for traverse.
No, this is an incorrect assertion.

If minimalization was the issue, the while-loop could be reduced to:
CPP / C++ / C Code:
while(temp1 != NULL)
{
    (*visit)(temp1->data);
    temp1 = temp1->next;
}
...but optimization is pointless until correctness has been achieved.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 21:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Help in C Print is not working with LinkList batman3280 C Programming Language 3 09-Mar-2006 20:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:29.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.