Thread: Program with Linked Lists

  1. #16
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Oh, I didn't realized it exited the function, I thought it just returned the given result without exiting...okay, so where do I put the return statement? Is that why it is giving me that error?

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Okay, I did some editing, and I think I got the return statement in the right place. But I am still getting that same error. What am I doing wrong?

    Here is my code for the insert method right now:

    Code:
    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;
    
        if(index > size()+1 || index < 0)
        {
           printf("\nInsertion at given location is not possible\n ");
           return -1;
        }
        else
        {
           for(i=1;i<index;i++)
           {
              prev_ptr=cur_ptr;   // t will be holding previous value of r
              cur_ptr=cur_ptr->next;
           }
    
           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;
        }
    }

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your insert function will now no longer insert at the head of a list (which includes when the list is empty).

  4. #19
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Okay, yeah, I thought that might be the problem, because that code was not having the error for values other than 0. Okay, so I changed my code around again, and now I think it is right. Here is my code for the insert function:

    Code:
    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;
    
        if(index > size()+1 || index < 0)
        {
           printf("\nInsertion at given location is not possible\n ");
           return -1;
        }
        
        if (index == 0) 
        {
           struct customer_data *temp;
    
           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;
    
           if ( customer_head == NULL)
           {
              customer_head=temp;
              customer_head->next=NULL;
           }
           else
           {
               temp->next=customer_head;
               customer_head=temp;
           }
           return 0;
        }
        
        else
        {
           for(i=-1;i<index;i++)
           {
              prev_ptr=cur_ptr;   // t will be holding previous value of r
              cur_ptr=cur_ptr->next;
           }
    
           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;
        }
    }
    It is working, I think, but now I am having another problem with the program. Now, it is not letting me do anything else after adding a customer to the list. It just prints out my error message, "Unknown Action," even when I enter one of the programmed commands, like P or D. So I still can't test it. Here is my main function, as well as my menu:

    Code:
    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;
    }
    Can you help me figure out why it is not letting me enter another command after it adds the customer?

    Thank you so much for your help, tabstop!

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have getchar() everywhere they are not needed, and nowhere where they are.

  6. #21
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Okay...so take out the getchar() calls in the main function, and put them where? Since I already have a call to getchar() before the switch statement, do I keep that one, or do I move it too? I get that I need to have the program get the character after every time it goes through one of the switch statements, but I don't really know how to do this other than what I have.

    Thank you again!!

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's probably a reason you're using getchar, but I don't know what it is. In any event, you never need an extra getchar after-getchar-and-before-scanf, but you do need one after-scanf-and-before-getchar.

  8. #23
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Okay, I think I understand...so instead of this:

    Code:
    printf("Please enter a customer to add:\nPlease enter the last name:\n");
    getchar();
    scanf("%s", &last_name);
    I should be doing this:

    Code:
    printf("Please enter a customer to add:\nPlease enter the last name:\n");
    scanf("%s", &last_name);
    getchar();
    Is that right? Also, what do I do with the call to getchar() above the switch statement? Do I take it out or leave it in?

    Thank you so much, tabstop!! I so appreciate your help with this. :-)

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, my point was: are you required to use getchar? If not, why are you using it when you should be using
    Code:
    scanf(" %c", &choice);
    instead?

  10. #25
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    So I made that change for all of my statements in the first menu option, and it works! But now it is adding the entry to the list twice for some reason. When I display the elements of the list it shows the entry twice, and when I ask for the size of the list, it shows 2 instead of 1. Can you help me figure out this problem? I thought I had the insert method working fine, but apparently not...

    Thanks for the help!! :-D


    EDIT: Okay, I didn't see your response before posting, so I guess I didn't understand correctly. I am not required to use getchar(), I was just following the example of a menu that my teacher did for one of our other assignments. So I changed the code so that it uses the line you provided above, and here is my main function:

    Code:
    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");
           scanf(" %c", &choice);
           getchar(); //to flush '\n'
           choice = toupper(choice);
    
           switch(choice)
            {
              case 'A':
                   printf("Please enter a customer to add:\nPlease enter the last name:\n");
                   scanf("%s", &last_name);
                   getchar();
                   
                   printf("Please enter the first name:\n");
                   scanf("%s", &first_name);
                   getchar();
                   
                   printf("Please enter the id (number):\n");
                   scanf("%d", &id);
                   getchar();
                   
                   printf("Please enter the balance:\n");
                   scanf("%f", &balance);
                   getchar();              
                  
                   printf("Please enter the index to insert:\n");
                   scanf("%d", &index);
                   getchar();
                   
                   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;
    }
    Is this right?
    Last edited by vileoxidation; 09-27-2009 at 04:05 PM.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. All instances of getchar in your code are extraneous and unnecessary and should be removed.
    2. If you call insertAtIndex twice (and you do) then you shouldn't act surprised when your elements are inserted twice.

  12. #27
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Okay, they all went away now. And I understand about calling it twice now...I didn't realize that using it in the if statement would make it execute again, I thought it would just access the return value of it after it ran the first time. So I changed that. I think its mostly working now, except that when I try to insert the second member of the list at index 1, I get that same Windows error that I was getting before, and the program terminates. What causes this? How do I fix it this time?

    Here is my code for the insert function, as well as the main method:
    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;
    
    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;
    
        if(index > size()+1 || index < 0)
        {
           printf("\nInsertion at given location is not possible\n ");
           return -1;
        }
        
        if (index == 0) 
        {
           struct customer_data *temp;
    
           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;
    
           if ( customer_head == NULL)
           {
              customer_head=temp;
              customer_head->next=NULL;
           }
           else
           {
               temp->next=customer_head;
               customer_head=temp;
           }
           return 0;
        }
        
        else if (index > 0)
        {
           for(i=-1;i<index;i++)
           {
              prev_ptr=cur_ptr;   // t will be holding previous value of r
              cur_ptr=cur_ptr->next;
           }
    
           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;
        }
    }
    
    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");
           scanf(" %c", &choice);
           choice = toupper(choice);
    
           switch(choice)
            {
              case 'A':
                   printf("Please enter a customer to add:\nPlease enter the last name:\n");
                   scanf("%s", &last_name);
                   
                   printf("Please enter the first name:\n");
                   scanf("%s", &first_name);
                   
                   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);
                   
                   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;
    }
    Thank you so much for helping me get this program working correctly!

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have this:
    Code:
    for(i=-1;i<index;i++)

  14. #29
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Hmm...I don't know, to be quite honest. It was probably something I carried over from the code I was using as an example, and it just never got out of there. Okay, I commented it out (and will remove it right now) and it works fine. Now I just have to get my search and remove methods working, and I'll be done! Thank you so much for getting me to this point, tabstop!!

    I know I'm probably getting on your nerves, and I apologize for that, but I might have some questions about the search and remove functions. Should I post them in this thread? Thank you for your patience with me!

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you can't get rid of it. You just can't start counting at -1, because that's not where your linked list starts (it appears to start at 0).

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, 04:09 AM
  3. Replies: 2
    Last Post: 01-18-2003, 01: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