Singly Link List help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 4
Reputation: core2d is an unknown quantity at this point 
Solved Threads: 0
core2d core2d is offline Offline
Newbie Poster

Singly Link List help

 
0
  #1
Oct 5th, 2008
I have an assignment to write a pseudo-code to have a list pointed by H (list of numbers) that have to be copied to a list pointed by H1 (only the odd numbers need to be copied). Can anyone tell me if I am wrong on this (not a working code just the core part that copies the odd numbers, sorry if code does not make sense I am completely new to linked list):

  1. Algorithm Node *evenOut(struct node *H, struct node *H1)
  2. Input: A list of numbers
  3. Output: A list pointed by H1 that only contains odd numbers
  4.  
  5. node *current = H;
  6. node *oddCurrent = H1;
  7. node *startPointerForOdd = NULL;
  8.  
  9. node *temp1 = current;
  10. node *temp2 = oddCurrent;
  11.  
  12. node *next;
  13.  
  14. int valueOfNode;
  15.  
  16. if (current == NULL)
  17. {
  18. Return 0;
  19. }
  20. else
  21. {
  22. while(current != NULL)
  23. {
  24. valueOfNode = temp1->next->value;
  25. if(valueOfNode % 2 != 0)
  26. {
  27. if(startPointerForOdd == NULL)
  28. {
  29. temp2 = valueOfNode;
  30. startPointerForOdd = temp2;
  31. }
  32. else
  33. {
  34. temp 2 = startPointerForOdd;
  35. while(temp2 ->next != NULL)
  36. {
  37. temp2 = temp2->next;
  38. }
  39. temp2->next->value = valueOfNode;
  40. }
  41. }
  42. }
  43. }
Last edited by core2d; Oct 5th, 2008 at 12:41 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Singly Link List help

 
0
  #2
Oct 5th, 2008
> Return 0;
What do you return in the other case?

You've got too many node pointers. All you need to scan down the list is
  1. while(current != NULL) {
  2. valueOfNode = current -> value;
  3. // do something with valueOfNode
  4.  
  5. current = current -> next; // advance
  6. }

Also, if you keep the end pointer of the list you're copying to, then that makes that part of the processing very easy as well.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Singly Link List help

 
0
  #3
Oct 5th, 2008
I think you have used too many pointers here. You can manage with a few.

> valueOfNode = temp1->next->value;

i got confused in this line, because earlier you had declared a node :
> node *next;
is this 'next' you are referring ? or is it the link of temp1 to the next node?

  1.  
  2. current = H; //current points to the first node of H.
  3. oddStart = H1; // oddStart points to the first node of H1.
  4. node* newnode; // for a newnode.
  5. while (current != NULL)
  6. {
  7. nodeValue = current->value;
  8. if ( nodeValue % 2 != 0)
  9. {
  10. if (oddStart == NULL)
  11. {
  12. oddStart = new node;
  13. oddStart->value = nodeValue;
  14. oddStart->link = NULL;
  15. }
  16. else
  17. {
  18. temp = oddStart;
  19. while (temp->link != NULL) // here 'link' is the link to the next node.
  20. temp = temp->link;
  21. newnode = new node;
  22. newnode->value = nodeValue;
  23. temp-> link = newnode;
  24. newnode->link = NULL;
  25. }
  26. }

i think this code should work for you. However, it would contain mistakes. But i just want to convey that you can cut down the use of pointer variables to a great extent. The rest is upon you.
Bhoot
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: core2d is an unknown quantity at this point 
Solved Threads: 0
core2d core2d is offline Offline
Newbie Poster

Re: Singly Link List help

 
0
  #4
Oct 5th, 2008
Thank you both for the help. I think I see what you guys are saying about reducing the number of pointers. For the revision code that bhoot_jb posted, would the "while statement" move the pointer to the next element in the link once you are out of the if-else statement, or would you have to put this at the end: nodeValue = current->link or change the while statement from while(current != NULL) to while(current->link != NULL)?
Last edited by core2d; Oct 5th, 2008 at 12:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Singly Link List help

 
0
  #5
Oct 5th, 2008
No, you still need to advance through the list yourself.

for ( current = H ; current != NULL ; current = current->next )
is another way of walking the list in a handy single construct.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Singly Link List help

 
0
  #6
Oct 5th, 2008
salem answered it for me.
well my code snippet was just a code written in haste.
the line you doubted was a missing line.
As Salem said, you will have to advance by yourself. There is no way the while() could do it for you.
The advancement can either be :
  1. current = current->link;
in the while(current!=NULL) loop or you can replace the whole while() loop with a for() loop as demonstrated by Salem.
Bhoot
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC