Reminder: concepts and their implementation
 

  • You have already learned about the list concept

  • You have seen the list implementation in Java

  • I will not re-teach the list concept

  • I will only show you the implementation of list in C !

      • In the process, you will learn to how to translate a program written in Java to C...

Defining a list object in Java
 

In Java, list objects are defined using a class similar to this one:

 public class List
 {
    int          value; // Data stored in a List element 
    List         next;  // A reference variable ! 
                        //  points to the next List elem     
                        //  in the linked list


   // Other non-relevant stuff omitted
 } 

We must define a struct List in C to store list "objects"

Defining a list object in C
 

In C, list objects are defined using a struct similar to this one:

 struct List
 {
    int          value; // Data stored in a List element 
    struct List *next;  // A reference variable 
                        //  points to the next List elem     
                        //  in the linked list


   // There are no functions defined inside a struct
 } ; 

 

Defining an empty list in Java
 

An empty list is defined as follows in Java:

 class List {
   int          value;
   List         next;  
 }

 public class demo {

   public static void main( String[] args )
   { 
     List         head = null;
                  // head is a reference variable



   }
 } 

We must define a reference variable in C to represent head !

Defining an empty list in C
 

An empty list is defined as follows in C:

 struct List {
   int          value;
   struct List *next;  
 };

 #include <stdio.h> // Contains the definition for NULL

 int main( int argc, char *argv[] )
 { 
     struct List *head = NULL;  // C uses NULL for null 
                  // head is a reference variable



 }
  

In C, a reference variable is defined as   struct List *