I am working on a program in C right now that deals with linked lists. Here is the assignment statement:

"Write a C program called Assignment4.c, that creates a linked list of Customer data. Each entry in the list is to have the customer’s last name, first name, id (int), balance (float), and a pointer to the next customer data. Create a struct for the Customer first. Then create a pointer (“head”) that points to the first element in the linked list (initially NULL), as a global variable."

There are also a few functions we are supposed to define:
int insertAtIndex
int size
void displayLinkedList
int removeCustomer
struct Customer * searchCustomer
and of course, int main

So I started at the top, and right now I have code for the first three functions, but I am not sure my code is right at all, because I can't compile my code since I haven't written the main method. I am having a few compilation errors, however, including the fact that in my displayLinkedList function the variables last_name, first_name, etc. are undeclared. The reason I have them undeclared like that is that I don't know how to access the variables from my linked list and print them. How do I do this?

Also, are the other two functions that I have code for correct at all?

And last, I am confused about how to do the remove_Customer function. Can someone help me with the pseudo code for this function?

Thank you guys in advance for helping me with this!

Here is what I have so far:

/****
Assignment4.c
****/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

typedef struct customer_data CUSTOMER_DATA;

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;

//struct customer_data * BuildList(newlastname, newfirstname, newid, newbalance) 
int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
{
    CUSTOMER_DATA * newcustomer;
       
    if ((newcustomer = malloc(sizeof(CUSTOMER_DATA))) == NULL) 
         { abort(); }
      
    newcustomer->last_name = *lastname;
    newcustomer->first_name = *firstname;
    newcustomer->id = id;
    newcustomer->balance = balance;
    newcustomer->next = NULL;
       
    if (!customer_head) customer_head = newcustomer;
    else customer_tail->next = newcustomer;
      
    customer_tail = newcustomer;

    return index;
}

int size()
{
     int size;
     
     size = sizeof(CUSTOMER_DATA);
     
     return size;
}

void displayLinkedList( )
{
     int i;
     for (i; i < sizeof(CUSTOMER_DATA); i++)
     {
          printf("Last Name: ", last_name);
          printf("First Name: ", first_name);
          printf("Customer ID: ", id);
          printf("Customer Balance: $ ", balance);
     }
}

Whenever I've worked with linked lists they have always had a head node, internal node and a tail node. The job of the head node is to point to the first node which is the the tail node initially. The job of the tail node is to signal the end and the internal node's well they carry the data and a pointer to the next internal node or tail node.

So they would look something like

Head node->{internal node  } ->{internal node }->tail node
           {data structure }   {data structure}

You should Google around...There is lots of linked list examples on the net

Whenever I've worked with linked lists they have always had a head node, internal node and a tail node. The job of the head node is to point to the first node which is the the tail node initially. The job of the tail node is to signal the end and the internal node's well they carry the data and a pointer to the next internal node or tail node.

So they would look something like

Head node->{internal node  } ->{internal node }->tail node
           {data structure }   {data structure}

You should Google around...There is lots of linked list examples on the net

Yeah, I did a bit of searching, and that is how I figured out the code that I have. I found a few websites that instructed the user about how to create and add to a linked list, and I just modified the code to suit my situation. Did I do it totally wrong?

I am confused about your advice, though. I have a head node and a tail node, don't I? customer_head and customer_tail are the things I am talking about. Is that right? Are you saying I need to add an internal node?

Once I add the internal node, where do I use it? How do I implement it in my code?

