pu71n@pu71n.github.io:/$ cat Linked List Basics

Why Linked Lists ?

Linked lists and arrays are similar since both store collections of data. The terminology is that arrays and linked lists store “elements” on behalf of “client “code”. The specific type of element is not important since essentially the same structure works to store elements of any type. Linked lists have their own strenghs and weaknesses, but they happen to be strong where arrays are weak. The array’s features all follow from its strategy of allocating the memory for all its elements in one block of memory. Linked lists use an entirely different strategy. As we will see, linked lists allocate memory for each element separately and only when necessary.

Pointer Refresher

Here is a quick review of the terminology and rules for pointers. The linked list code to follow will depend on these rules.

What Linked Lists Look LikeA “pointer” stores a reference to another variable sometimes know as its “pointee”. Alternately, a pointer may be set to the value NULL which encodes that it does not currently refer to a pointee.

What Linked Lists Look Like ?

An array allocates memory for all its elements lumped together as one block of memory. In contrast, a linked list allocates space for each element separately in its own block of memory called a “linked list element” or “node”. The list gets is overall structure by using pointers to connect all its nodes together like the links in a chain. Each node contains two fields: a data field to store whatever element type the list holds for its client, and a next field which is a pointer used to link one node to the next node. Each node is allocated in the heap with a call to malloc(), so the node memory continues to exist until it is explicitly deallocated with a call to free(). The front of the list is a pointer to the first node.

struct list_float          /*     ---------    ---------           */
{                          /*     | Value |    | Value |           */
  float             value; /*     |  ---  |    |  ---  |           */
  struct list_float *next; /*     |  next-|--> |  next-|--> NULL   */
}                          /*     ---------    ---------           */

The Empty List - NULL

The above is a list pointed to by head is described as being of “length two” since it’s made of two nodes with the .next field of the last node set to NULL. There needs to be some representation of the empty list - the list with zero nodes. The most common representation chosen for the empty list is a NULL head pointer.

Linked List Types: Nodes and Pointer

Before writing the code to build the above list, we need two data types:

  1. Node : The type for the nodes which will make up the body of the list. These are allocated in the heap. Each node contains a single client data element and a pointer to the next node in the list.
struct node {
	int data; 
	struct node* next; 
};
  1. Node Pointer : The type for pointers to nodes. This will be the type of the head pointer and the .next fields inside each node.