public class List
{
int value; // Payload in the List element
List next; // Reference variable to a List object !!!
// Other non-relevant stuff omitted
}
|
struct List
{
int value; // Payload in the List element
struct List *next; // Reference variable to a List object !!!
}
|
Moral of this example:
|
I will illustrate how to program linked list in C using what you know in Java !!
List p; // Define a reference variable to List objects
p = new List( ); // new List( ) creates a List object and return its location
// p = new List( ) makes p point to this List object
p.value = 4444; // Initialize value (access the value field)
p.next = null; // Initialize next
|
Comment:
|
struct List *p; // Define a reference variable to List objects
p = malloc(sizeof(struct List));
// malloc(sizeof(struct List)) creates a List object and return its location
// p = malloc(...) makes p point to this List object
p->value = 4444; // Initialize value (access the value field)
p->next = null; // Initialize next
|
Comment: the C library function malloc( ) performs the same task as Java's new operator
|
We will use these C constructs in a complete program later
But first, I want to stress an important difference in memory management between Java (that babies you to death) and C....