Thread: little emergency help with linked list program

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    little emergency help with linked list program

    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.
    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;
    }
    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.

    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.
    Last edited by DLH112; 12-02-2009 at 05:32 PM.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What it *hptr initialized to?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    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 :]
    Last edited by DLH112; 12-02-2009 at 07:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM