|
In Java, Node objects (= nodes) are defined using a class:
public class Node
{
int value; // Data stored in a List element (= a Node)
Node next; // A reference variable !
// points to the next List elem (= Node)
// in the linked list
// Other non-relevant stuff omitted
}
|
We now define a struct Node in C to store list "objects" (= nodes)
In C, Node "objects" (= nodes) are defined using a struct:
struct Node
{
int value; // Data stored in a List element (= a Node)
struct Node *next; // A reference variable !!!!!!!
// points to the next List elem (= Node)
// in the linked list
// There are no functions defined inside a struct !!!
} ;
|
Notice we define a reference variable in both cases !
The only difference is the way to express it !
An empty list is defined as follows in Java:
public class Node
{
int value; // Data stored in a List element (= a Node)
Node next; // A reference variable !
}
public class List
{
private Node head = null; // empty list
// head is a reference variable
public void insert( .... ) { .... } // Only shown for cintext
}
|
We must define a reference variable in C to represent head !
An empty list is defined as follows in C:
struct Node
{
int value;
struct Node *next;
};
#include <stdio.h> // Contains the definition for NULL
int main( int argc, char *argv[] )
{
struct Node *head = NULL; // C uses NULL for null
// head is a reference variable
}
struct Node *insert( .... ) { ... } // Shown for context
|
In C, a reference variable head is defined as struct Node *
Furthermore, it's defined inside the function that manipulates the linked list
An object is allocated using the new operator in Java:
class Node
{
int value;
Node next;
}
public class demo {
public static void main( String[] args )
{
Node p; // p is a reference variable
p = new Node( ); // The new operator allocates
// memory for a list object
// and returns its base address
// #bytes of memory needed depends
// of fields in Node class
}
}
|
You need to understand what happens inside the computer system when it executes the following:
Node p; // p is a reference variable
p = new Node( );
|
"Node p;" will allocate (= reserve) memory for the reference variable p:
You need to understand what happens inside the computer system when it executes the following:
Node p; // p is a reference variable
p = new Node( );
|
"new Node( )" will dynamically allocate (=reserve) memory for a Node object and return its base address:
You need to understand what happens inside the computer system when it executes the following:
Node p; // p is a reference variable
p = new Node( );
|
"p = " will assign the return value to the variable p:
Summary of the effect of:
Node p; // p is a reference variable
p = new Node( );
|
The reference variable p will point to a newly created Node object:
Note: we will be repeating these same steps using the C programming language !!
Allocating memory in the C programming lanugae:
|
|
Example program that prints the storage size of some variable types (including a struct Node):
#include <stdio.h>
struct Node
{
int value;
struct Node *next;
};
int main( int argc, char *argv[] )
{
struct Node a, *p;
printf("sizeof(int) = %ld\n", sizeof(int));
printf("sizeof(double) = %ld\n", sizeof(double));
printf("sizeof(struct Node) = %ld\n", sizeof(struct Node));
printf("sizeof(struct Node a) = %ld\n", sizeof(a));
printf("sizeof(struct Node *p) = %ld\n", sizeof(p));
}
|
DEMO: demo/C/set2/sizeof3.c
These were the statements in Java to allocate a Node object:
class Node
{
int value;
Node next;
}
public class demo {
public static void main( String[] args )
{
Node p; // p is a reference variable
p = new Node( );
// The new operator allocates
// memory for a list object
// and returns its base address
// #bytes of memory needed depends
// of fields in Node class
}
}
|
We will now express the same actions using C statements !
The statements in C to allocate a Node object are as follows:
#include <stdlib.h> // Header file containing the declaration for malloc( ) struct Node { int value; struct Node *next; } ; int main( int argc, char *argv[] ) { struct Node *p; // p is a reference variable p = malloc( sizeof(struct Node) ); // The malloc function allocates // memory for a struct Node variable // and returns its base address // #bytes of memory needed depends // of fields in Node structure } |
I will illustrate what happens inside the computer when these statements are executed.
I will illustrate what happens inside the computer system in this program:
struct Node *p; // p is a reference variable
p = malloc(sizeof(struct Node));
|
"struct Node *p;" will allocate (reserve memory) a reference variable p:
I will illustrate what happens inside the computer system in this program:
struct Node *p; // p is a reference variable
p = malloc(sizeof(struct Node));
|
malloc( ) allocates (=reserve) memory for a struct Node variable (= object) and return its base address:
I will illustrate what happens inside the computer system in this program:
struct Node *p; // p is a reference variable
p = malloc(sizeof(struct Node));
|
"p = " will assign the return value to the variable p:
Summary of the effect of:
struct Node *p; // p is a reference variable
p = malloc(sizeof(struct Node));
|
The reference variable p will point to a newly created struct Node var (= "object"):
Note: we have repeated the same steps used by Java !! (I.e.: it's the same concept (allocate memory) !)