I unexpectedly had to work a lot on some other project today, and was not able to access the internet or work on this program as much as i had wanted. It is pretty much finished but I'm still having a few problems.
The program creates a linked list and puts values in each node of the list and uses them to do different functions. I really don't know how else to better describe this program.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 *head = NULL; int j; for ( j=0; j<13; j++ ) insertFirst( &head, j ); countTarget(head, 2); traverse( head ); freeList( head ); incrementEach(head, 5); traverse(head); system("pause"); return 1; }
Note that everything but the countTarget and the incrementEach function (as well as the stuff in main that uses them) was already in the program this is based off of. My main problem that I'm having is that incrementEach only increments the last one. How can I make it so it will increment all of them in the list.(Edit: it actually makes it print out an extra node that is the last one + amount) Also I'm not entirely positive that countTarget is accurate but it seems to be working.



LinkBack URL
About LinkBacks


