1

I'm learning C and I'm trying to understand a better concept of a Linked List. I have a code as an example (I changed it to be like the one i want to understand better). The code doesnt have any mistakes but again like i said it's to understand it better. Please have a look at the code first and below the code i'll ask my questions:

Here is the code for my struct and my first function:

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

struct client{
char first_name[20];
char last_name[20];
char id;
char reference;
char deposit;
struct client *next; // pointer to the next entry in the clients list
};
typedef struct client new_Client;

/***********************************************************
 * find_Client
 * Finds a place in the list to insert new user
 * Input: User's first and last name, id, reference, deposit
 * Returns: A pointer to teh struct which AFTER we'll insert
 the new struct. NULL if head of the linked-list
 ***********************************************************/

new_Client *find_Client(char *new_last_name, new_Client *head)
 {
    new_Client *prev = NULL; // previous has nothing
    new_Client *curr = head; // current is the head

    while (curr != NULL) //// while the current is not NULL because if it's null it's not head
     {  
      if (strcmp(new_last_name, curr->last_name)<0)
        break;

         prev = curr;
         curr = curr->next;  // pointing to the next node
     }
   return prev;
 }

And here is my next function where i usually have a problem understanding

/***********************************************************
 * add_new_Client
 * Adds a new client
 * Input: clients details and the address of the list's head
 * Returns: nothing
 ***********************************************************/

void add_new_Client(char *first_n, char *last_n, char *ident, char *ref, char *dep, new_Client **head)
{
    new_Client *after;

    // create new entry
    new_Client *new_entry = (new_Client*)malloc(sizeof(new_Client));    //allocating size of the struct

    if (NULL == new_entry) 
    {
    exit(1); //Q1: does it mean that it terminates?

    }
    else 
    {
    strcpy(new_entry->first_name, first_n);
    strcpy(new_entry->last_name, last_n);
    strcpy(&new_entry->id, ident);
    strcpy(&new_entry->reference, ref);
    strcpy(&new_entry->deposit, dep);
    }
    after = find_Client(new_entry->last_name, *head); // Q2: don't understand this line..
      if (NULL == after) // new head
        {
            if (NULL == *head) // Q3: for adding the first item (when the head is NULL) --> why is this condition necessary
            {
            new_entry->next = NULL;
            }
           else
           {
               new_entry->next = *head;
           }
           *head = new_entry;
       }else
         { //add in the middle
           new_entry->next = after->next;
           after->next = new_entry;
       }

   }

Okay so i put my questions in the code itself. They're marked as Q1,Q2 and Q3. Please let me know if you would prefer i edit on my post and change the questioning methode for it to be easier for you to understand my questions.

user2985083
  • 63
  • 1
  • 8
  • 2
    I might suggest going off to write your own code first so that you're not trying to force understanding based off of someone else's logic. – Justin Jasmann Feb 27 '14 at 20:46

1 Answers1

4
//Q1: does it mean that it terminates?
exit(1);

Yes, the program will exit with 1, which is interpreted as failure.


// Q2: don't understand this line..
after = find_Client(new_entry->last_name, *head);

here, after is a pointer to a struct new_Client. The function find_Client returns a pointer to a client that was searched using new_entry->last_name, and head is passed in order to give the search a beginning, somewhere to start.


// Q3: for adding the first item (when the head is NULL) --> why is this condition necessary
if (NULL == *head)

Because when the there is only one element in the list it is alone, that is, it doesn't have a next element. If the list size is higher than one, however, the new node's next element will point to the head, and the this new node will be the new head. That is, the new node's next element will have the value of the last head element, just before it. This is why this distinction is necessary.

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43