One thing I noticed right away is that your insertAtIndex function does not have a for loop. If you want to insert a node at the fifth index, then you will need a for loop and you will have to go to the 'next' node four times. (It looks like you are treating your insertAtIndex method as if it were an "add" method (which usually inserts at the end).

And last, I am confused about how to do the remove_Customer function. Can someone help me with the pseudo code for this function?

To remove a customer, you would have to make the node before the customer you want to remove point to the node after the customer. Think about it like this:

You have a list of customers. For simplicity, let's say you have three customers total and you want to remove the middle customer. So you'd have the following data (according to your project description):

Customer1, Customer2, Customer3. Where Customer1->next = Customer2, and Customer2->next=Customer3.

So if you want to remove a customer, what you need to do is take that customer out of your list. To take customer 2 out of your list, you'd need to make customer 1 point to customer 3, then you'd need to "free" customer 2 if you previously allocated memory for that customer. So customer1.next = customer3; The trickiest part of all of this is that you need to use some sort of loop in order to find the customer in your list that you want to delete. And you also need to store a pointer to the previous customer, otherwise, once you find the customer that you want to delete, you have already passed the customer before him. (And you need to set the customer before him to point to the customer after him). I hope that isn't too confusing. I'd provide pseudocode but there are plenty of examples on the net that do exactly that better than I could.

Also, sorry but I had a little picture to describe what I was tsalking about but the formatting got messed up and now I'm tired of typing.

One thing I noticed right away is that your insertAtIndex function does not have a for loop. If you want to insert a node at the fifth index, then you will need a for loop and you will have to go to the 'next' node four times. (It looks like you are treating your insertAtIndex method as if it were an "add" method (which usually inserts at the end).


To remove a customer, you would have to make the node before the customer you want to remove point to the node after the customer. Think about it like this:

You have a list of customers. For simplicity, let's say you have three customers total and you want to remove the middle customer. So you'd have the following data (according to your project description):

Customer1, Customer2, Customer3. Where Customer1->next = Customer2, and Customer2->next=Customer3.

So if you want to remove a customer, what you need to do is take that customer out of your list. To take customer 2 out of your list, you'd need to make customer 1 point to customer 3, then you'd need to "free" customer 2 if you previously allocated memory for that customer. So customer1.next = customer3; The trickiest part of all of this is that you need to use some sort of loop in order to find the customer in your list that you want to delete. And you also need to store a pointer to the previous customer, otherwise, once you find the customer that you want to delete, you have already passed the customer before him. (And you need to set the customer before him to point to the customer after him). I hope that isn't too confusing. I'd provide pseudocode but there are plenty of examples on the net that do exactly that better than I could.

Also, sorry but I had a little picture to describe what I was tsalking about but the formatting got messed up and now I'm tired of typing.

Okay, so that makes sense to me...but where do I put the for loop in the insertAtIndex function? Do I put it before all the pointer assignment statements, and include those in the loop?

Also, I think i understand how to do the remove function. If I could get my IDE to work on this computer I would try it out, but I can't, so I'll have to do it later.

And last, can you tell me how to access the variables like first_name and last_name in my other functions? For instance, in my displayLinkedList function, how do I write the code so that I can use the last_name variable from the insert function?

Thank you so much for helping me with this!! :-)

Okay, I have changed things around a bit, and have all of the functions written except for the remove function. I am having a few problems though. First, when I go to add a customer, it lets me type in the last name, but then prints the prompt for the first name followed right away by the prompt for the id, without allowing the user to type anything in for the first name. So I can't even test the code and see if it is working.

Also, I don't think the way I am accessing the variables is right, because I am declaring a new "node" in each function and accessing the properties of that, so I am not thinking that will do the right thing. Can someone help me figure out how to access the variables from the linked list?

Anyway, here is my code:

/****
Assignment4.c
****/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

typedef struct customer_data CUSTOMER_DATA;

void printmenu();

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;

int insertAtIndex(char * lastname, char * firstname, int id, 
                       float balance, int index)
{
    struct customer_data *temp1, *temp2, *q;
    int i, loc, num;

    temp1 = q;
    for ( i = 0; i < index; i++ )
    {
        temp1 = temp1 -> next;

        if ( temp1 == NULL )
        {
            printf ( "\nThere are less than %d elements in list", loc );  
            return -1;       
        }
        else
            return index;
    }

    /* insert new node */
    temp2 = (struct customer_data*) malloc (sizeof (struct customer_data));
    temp2 -> last_name = *lastname;
    temp2 -> first_name = *firstname;
    temp2 -> id = id;
    temp2 -> balance = balance;
    temp2 -> next = temp1 -> next;
    temp1 -> next = temp2;
}


int size()
{
     int size;
     
     size = sizeof(CUSTOMER_DATA);
     
     return size;
}

void displayLinkedList( )
{
     struct customer_data *info;
     int i;
     for (i; i < sizeof(CUSTOMER_DATA); i++)
     {
          printf("\n");
          printf("\nLast Name: ", info->last_name);
          printf("\nFirst Name: ", info->first_name);
          printf("\nCustomer ID: ", info->id);
          printf("\nCustomer Balance: $ ", info->balance);
          info = info -> next;
     }
}

struct Customer * searchCustomer(int id)
{
       struct customer_data *temp;
       temp = customer_head; //initialize temp to head
       while(temp !=NULL) 
       {
              printf("The customer's last name is ", temp->last_name);
              printf("The customer's first name is ", temp->first_name);
              printf("The customer's balance is $ ", temp->balance);
              return ;
       }
       temp=temp->next;
}


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[100], first_name[100];
      char choice;
      struct customer_data *info;

      printMenu();

      choice = 'Z';

       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:\nPlease enter the last name:\n");
               getchar(); //to flush '\n'
               scanf("%c", last_name);
               
               printf("Please enter the first name:\n");
               getchar(); //to flush '\n'
               scanf("%c", 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 != -1)
                  { printf("The customer is added at ", index); }
               else
                  { printf("The customer could not be added."); }
               
               break;

          case 'D':
               printf("The size of the linked list is ", 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(id = NULL)
                     { printf("Customer with the id was not found\n"); }
               else 
                     { 
                     printf("The customer's last name is", info->last_name, "\n");
                     printf("The customer's first name is", info->first_name, "\n");
                     printf("The customer's balance is", info->balance, "\n"); 
                     }
               break;

          case 'Q':   //Quit
               break;

          case '?':   //Display Menu
               printMenu();
               break;

          default:
               printf("Unknown action\n");
               break;
        }

       } while (choice != 'Q');

       return 0;
}

Alright, I have changed some things around, and I think I am doing it mostly correct now. However, I am still having one issue. When I add a customer, and then go to do one of the other options, it gives me the error message, Unknown Action. Why won't it let me do anything else after adding a customer? Below is my structure, as well as my insert function and my main function. If you need more of my code just let me know.

Thank you in advance for helping me!

#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
    {
       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");
       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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.