title: Stack push pop rotate
body: <p>This program has the functions push, pop and rotate. The rotate function is supposed the top three items on the stack   </p>
  
  <p>If the numbers in the stack are:</p>
  
  <pre><code>2
  6
  5
  8
  9
  </code></pre>
  
  <hr>
  
  <p>After you call the <code>rotate</code> function they should be:</p>
  
  <pre><code>5
  2
  6
  8
  9
  </code></pre>
  
  <hr>
  
  <p>The push and pop functions are correct, i'm just not sure how to implement this rotate function correctly</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "stack.h"
  
  struct node* push(struct node* top, int i) {
     struct node *new_node;
     new_node = malloc(sizeof(struct node));
     if(new_node == NULL) {
      printf("malloc failed");
          return top;
     }
  
     new_node-&gt;value = i;
     new_node-&gt;next = top;
     return new_node;
  
  }
  
  struct node *make_empty(struct node *top) {
     return NULL;
  }
  
  struct node *pop(struct node *top, int *i) {
     struct node *new_node;
     new_node = malloc(sizeof(struct node));
     if (new_node == NULL) {
      printf("malloc failed");
      return top;
     } 
  
     *i = top-&gt;value;
     new_node = top;
     top = top-&gt;next;
     free(new_node);
     return top;
  }
  
  struct node *rotate(struct node *top) {
  
     struct node *new_node;
     struct node *prev = top;
     struct node *cur = top;
     int i;
  
     new_node = malloc(sizeof(struct node));
     if (new_node == NULL) {
      printf("malloc failed");
      return top;
     }
  
    i = cur-&gt;value;
     top = top-&gt;next;
  
     new_node-&gt;next = top-&gt;next;
     new_node-&gt;value = cur-&gt;value;
  
     return new_node;
  } 
  void print_stack(struct node *top) {
     struct node *p;
     if(top != NULL) {
        for(p = top; p !=NULL; p=p-&gt;next)
      printf("%d\n", p-&gt;value);
        printf("\n");
     }
     else
        printf("stack is empty\n");
  }
  </code></pre>