| | |
Singly Link List help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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)
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; } } } }
Last edited by core2d; Oct 5th, 2008 at 12:41 am.
> 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
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.
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)
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.
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
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?
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.
> 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)
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.
Bhoot
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
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 :
in the while(current!=NULL) loop or you can replace the whole while() loop with a for() loop as demonstrated by Salem.

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)
current = current->link;
Bhoot
![]() |
Similar Threads
- Deleting Pointed node in Singly Linked List (C)
- Removing an item from head of linked list (C)
- del nth last node in a singly link list using only 1 traversal (C)
- Link-listed addressbook (C)
- Link list Query (C)
- qustion about the double link list (C)
Other Threads in the C++ Forum
- Previous Thread: C++.
- Next Thread: Convert integer to string
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






