Categories
Uncategorized

Build a Singly Linked List

Implement a singly linked list.

One reply on “Build a Singly Linked List”

//------------------------------------------------------------------------------
// Implement a wrapper for testing a singly linked list
//------------------------------------------------------------------------------
#include 
#include 

//---------------------------------------------------------
// Define the list data structures
//---------------------------------------------------------
typedef struct node_struct{
  struct node_struct *next;
  int value;
} node;

//------------------------------------------------------------------------------
// Build a test list
//------------------------------------------------------------------------------

node *NewList(int length, int valArray[])
{
  int i;
  node *headNode;
  node *newNode;
  node *prevNode =  ((node *)NULL);

  for(i=0; inext = newNode;
      prevNode = newNode;
    }

  }

  return((node *) headNode);
}


//------------------------------------------------------------------------------
//                                Main
//------------------------------------------------------------------------------

int main()
{
// Build an array  from which to build a linked list
    int testarray[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};

//Build the linked list
  node *listHead = NewList(18, testarray);
 
  SomeFunctionThatUsesTheLinkedList(  listHead );
 
  return(0);
}

Leave a Reply