title: Simple stack program in C
body: <p>I am in an OS class and I have to write a simple stack program (the main function just determines what the user is asking you to do). If this were not required to be in C, I would have had this done ages ago, but because I am not very good at C coding, it has "a bug"... The bug so far is that it just continues to "pop" the same value off. (It's not actually popping anything off). I think it's because I don't understand how structures and pointers really work. Or is it a not so obvious coding mistake?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  struct node {
      int data;
      struct node *next;
      struct node *prev;
  } first;
  
  void push(int);
  void pop();
  
  int main(void)
  {
      int command = 0;
  
      while (command != 3)
      {
          printf("Enter your choice:\n1) Push integer\n2) Pop Integer\n3) Quit.\n");
          scanf("%d",&amp;command);
          if (command == 1)
          {
              // push
              int num;
              scanf("%d",&amp;num);
              push(num);
          }
          else
          {
              if (command == 2)
              {
                  pop();
              }
              else
              {
                  if (command != 3)
                  {
                      printf("Command not understood.\n");
                  }
              }
          }
      }
  
      return 0;
  }
  
  void push (int x)
  {
      struct node newNode;
      newNode.data = x;
      newNode.prev = NULL;
      newNode.next = &amp;first;
      first = newNode;
      printf("%d was pushed onto the stack.\n", first.data);
  }
  
  void pop()
  {
      if (first.data == '\0')
      {
          printf("Error: Stack Empty.\n");
          return; 
      }
      printf("%d was popped off the stack.\n", first.data);
      first = *(first.next);
      first.prev = NULL;
  }
  </code></pre>