A little help with Linked List functions

This is a discussion on A little help with Linked List functions within the C Programming forums, part of the General Programming Boards category; I need to create a linked list of customer data. One functions needs to insert a new customer data at ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    A little help with Linked List functions

    I need to create a linked list of customer data. One functions needs to insert a new customer data at the location indicated by the parameter "index". If index is 0 then it should be inserted as the first data in the linked list. I think I coded that function right, here it is:
    Code:
    int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
    {
        int counter = 0;
        
        struct Contact * newContact, *temp;
        newContact = (struct Contact *) malloc(sizeof(struct Contact));
        
        strcpy(newContact->last_name,lastname);
        strcpy(newContact->first_name,firstname);
        newContact->id = id;
        newContact->balance = balance;
        
        if(head == NULL) 
        { 
          head=newContact; 
          newContact->next = NULL; 
        }
        
        else
        {
          temp = head;
            
          while(counter != index)
          {
           temp=temp->next;
          }          
          newContact->next=NULL; 
          temp->next=newContact;
    
        }
        return 0;
    }
    The second function just returns the number of entries in the linked list. Here is that function:
    Code:
    int size()
    {
     struct Contact *conductor;
     conductor = head;
     int total = 0;
     
     while(conductor !=NULL) 
     {
        total++;
        conductor =  conductor->next;
     }
     
     return total;   
    }
    The third function prints all entries in the linked list it looks like this:
    Code:
    void displayLinkedList( )
    {
      struct Contact *conductor;
      
      conductor = head;
     
         while ( conductor != NULL ) 
         {
           printf("%s\n", conductor->last_name);
           printf("%s\n", conductor->first_name);
           printf("%d\n", conductor->id);
           printf("%f\n", conductor->balance);
           printf("\n");
           conductor = conductor->next;
         }
    
        
    }
    Now in my main function I have a switch case to act as a menu. I'm not sure if its my function causing the problems, or a logic error in my do-while switch loop. When I run my program I can always input my first choice fine. But when I try to input my second choice it just automatically goes to the default case in the switch. Here is that-
    Code:
    do
           {
     
             printf("What action would you like to perform?\n");
     
             choice = getchar();
             getchar(); //to flush '\n'
             choice = toupper(choice);
           
             switch (choice)
             {
                  case 'A':
                       printf("Please enter a customer to add: \n");
                       printf("Please enter the last name: \n");
                       
                       i = 0;
                       do
                       {
                           inputChar = getchar(); 
                           if (inputChar != '\n' && inputChar != '\r')
                           {
                              *(last_name + i) = inputChar;
                           }
     
                           i++;
                        } while (i < 50 && inputChar != '\n' && inputChar != '\r');
                        *(last_name + i) = '\0';
                        
                        printf("Please enter the first name: \n");
                        
                        i = 0;
                        do
                        {
                            inputChar = getchar();
                            if (inputChar != '\n' && inputChar != '\r')
                            {
                               *(first_name + i) = inputChar;
                            }
                            
                            i++;
                        } while (i<50 && inputChar != '\n' && inputChar != '\r');
                       *(first_name + i) = '\0'; 
                       
                       printf("Please enter the id(number): \n");
                       scanf("%d", &id);
                       
                       printf("Please enter the balance: \n");
                       scanf("%f", &balance);
                       
                       printf("Please enter the index to insert: \n");
                       scanf("%d",&index);
                       
                       bool_index = insertAtIndex(last_name, first_name, id, balance, index);
                       if(bool_index < 0)
                         printf("The Customer could not be added\n");
                        else
                            printf("The Customer is added at %d\n", index); 
                       
                       break;
                      
                  case 'D':
                       printf("The size of the linked list is %d\n", size());
                       break;
                   
                  case 'P':
                       if(size() == 0)
                             printf("The list is empty\n");
                       else
                             displayLinkedList( );
                       break;
                  
                  /*case 'R':
                       printf("Please enter an id(number) to remove: \n");
                       scanf("%d", &id);
                       bool_remove = removeCustomer(id);
                       
                       if(bool_remove < 0)
                             printf("Customer with the id was not found");
                       else
                             printf("Customer was removed");  
                            
                       break;
                       
                   case 'S': */                                   
                       
                  default:
                       printf("Unknown action\n");
                       break;
             }
           } while (choice != 'Q');
    Any help would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What happens when you make a choice? You press S. You press enter. The first pass at the loop takes the S. The next pass takes enter.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Ok thanks, I shoud have overlooked it. I have my option menu working fine, but it seems like my insertAtIndex function is not working properly. I can add one new element fine but when I try to add another the program crashes. Heres that function(with the struct):
    Code:
    struct Contact
    {
      char  * last_name;
      char * first_name;
      int id;
      float balance;
      struct Contact *next;
      
    };
    
    struct Contact * head = NULL;
    
    int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
    {
        int counter = 0;
        
        struct Contact * newContact, *temp;
        newContact = (struct Contact *) malloc(sizeof(struct Contact));
        
        strcpy(newContact->last_name,lastname);
        strcpy(newContact->first_name,firstname);
        newContact->id = id;
        newContact->balance = balance;
        
        if(head == NULL) 
        { 
          head=newContact; 
          newContact->next = NULL; 
        }
        
        else
        {
          temp = head;
            
          while(counter != index)
          {
           temp=temp->next;
          counter++;
    }          
          newContact->next=NULL; 
          temp->next=newContact;
    
        }
        
        return 0;
    }
    I thought I did my memory allocation right, or does it the problem stem from something else?
    Last edited by matt_570; 09-25-2009 at 10:18 PM. Reason: typo

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you have three entries in the list and you try to insert at index 5.....

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    I think I understand what you're implying, however after working on the function again today for hours I'm still having the same problem. I ended up with three different functions and none of them working properly Heres is the cleanest one:-

    Code:
    int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
    {
        int i;
        
        struct Contact * newContact, *conductor;
        conductor = head;
        
        newContact = (struct Contact *) malloc(sizeof(struct Contact));
        
        strcpy(newContact->last_name,lastname);
        strcpy(newContact->first_name,firstname);
        newContact->id = id;
        newContact->balance = balance;
        
        newContact->next = NULL;
        
        if(conductor == NULL) 
        { 
    
          head=newContact; 
          newContact->next = NULL; 
        }
        
        else
        {
        
            
          for(i = 0; i< index; i++)
          {
           conductor=conductor->next;
           
          }           
          conductor->next=newContact;
    
        }
            
        return index;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're still doing it wrong. You should be doing something like:
    Code:
    for( i = 0; contuctor && i < index; i++, conductor = conductor->next )
    {
        if( conductor->next == NULL )
            break;
    }
    Or something close to that. Basically you have to make sure that there's enough room in your list, otherwise you'll just blow past the end again when you try to enter 5 in a list of 3. Also, you have to actually fix your list, in case you want to enter before 4 in a list of 4. So after that loop, you need:
    Code:
    newContact->next = conductor->next;
    conductor->next = newContact;

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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