Program with Linked Lists

This is a discussion on Program with Linked Lists within the C Programming forums, part of the General Programming Boards category; Hello, and thank you for taking the time to help me out. Now, I think this program has been asked ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    78

    Program with Linked Lists

    Hello, and thank you for taking the time to help me out. Now, I think this program has been asked about before, and recently, but I didn't want to hijack the thread of someone else with my questions. Please tell me if I should have done that, and I will know for next time.

    Anyway, I am working on a program that manages customer data, and has multiple functions that perform actions on the data, such as adding, removing, printing, and searching, as well as getting the size of the list.

    I believe I have the printing, removing, and size functions done correctly, but I can't really test the program, because my insert function is not working correctly. It allows me to enter all the information required, and then when I hit enter the last time, after entering the index, no matter what value I have entered, the program has an error and terminates. It happens for either of the two for loops in the code below, either the one commented out or the one that is being executed. I am running Windows Vista, and I get a message that says "Try.exe has stopped working/Windows can check online for a solution to the problem." Then two options: "Check online for a solution and close the program/Close the program." What is going wrong here? What can I do to fix the problem? Is my insert function right at all?

    Thank you in advance for helping me! Here are the important parts of my code, not including all the other functions like the size and remove functions.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct customer_data CUSTOMER_DATA;
    
    void printmenu();
    
    //Structure  
    struct customer_data
    {
           char last_name;
           char first_name;
           int id;
           float balance;
           CUSTOMER_DATA *next;
    };
    
    CUSTOMER_DATA * customer_head = NULL;
    CUSTOMER_DATA * customer_tail = NULL;
    
    // Adding a new Node after specifies number of Nodes
    int insertAtIndex(char * lastname, char * firstname, int id, 
                           float balance, int index)
    {
        int i;
        struct customer_data *temp,*prev_ptr,*cur_ptr;
         
        cur_ptr=customer_head;
    
    /*
        for(i=0;i<index;i++)
        {
             prev_ptr = cur_ptr;
             cur_ptr = cur_ptr->next;
        }
    */
        for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
        {
            if( cur_ptr->next == NULL )
            break;
        }
    
        temp=(struct customer_data*) malloc (sizeof (struct customer_data));
        temp->last_name=*lastname;
        temp->first_name=*firstname;
        temp->id=id;
        temp->balance=balance;
    
    
        prev_ptr->next=temp;
        temp->next=cur_ptr;
    
        return index;
    }
    
    void printMenu()
    {
            printf("Choice\t\tAction\n");
            printf("------\t\t------\n");
            printf("A\t\tAdd Customer\n");
            printf("D\t\tDisplay List Size\n");
            printf("P\t\tPrint List Elements\n");
            printf("R\t\tRemove Customer\n");
            printf("S\t\tSearch Customer\n");
            printf("Q\t\tQuit\n");
            printf("?\t\tDisplay Help\n\n");
    
            return;
    }
    
    int main()
    {
          int i, id, index;
          float balance;
          char last_name[20], first_name[20];
          char choice;
          struct customer_data *info;
    
          printMenu();
    
          choice = 'Z';
    
           do
           {
    
           printf("\nWhat 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:\nPlease enter the last name:\n");
                   getchar(); //to flush '\n'
                   scanf("%s", &last_name);
                   
                   printf("Please enter the first name:\n");
                   getchar(); //to flush '\n'
                   scanf("%s", &first_name);
                   
                   printf("Please enter the id (number):\n");
                   getchar(); //to flush '\n'
                   scanf("%d", &id);
                   
                   printf("Please enter the balance:\n");
                   getchar(); //to flush '\n'
                   scanf("%f", &balance);
                  
                   printf("Please enter the index to insert:\n");
                   getchar(); //to flush '\n'
                   scanf("%d", &index);
                   
                   insertAtIndex(last_name, first_name, id, balance, index);
                   
                   if (insertAtIndex(last_name, first_name, id, balance, index) != -1)
                      { printf("The customer is added at %d", index); }
                   else
                      { printf("The customer could not be added."); }
                   
                   break;
    
              case 'D':
                   printf("The size of the linked list is %d", size(), "\n");
                   break;
    
              case 'P':
                   displayLinkedList();
                   break;
    
              case 'R':
                   printf("Please enter an id(number) to remove:\n");
                   scanf("%d", id);
                   if(id != -1)
                         { printf("Customer was removed\n"); }
                   else 
                         { printf("Customer with the id was not found\n"); }
                   break;
    
              case 'S':
                   printf("Please enter an id(number) to search:\n");
                   scanf("%d", id);
                   if(searchCustomer(id) == NULL)
                         { printf("Customer with the id was not found\n"); }
                   else 
                         { searchCustomer(id); }
                   break;
    
              case 'Q':   //Quit
                   break;
    
              case '?':   //Display Menu
                   printMenu();
                   break;
    
              default:
                   printf("Unknown action\n");
                   break;
            }
    
           } while (choice != 'Q');
    
           return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't know where you live, but oftentimes people have more than one character in their first and last names.

    Anyway, in your insert function you create a prev_ptr pointer. Currently it points to New Jersey, since you didn't set it to point to any actual data in your program. Then you try to do this:
    Code:
    prev_ptr->next=temp;
    and New Jersey gets angry.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by tabstop View Post
    I don't know where you live, but oftentimes people have more than one character in their first and last names.

    Anyway, in your insert function you create a prev_ptr pointer. Currently it points to New Jersey, since you didn't set it to point to any actual data in your program. Then you try to do this:
    Code:
    prev_ptr->next=temp;
    and New Jersey gets angry.
    Thank you for the really quick response!

    Oops...I thought I put a limit of 20 characters on the input for first and last name, but that was in the main function, not in the structure...I changed it so that the first and last name declarations include [20]. But now it won't let me compile the code. I am getting some errors...so where is it that I set the size of these? Thank you!

    And I understand what you are saying...I need to make prev_ptr point to some data...do I have it point to customer_head? Is that right?

    Thank you so much for your help! I greatly appreciate it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We have a string copy function (hint: it's not "="). If you read your own code, you will see that prev_ptr needs to point to the object directly before the insertion point. Therefore you need to be keeping track of that the whole time, as once you get outside that for loop It's Too Late.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by tabstop View Post
    We have a string copy function (hint: it's not "="). If you read your own code, you will see that prev_ptr needs to point to the object directly before the insertion point. Therefore you need to be keeping track of that the whole time, as once you get outside that for loop It's Too Late.
    Okay, that makes sense. I changed my code so that I use the strcpy function, in what I at least think is the right place. Did I do it right?

    And, I am a bit confused about what I am to do with prev_ptr. How do I set it to point to the node before the insertion point? And I understand that I put it within the for loop, but do I put it at the end of that?

    Thank you so much, tabstop! :-)

    Here is my updated insertAtIndex function:

    Code:
    // Adding a new Node after specifies number of Nodes
    int insertAtIndex(char * lastname, char * firstname, int id, 
                           float balance, int index)
    {
        int i;
        struct customer_data *temp,*prev_ptr,*cur_ptr;
         
        cur_ptr=customer_head;
    
    /*
        for(i=0;i<index;i++)
        {
             prev_ptr = cur_ptr;
             cur_ptr = cur_ptr->next;
        }
    */
        for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
        {
            cur_ptr = cur_ptr->next;
            
            if( cur_ptr->next == NULL )
            break;
        }
    
        temp=(struct customer_data*) malloc (sizeof (struct customer_data));
       
        strcpy(temp->last_name,lastname);
        strcpy(temp->first_name,firstname);
        temp->id=id;
        temp->balance=balance;
    
    
        prev_ptr->next=temp;
        temp->next=cur_ptr;
    
        return index;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can do it the easy way: use a double linked list. Or you can do it the hard way: loop through with another pointer, until p->next == current.


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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    strcpy looks right now.

    So think about how you ever keep track of the most recent anything. If you start in California, and then go to Nevada, your most recent state was California. Once you get to Utah, your most recent state was Nevada....

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quzah:
    I think I'll have to do it the hard way, because I don't really know how to implement a double linked list...we haven't gone over that in my class yet, I don't think. So...I am guessing I use a while loop, so I changed that part of the code to this:

    Code:
        
    for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
        {
            while (p->next != cur_ptr)
            {
                  cur_ptr = cur_ptr->next;
            
                  if( cur_ptr->next == NULL )
                  break;
            }
        }
    But the same thing is still happening. The code has that same error and terminates. Am I doing it wrong still? Is something else unrelated to this occurring?

    Thank you very much for your help!!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I do not see the words "prev_ptr" anywhere in that code.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Okay, I changed the for loop again, so that prev_ptr is doing what I think its supposed to. Here is what it looks like now:

    Code:
        for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
        {
            while (p->next != cur_ptr)
            {
                  prev_ptr = cur_ptr;
                  cur_ptr = cur_ptr->next;
                  if( cur_ptr->next == NULL )
                  break;
            }
        }
    The same thing is still happening, though. I still get that error when I enter all the information. Am I still not doing it correctly?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have one too many loops still, and I don't even know where p came from.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Well, I tried to combine your response with that Quzah, which was probably not a good idea. I'm just trying to incorporate all the ideas, but it probably won't work very well.

    Alright, I commented out the while loop, so it looks like this:

    Code:
            for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
            {
                 //while (p->next != cur_ptr)
                 //{
                        prev_ptr = cur_ptr;
                        cur_ptr = cur_ptr->next;
                        
                        return index;
                        
                        if( cur_ptr->next == NULL )
                        break;
                // }
            }
    But again, I am getting that same error.

    Thank you for your help!!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    return index!?!?!?!?!?!!?!?!?!?

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Well, I have to return the index that it is being added at...is that not right? I just realized when looking at my code that I didn't have the right return statements, because I am supposed to return -1 if it doesn't work and the index if it does work.

    Here is my whole code, excluding the other functions:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct customer_data CUSTOMER_DATA;
    
    void printmenu();
    
    //Structure  
    struct customer_data
    {
           char last_name[20];
           char first_name[20];
           int id;
           float balance;
           CUSTOMER_DATA *next;
    };
    
    CUSTOMER_DATA * customer_head = NULL;
    CUSTOMER_DATA * customer_tail = NULL;
    
    // Adding a new Node after specifies number of Nodes
    int insertAtIndex(char * lastname, char * firstname, int id, 
                           float balance, int index)
    {
        int i;
        struct customer_data *temp,*prev_ptr,*cur_ptr, *p;
         
        cur_ptr=customer_head; 
        
        if(index > size()+1 || index < 0)
        {
           printf("\nInsertion at given location is not possible\n ");
           return -1;
        }
        else
        {
            for( i = 0; cur_ptr && i < index; i++, cur_ptr = cur_ptr->next )
            {
                 //while (p->next != cur_ptr)
                 //{
                        prev_ptr = cur_ptr;
                        cur_ptr = cur_ptr->next;
                        
                        return index;
                        
                        if( cur_ptr->next == NULL )
                        break;
                // }
            }
        }
        
        temp=(struct customer_data*) malloc (sizeof (struct customer_data));
       
        strcpy(temp->last_name,lastname);
        strcpy(temp->first_name,firstname);
        temp->id=id;
        temp->balance=balance;
    
    
        prev_ptr->next=temp;
        temp->next=cur_ptr;
    }
    
    void printMenu()
    {
            printf("Choice\t\tAction\n");
            printf("------\t\t------\n");
            printf("A\t\tAdd Customer\n");
            printf("D\t\tDisplay List Size\n");
            printf("P\t\tPrint List Elements\n");
            printf("R\t\tRemove Customer\n");
            printf("S\t\tSearch Customer\n");
            printf("Q\t\tQuit\n");
            printf("?\t\tDisplay Help\n\n");
    
            return;
    }
    
    int main()
    {
          int i, id, index;
          float balance;
          char last_name[20], first_name[20];
          char choice;
          struct customer_data *info;
    
          printMenu();
    
          choice = 'Z';
    
           do
           {
    
           printf("\nWhat 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:\nPlease enter the last name:\n");
                   getchar(); //to flush '\n'
                   scanf("%s", &last_name);
                   
                   printf("Please enter the first name:\n");
                   getchar(); //to flush '\n'
                   scanf("%s", &first_name);
                   
                   printf("Please enter the id (number):\n");
                   getchar(); //to flush '\n'
                   scanf("%d", &id);
                   
                   printf("Please enter the balance:\n");
                   getchar(); //to flush '\n'
                   scanf("%f", &balance);
                  
                   printf("Please enter the index to insert:\n");
                   getchar(); //to flush '\n'
                   scanf("%d", &index);
                   
                   insertAtIndex(last_name, first_name, id, balance, index);
                   
                   if (insertAtIndex(last_name, first_name, id, balance, index) != -1)
                      { printf("The customer is added at %d", index); }
                   else
                      { printf("The customer could not be added."); }
                   
                   break;
    
              case 'D':
                   printf("The size of the linked list is %d", size(), "\n");
                   break;
    
              case 'P':
                   displayLinkedList();
                   break;
    
              case 'R':
                   printf("Please enter an id(number) to remove:\n");
                   scanf("%d", id);
                   if(id != -1)
                         { printf("Customer was removed\n"); }
                   else 
                         { printf("Customer with the id was not found\n"); }
                   break;
    
              case 'S':
                   printf("Please enter an id(number) to search:\n");
                   scanf("%d", id);
                   if(searchCustomer(id) == NULL)
                         { printf("Customer with the id was not found\n"); }
                   else 
                         { searchCustomer(id); }
                   break;
    
              case 'Q':   //Quit
                   break;
    
              case '?':   //Display Menu
                   printMenu();
                   break;
    
              default:
                   printf("Unknown action\n");
                   break;
            }
    
           } while (choice != 'Q');
    
           return 0;
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So do you know what the return keyword even does?

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. Replies: 2
    Last Post: 01-18-2003, 12:32 AM
  4. I need some help on my linked lists app
    By valar_king in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:36 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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