|
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"
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 } ; |
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 !
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 *