Review:   inserting a Node at the head (= start) of a linked list

  • Insert the list element (= Node) at e at the head (= start) of the linked list at h and return the new list

    Input:

  • Output:

Review:   insert at head algorithm in Java

  • In CS171, you learned the following insert at head algorithm in Java:

    class List // Class  definition included to use 1 program file
    {
       int value;
       List next;
    }
    
    public class insert
    {
       /* =========================================
          insert(h, e): insert e at start of list h
                        return new list
          ========================================= */
       public static List insert (List h, List e)
       {
          e.next = h;
          return e;      // Return new list that starts at e
       }
    
       ... // Other code omitted for brevity
    }

To write a C function, you need to express the algorithm (a concept !) with C syntax

Expressing the insert at head algorithm in C

  • The class definition becomes a struct definition in C:

    struct List // Struct definition included to use 1 program file
    {
       int value;
       struct List *next;  // Reference variable
    }
    
    public class insert
    {
       /* =========================================
          insert(h, e): insert e at start of list h
                        return new list
          ========================================= */
       public static List insert (List h, List e)
       {
          e.next = h;
          return e;      // Return new list that starts at e
       }
    
       ... // Other code omitted for brevity
    }

Next:   public class ....

Expressing the insert at head algorithm in C

  • There is no class construct in C:

    struct List // Struct definition included to use 1 program file
    {
       int value;
       struct List *next;  // Reference variable
    }
    
    
    
    /* =========================================
       insert(h, e): insert e at start of list h
                     return new list
       ========================================= */
    public static List insert (List h, List e)
    {
        e.next = h;
        return e;      // Return new list that starts at e
    }
    
    ... // Other code omitted for brevity
    
    

Next:   the parameters

Expressing the insert at head algorithm in C

  • The data type of the parameters is: struct List *

    struct List // Struct definition included to use 1 program file
    {
       int value;
       struct List *next;  // Reference variable
    }
    
    
    
    /* =========================================
       insert(h, e): insert e at start of list h
                     return new list
       ========================================= */
    public static List insert (struct List *h, struct *List e)
    {
        e.next = h;
        return e;      // Return new list that starts at e
    }
    
    ... // Other code omitted for brevity
    
    

Next:   the return value

Expressing the insert at head algorithm in C

  • The data type of the return value is also: struct List *

    struct List // Struct definition included to use 1 program file
    {
       int value;
       struct List *next;  // Reference variable
    }
    
    
    
    /* =========================================
       insert(h, e): insert e at start of list h
                     return new list
       ========================================= */
    struct List *insert (struct List *h, struct *List e)
    {
        e.next = h;
        return e;      // Return new list that starts at e
    }
    
    ... // Other code omitted for brevity
    
    

Next:   the expression e.next

Expressing the insert at head algorithm in C

  • The Java expression e.next expressed in C is (*e).next or e->next:

    struct List // Struct definition included to use 1 program file
    {
       int value;
       struct List *next;  // Reference variable
    }
    
    
    
    /* =========================================
       insert(h, e): insert e at start of list h
                     return new list
       ========================================= */
    struct List *insert (struct List *h, struct *List e)
    {
        e->next = h;
        return e;      // Return new list that starts at e
    }
    
    ... // Other code omitted for brevity
    
    

Done !

The insert at head program in Java

class List  // Class included to make 1 program file
{
   int value;
   List next;
}

public class insert
{
   /* =========================================
      insert(h, e): insert e at start of list h
                    return new list

      A List variable is a reference variable !!!
      ========================================= */
   public static List insert (List h, List e)
   {
      e.next = h;
      return e;         // Return new list that starts at e
   }

   public static void main(String[] args)
   {
      List head = null;    // Empty list
      List p;

      /* -------------------------------------------------
         Create a list element and insert in List at head
         ------------------------------------------------- */
      p = new List( );
      p.value = 1;
      head = insert(head, p);       // Insert p in head
      print(head);
  
      /* -------------------------------------------------
         Create a list element and insert in List at head
         ------------------------------------------------- */
      p = new List( );
      p.value = 2;
      head = insert(head, p);       // Insert p in head again           
      print(head);
   }

   // Print the list
   public static void print(List h)
   {
      while (h != null)
      {
         System.out.printf("%d ", h.value);
         h = h.next;
      }
      System.out.printf("\n\n");
   }
} 

DEMO: demo/C/Linked-list/insert.java

The insert at head program in C

I will "convert" the program in class as a demo

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

struct List       // This is the List class definition in C
{
   int          value;
   struct List *next;
};

/* =========================================
   insert(h, e): insert e at start of list h
                 return new list

   The data type List is struct List * in C
   ========================================= */
struct List *insert (struct List *h, struct List *e)
{
   e->next = h;
   return e;            // Return new list that starts at e
}

void print(struct List *h);   // We must declare print to avoid compile error

void main(int argc, char *argv[])
{
   struct List *head = NULL;   // Nul pointer NULL instead of null
   struct List *p;

   /* -------------------------------------------------
      Create a list element and insert in List at head
      ------------------------------------------------- */
   p = malloc( sizeof(struct List) );
   p->value = 1;
   head = insert(head, p);       // Insert p in head
   print(head);

   /* -------------------------------------------------
      Create a list element and insert in List at head
      ------------------------------------------------- */
   p = malloc( sizeof(struct List) );  // This is new List( )
   p->value = 2;
   head = insert(head, p);       // Insert p in head
   print(head);
}

// Print the list
void print(struct List *h)
{
   while (h != NULL)
   {
      printf("%d ", h->value);
      h = h->next;
   }
   printf("\n\n");
}