Doubly Linked List

Overview

I have the good fortune of being assigned to program a doubly linked list in C. This type of data stucture must support the following features:

  • Add a value to the list.
  • Return the nth element in the list.
  • Return the first element in the list.
  • Return the last element in the list.
  • Remove an arbitrary element in the list.
  • Search for an arbitrary element by either first or last name.
  • Reverse the list.
  • Sort the list by either first or last name, in either ascending or descending order.
  • Print the list.
  • Return the length of the list.

Add a Value to the List

Cases

It is helpful to consider three different case for the list:

  • Empty
  • One Element
  • Many Elements

Create a Node

struct node tmp = (struct node) malloc(sizeof(struct node)))

Code

The implementation of a doubly linked list with the features above is now available. The only feature lacking is the sort. It has been stubbed but not completed. Since I need a Menu system to interface with the link list model it seemed logically to have a MVC type of approach where:

  • the model handles the crud on the list
  • the interface uses the menu choices to execute the appropriate action of the model
  • the controller (driver) runs the menu
My tags:
 
Popular tags:
 
Powered by MojoMojo