Apprentiship blog

Data structures, Double linked lists

December 13, 2018

Today’s exercise from learn-C-the-hard-way was to copy from the book, and test a program that created and modified double linked lists.

What’s a double linked list?

In short it’s a collection of nodes, with each node having a pointers to it’s next and previous node. The List points to the first, and list node, which allows us to traverse the entire list… starting from either the first node or the last node.

// forward decoration, needed for defining Node as a data type
struct Node;

typedef struct Node {
	struct Node *next;
	struct Node *prev;
	void *value;
} Node;

// The list,
// knows many nodes it has
// and where to find the start / end of the chain
typedef struct List {
	int count;
	Node *first;
	Node *last;
} List;

Most of the rest of this exercise was about preforming push, pop, shift, unshift and iterations over the double linked list, and testing that the operations worked. You can checkout the “porject” here

As I was copying the code from the book, there wasn’t much of a need for test first programming… I think the focus was on the general topic. But I read a ahead and the next exercise Linked List Algorithms Provides the tests for a bubble and merge sort, then encourages me to find the solutions for myself. Then states this practice of providing only the tests will steadily become more common through out the book, which is a good thing as copying the answers straight out of the book was getting a bit tiresome.


Written by Marc McIntosh Find him on github