title: Linked list remove and return first element
body: <p>I'm trying to parse commands in a shell script using C. I currently have the linked list set up but I'm having trouble removing and returning the first element properly. When I try to get the first element, it works fine once but then after that it gets stuck and goes into an infinite loop and I don't understand why. Thanks in advance for any help. Here's my code: </p>
  
  <pre><code>     enum command_type
         {
           AND_COMMAND,         // A &amp;&amp; B
           SEQUENCE_COMMAND,    // A ; B
           OR_COMMAND,          // A || B
           PIPE_COMMAND,        // A | B
           SIMPLE_COMMAND,      // a simple command
           SUBSHELL_COMMAND,    // ( A )
         };
  
       // Data associated with a command.
       struct command
       {
         enum command_type type;
  
         // Exit status, or -1 if not known (e.g., because it has not exited yet).
         int status;
  
         // I/O redirections, or null if none.
         char *input;
         char *output;
  
         union
         {
           // for AND_COMMAND, SEQUENCE_COMMAND, OR_COMMAND, PIPE_COMMAND:
                    struct command *command[2];
  
           // for SIMPLE_COMMAND:
           char **word;
  
           // for SUBSHELL_COMMAND:
           struct command *subshell_command;
         } u;
       };
  
       typedef struct command_stream *command_stream_t;
       typedef struct command *command_t;
  
       struct command_stream
       {
          command_t command_stream;
          command_stream_t next;
       }
  
       command_t
       read_command_stream(command_stream_t s)
       {
          if(s)
          {
             command_t comm = s-&gt;command;
             s=s-&gt;next;
             return comm;
          }
          return NULL;
       }
  
       void printList(command_stream_t head)
       { 
          command_t command;
          while((command = read_command_stream(head)))
          { 
             //print_command is a function that takes in a command_t and then prints the    
             //command in a nice format.
             print_command(head-&gt;command);
          }
       }
  </code></pre>