ah okay, I made a second node that the increment function is done on before being "traversed". Is there no way to go back to the beginning of the same list/node?
Code:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct NODE
{
struct NODE *link;
int value;
};
typedef struct NODE Node;
Node *insertFirst( Node **hptr, int val )
{
Node *node = (Node *)malloc( sizeof( Node ) );
node->value = val;
node->link = *hptr;
*hptr = node;
return node;
}
void traverse( Node *p )
{
while ( p != NULL )
{
printf("%d ", p->value );
p = p->link;
}
}
void freeList( Node *p )
{
Node *temp;
while ( p != NULL )
{
temp = p;
p = p->link;
free( temp );
}
}
int countTarget(Node *head, int target)
{
int count = 0;
while (head != NULL)
{ if (head->value == target)
count++;
head = head->link;
}
printf("the target value occured %d times ", count);
return count;
}
incrementEach(Node *head, int amount)
{
while(head != NULL)
{ head->value = head->value + amount;
head = head->link;
}
}
int main()
{
Node *head2 = NULL, *head = NULL;
int j;
for ( j=0; j<13; j++ )
{
insertFirst(&head, j);
insertFirst(&head2, j);
}
countTarget(head, 5);
traverse( head );
freeList( head );
incrementEach(head2, 100);
traverse(head2);
system("pause");
return 1;
}
It works now, but like I said, is there no way to do it without making a second node?
Edit: I figured it out, I think this is complete, just making sure it fits all the specifications now... done and submitted :]