title: linked list to get input from the user and print it
body: <p>I am trying a write a program in c to get an input (chars) from the user. The user should be able to type the input as long as he wants (''infinite''). </p>
  
  <p>This is the program I finally wrote with getting no errors at all:</p>
  
  <p>Code: </p>
  
  <pre><code>/*
  main:
  
  we will try to get an input from the user.
  if we succeed, call insert function. 
  if we don't succeed, call freeList function.
  if we encounter '\n' or EOF, we quit the program.
  print the list, the free it.
  
  insert (type void. does not return a value):
  
  if allocation is successful, then we can put a new item in the linked list. 
  the item is a type node.
  we don't care the number of nodes in the list. 
  if allocation is not successful, call free function.
  
  free:
  free the memory.
  
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  typedef struct list *ptr;
  typedef struct list{
      char data;
      ptr next;
  }node;  /*describes a linked list.*/
  void insert(ptr *H, char c);
  void freeList(ptr *H);
  void printList(ptr *H);
  
  int main(){
  char c;  
  printf("enter a string\n");
  ptr H=(ptr)malloc(sizeof(node));/*create an empty list. this is its head.*/
  while ((c=getchar())!=EOF &amp;&amp; c!='\n'){  
      insert(&amp;H,c);
      }
  printList(&amp;H); /*print the list*/
  freeList(&amp;H); /*free the list*/
  printf("\n");
  return 0;
  }
  void insert(ptr *H, char c){
      ptr p1;
      p1=*H;
      ptr T=(ptr)malloc(sizeof(node)); /*try to allocate a new memory cell*/
      if (!T)
      {
          printList(H);       
          freeList(H); /*we should not free H because we will 
              lose the list we've created.*/
      }
      else
      {
          T-&gt;data=c;
          while(p1-&gt;next)
          {
              p1=p1-&gt;next;
          }
          p1-&gt;next=T; /*add T to the end of the linked list*/
  
      }
  
  
  }
  void freeList(ptr *H){
      ptr p1; /*helper pointer*/
      while(*H){      /*while H points to a certain node*/
      p1=*H;
      (*H)=p1-&gt;next;
      free(p1);
      }   
  }
  void printList(ptr *H){ /*a copy of H is sent so no problem working with it.*/
      ptr p1=*H; printf("string is: \n");
      while (p1) /*while H is not null        */
      {
          printf("%c", p1-&gt;data);
          p1=p1-&gt;next;
      }
  
  }
  </code></pre>
  
  <p>This code actually works, but any feedback is good.</p>