HOW TO ITERATE OVER EACH ELEMENT? HELP PLEASe!

void insertHead(T value) = 0;
{

for (Node* next_pointer)
if ( head!= NULL)
{
Node* curr = new Node (value, head);
curr->next = head;
head = curr;
}
}
Do not allow duplicate values in the list.

I know i need to iterate over each element of the list with a for loop and compare it to the new value...I don't know how to do it!! (code). Could you guys help me please??? any example using a code???
closed account (D80DSL3A)
Going through the list until the end is reached, or equal value is found:
1
2
Node* curr = NULL;
for( curr = head; (curr != NULL)&&(curr->val != value); curr = curr->next);// empty statement. All is done. 


No match was found if we find that curr == NULL, as that's what terminated the for loop. So,
1
2
if( curr == NULL )// value is new and should be added
    head = new Node(value,head);// I bet this will work! 

You shouldn't need the 3 lines for allocating a node (though your 3 are correct).
The 1 liner above does all the same things.
Last edited on
void insertHead(T value) = 0;
{
Node* curr = NULL; //could u explain me this line?
for( curr = head; (curr != NULL)&&(curr->val != value); curr = curr->next); //what do you mean by empty statement?
if( curr == NULL ) //value is new?????
head = new Node(value,head); //???

if ( head!= NULL)
{
Node* curr = new Node (value, head);
curr->next = head;
head = curr;
}
}
Could you explain what you did step by step? I'm a beginner so I really want to understand this stuff.
Topic archived. No new replies allowed.