title: How do you create an empty linked list in C?
body: <p>I have a C code, in which I am assigned to create an empty list. Although I create one, with Element = 0, it is still considered a list by the program, so it prints 0, when in reality I want it to be printed as NULL. Also, when I print the length of my list, it also comes out as 1, rather the 0 I'd be looking for. </p>
  
  <p>This is my code so far.</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;time.h&gt;
  
      typedef int ElementType;
  
      struct Node;
      typedef struct Node *PtrToNode;
      typedef PtrToNode List;
      typedef PtrToNode Position;
  
      List CreateList();
      int IsEmpty(List L);
      int IsLast(List L);
      Position GetNode(List L,int idx);
      void FetalError(const char *msg);
     void PrintList(List L);
  
      struct Node {
          ElementType Element;
          Position Next;
          Position Previous;
      };
  
      List CreateList()
      {
          List L;
          L= (List)malloc(sizeof(struct Node));
          if(L==NULL) FetalError("Out of Memory");
          L-&gt;Element = 0;
          L-&gt;Next = NULL;
          L-&gt;Previous = NULL;
          return L;
      }
  
  Position Insert(ElementType X, List L, Position P)
  {
      Position Tmp;
      Tmp=(Position)malloc(sizeof(struct Node));
      if(Tmp==NULL) FetalError("Out of Memory");
      Tmp-&gt;Element=X;
      Tmp-&gt;Next=P-&gt;Next;
      P-&gt;Next=Tmp;
      return Tmp;
  }
  
      int IsEmpty(List L){
          return(L-&gt;Next==NULL);
      }
  
      int IsLast(List P){
          return(P-&gt;Next==NULL);
      }
  
         void FetalError(const char *msg)
      {
          printf(msg);
          exit(-1);
      }
  
      PrintList(List L){
  
          while(L!= NULL){    
              printf("%d &lt;-&gt; ", L-&gt;Element);
              L=L-&gt;Next;
          }
          printf("\n");
      }
  
          int getCount(List L) 
  { 
      int count = 0;  // Initialize count 
      struct Node* current = L;  // Initialize current 
      while (current != NULL) 
      { 
          count++; 
          current = current-&gt;Next; 
      } 
          printf("%d number of elements", count); 
  } 
      Position GetNode(List L, int idx){
  
          int i = 0; 
          while (L != NULL) 
          { 
              if (i == idx) 
                  return(L); 
              i=i+1; 
              L= L-&gt;Next; 
          } 
          return;  
       } 
  
      Position Last(List L)
      { 
      return L;
      } 
  
      int main(int argc, char *argv[]) 
      {
          int i;
          ElementType X;
          List L;
          Position P, P1 , P2;
  
          srand(0);
  
          // creat an empty list
          printf("Creating an empty list\n");
          L = CreateList();
          PrintList(L);
          getCount(L);
  
          // Insert at the beginning
          printf("**(Inserting 5 random numbers at the beginning\n");
          for( i=0 ; i&lt;5 ; i++ ) {
              Insert(rand(),L,L);
              PrintList(L);
          }
          P = GetNode(L,5);
          printf("The Element of 5th Node is %d\n",P-&gt;Element);
          getCount(L);
  
          return 0;
      }
  </code></pre>