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):

Algorithm Node *evenOut(struct node *H, struct node *H1)
Input: A list of numbers
Output: A list pointed by H1 that only contains odd numbers

node *current = H;
node *oddCurrent = H1;
 node *startPointerForOdd = NULL;

node *temp1 = current;
node *temp2 = oddCurrent;

node *next;

int valueOfNode;

if (current == NULL)
{
      Return 0;
}
else
{
    while(current != NULL)
    { 
            valueOfNode = temp1->next->value;
            if(valueOfNode % 2 != 0)
            {
                      if(startPointerForOdd == NULL)
                      {
                                 temp2 = valueOfNode; 
                                 startPointerForOdd = temp2;
                      }
                      else
                     {
                                 temp 2 = startPointerForOdd;
                                  while(temp2 ->next != NULL)
                                  {
                                       temp2 = temp2->next;
                                   }
                                   temp2->next->value = valueOfNode;
                     }
             }           
    } 
}

> 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

while(current != NULL) {
    valueOfNode = current -> value;
    // do something with valueOfNode 

    current = current -> next;  // advance
}

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.

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?

current = H;       //current points to the first node of H.
oddStart = H1;   // oddStart points to the first node of H1.
node* newnode; // for a newnode.
while (current != NULL)
{
   nodeValue = current->value;
   if ( nodeValue % 2 != 0)
   {
      if (oddStart == NULL)
      {
         oddStart = new node;
         oddStart->value = nodeValue;
         oddStart->link = NULL;
      }
      else
      {
         temp = oddStart;
         while (temp->link != NULL)      // here 'link' is the link to the next node.
             temp = temp->link;
         newnode = new node;
         newnode->value = nodeValue;
         temp-> link = newnode;
         newnode->link = NULL;
      }
   }

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. :)

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)?

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.

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 :

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.