I decided to take a crack at it.
I am afraid this is in C, but here is a barebones example of what a list is. It only implements the simplest of list methods (add to head and print).
Code:
#include <stdio.h>
#include <stdlib.h>
struct IntList {
int data;
struct IntList * next;
};
/**
* Adds data to the beginning of the list. Modifies the structure of the list.
* @param head The list to modify.
* @param addMe The data to add to the list.
*/
void IntList_add2head (struct IntList ** head, int addMe) {
// Create a new node to be inserted into the list.
struct IntList * NewNode = malloc (sizeof (struct IntList));
NewNode->data = addMe;
// Make the new node precede the head of the list.
NewNode->next = *head;
// Make the head point at the new node.
*head = NewNode;
}
/**
* Prints the contents of the list.
* Output should look like...
* HEAD -> 5 -> 1 -> 7 -> NULL
* @param head The list to print.
*/
void IntList_print (struct IntList * head) {
struct IntList * curr_node;
printf ("HEAD ->");
// The structure of this for loop is characteristic of linked lists.
for (curr_node = head; curr_node != NULL; curr_node = curr_node->next) {
printf (" %d ->", curr_node->data);
}
printf (" NULL\n");
}
int main (void) {
// The end of a list is always NULL. So, an empty list is one where
// the head is NULL.
struct IntList * head = NULL;
printf ("Welcome to the linked list example.\n");
// Repeatedly prompt the user for input and add items to the list.
for (;;) {
int new_value;
printf ("\n");
IntList_print (head);
printf ("Input a number (x to exit): ");
if (scanf ("%d", &new_value) != 1)
break;
IntList_add2head (&head, new_value);
}
return 0;
}