943,829 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 635
  • C++ RSS
Oct 5th, 2008
0

Singly Link List help

Expand Post »
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):

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
core2d is offline Offline
4 posts
since Oct 2008
Oct 5th, 2008
0

Re: Singly Link List help

> 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
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 5th, 2008
0

Re: Singly Link List help

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?

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 57
Solved Threads: 2
Junior Poster in Training
bhoot_jb is offline Offline
89 posts
since Mar 2008
Oct 5th, 2008
0

Re: Singly Link List help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
core2d is offline Offline
4 posts
since Oct 2008
Oct 5th, 2008
0

Re: Singly Link List help

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 5th, 2008
0

Re: Singly Link List help

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 :
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 57
Solved Threads: 2
Junior Poster in Training
bhoot_jb is offline Offline
89 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++.
Next Thread in C++ Forum Timeline: Convert integer to string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC