Linked List errors, and need help adjusting.

This is a discussion on Linked List errors, and need help adjusting. within the C Programming forums, part of the General Programming Boards category; Here is the question posed. Create a structure that has one variable called value and one pointer to the list ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Linked List errors, and need help adjusting.

    Here is the question posed.

    Create a structure that has one variable called value and one pointer to the list (making it a linked list). Prompt for 5 values from the keyboard as input and store them in the linked list. Print out the current contents of the list. Allow the user to add one more value to the linked list, and print the contents of the list again.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 30
    struct ValueList
    {
           char valuename[MAX];
           struct ValueList *value;
           };
    struct ValueList *firstvalue;
    
    int main()
    {
        void readValue();
        void display();
        
        firstvalue=NULL;
        readValue();
        display();
        
        return 0;
    }
    
    void readValue()
    {
         char value[MAX];
         void insert(char *);
         
         printf("Please enter 5 whole numbers, one per line");
         printf("\nTo stop entering whole numbers, enter a single x\n");
         while(1)
         {
                 printf("Enter a whole number: ");
                 gets(value);
                 if (strcmp(value, "x") ==0)
                 break;
                 insert(value);
                 }
                 }
    
    void insert(char *value)
    {
         struct ValueList *linear Locate(*char);
         struct Valuelist *newnode, *here;
         
         newnode= (struct ValueList *) malloc(sizeof(struct ValueList));
         if (newnode == (struct ValueList *) NULL)
         {
                     printf("\n\nCould not allocate enough space\n");
                     exit(1);
                     }
         if(firstValue==NULL)
         {
                             newnode->nextAddr=NULL;
                             firstValue=newaddr;
                             }
         else if(strcmp(value, firstValue->value) <0)
         {
              newaddr->nextAddr = firstValue;
              firstValue=newaddr;
              }
         else
         {
             here = linear Locate(value);
             newaddr->nextAddr = here->nextAddr;
             here->nextAddr = newaddr;
             }
             
         strcpy(newaddr->value,value);
         }
         
         
    struct ValueList *linear Locate(char *value)
    {
           struct ValueList *one, *two;
           one = firstValue;
           two = one->nextAddr;
           
    if(two==NULL)
    return (one);
    while(1)
    {
     if(strcmp(value, two->value) <0)
     break;
     else if(two->nextAddr == NULL)
     {
          one = two;
          break;
    }
    else
    {
        one = two;
        two = one->nextAddr;
    }
    }
    return(one);
    }
    
    void display()
    {
         struct ValueList *contents;
         
         contents = firstValue;
         printf("\nThe numbers currently in the list are\n");
         
         while(contents != NULL)
         {
         printf("%s\n",contents->value);
         contents = contents->nextAddr;
         }
         }
    Now I know I am supposed to add more values however the example from my textbook is garbage and I am suffering from a dislocated shoulder so I am really not liking coding with two semi functioning arms and hands.

    Any help would be appreciated and I thank all of you for the help in advance.

    Todd

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I didn't read all the code
    But here are some notes:
    1. Don't cast malloc http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    2. is "linear Locate" a function name? Then it should be one word like linearLocate or linear_locate base on the naming conventions you choose
    3. firstvalue=NULL; in your main you affectivly loose the pointer to your list making it unaccessible with a lot of memory leaks.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 30
    struct ValueList
    {
           char valuename[MAX]; /* change valuename to value */
           struct ValueList *value; /* change value to nextAddr */
           };
    struct ValueList *firstvalue;  /* change firstvalue to firstValue */
    
    int main()
    {
        void readValue();
        void display();
        
        firstvalue=NULL; /* change firstvalue to firstValue */
        readValue();
        display();
        
        return 0;
    }
    
    void readValue()
    {
         char value[MAX];
         void insert(char *);
         
         printf("Please enter 5 whole numbers, one per line");
         printf("\nTo stop entering whole numbers, enter a single x\n");
         while(1)
         {
                 printf("Enter a whole number: ");
                 gets(value);
                 if (strcmp(value, "x") ==0)
                 break;
                 insert(value);
                 }
                 }
    
    void insert(char *value)
    {
         struct ValueList *linear Locate(*char); /* change linear Locate to Locate */
         struct Valuelist *newnode, *here; /* change Valuelist to ValueList and
                                            define another struct Valuelist pointer 
                                            called newaddr */
         
         newnode= (struct ValueList *) malloc(sizeof(struct ValueList));
         if (newnode == (struct ValueList *) NULL) 
         {
                     printf("\n\nCould not allocate enough space\n");
                     exit(1);
                     }
         if(firstValue==NULL)
         {
                             newnode->nextAddr=NULL;    
                             /* add nextaddr = newnode; here */
                             firstValue=newaddr;
                             }
         else if(strcmp(value, firstValue->value) <0)
         {
              /* add nextaddr = newnode; here */
              newaddr->nextAddr = firstValue;
              firstValue=newaddr;
              }
         else
         {
             /* add nextaddr = newnode; here */
             here = linear Locate(value);  /* change linear Locate to Locate */
             newaddr->nextAddr = here->nextAddr;
             here->nextAddr = newaddr;
             }
             
         strcpy(newaddr->value,value);
         }
         
         
    struct ValueList *linear Locate(char *value) /* change linear Locate to Locate */
    {
           struct ValueList *one, *two;
           one = firstValue;
           two = one->nextAddr;
           
    if(two==NULL)
    return (one);
    while(1)
    {
     if(strcmp(value, two->value) <0)
     break;
     else if(two->nextAddr == NULL)
     {
          one = two;
          break;
    }
    else
    {
        one = two;
        two = one->nextAddr;
    }
    }
    return(one);
    }
    
    void display()
    {
         struct ValueList *contents;
         
         contents = firstValue;
         printf("\nThe numbers currently in the list are\n");
         
         while(contents != NULL)
         {
         printf("%s\n",contents->value);
         contents = contents->nextAddr;
         }
         }
    just follow my comments and it should work.
    for some reason you changed valuename to value, value to nextAddr, newnode to newaddr and firstvalue to firstValue a few lines after insert.
    also function function names can not have spaces e.g your linear Locate should be linear_Locate or one word Locate.
    Last edited by peterchen; 01-06-2007 at 11:25 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21