public class ListElem
{
public int value;
public ListElem next;
public ListElem()
{
value = 0;
}
public ListElem(int x)
{
value = x;
}
}
|
Java:
public class ListElem
{
public int value;
public ListElem next;
...
}
|
|
public class ListElem
{
public:
int value;
ListElem * next; // Pointer !!!!
ListElem()
{
value = 0;
}
ListElem(int x)
{
value = x;
}
}
|
Again, the most important thing to remember is:
|
class List
{
public:
ListElem * head; // Pointer variable !!!
List(); // Constructor
void delete(); // Member functions
void insert(ListElem * elem);
} ; // Don't forget the ; !!!
|
#include "List.h" // Always include declarations first !
// *************************************************
// Constructor
// ************************************************
List::List()
{
head = NULL; // Start with empty list
}
// *************************************************
// delete(): delete at head
// *************************************************
void List::delete()
{
ListElem * p2;
if ( head != null )
{
p2 = head;
head = p2->next; // The second is the new "first element"
}
}
// -------------------------------
// insert(v): insert at head
// -------------------------------
void List::insert(ListElem * elem)
{
elem->next = head; // Link new element
head = elem;
}
|
How to run:
CC -c List.C
CC -c main.C
CC main.o List.o
or:
CC main.C List.C
Run:
a.out
|