/***************************8
 * stack_queue.c
 *
 * a stack (first-in-last-out) and 
 * a queue (first-in-first-out),
 * both implemented with a linkedlist type 
 *
 *   $ gcc stack_queue.c linked_list.c -o stack_queue
 *   $ ./stack_queue
 *   -- stack demo --
 *   Push 1 onto the stack.
 *   Push 2 onto the stack.
 *   Push 3 onto the stack.
 *   Popping stack until empty:  3 2 1.
 *   Done.
 *   
 *   -- queue demo --
 *   Push 1 onto the queue.
 *   Push 2 onto the queue.
 *   Push 3 onto the queue.
 *   Popping stack until empty:  1 2 3.
 *   Done.
 *
 * Jim Mahoney | cs.bennington.college | MIT License | March 2021
 *******************/

#include <stdio.h>
#include <stdlib.h>

#include "linked_list.h"

// --- stack ------

typedef linkedlist mystack;                     // define a new type: mystack
mystack new_stack(){                            // create a stack
  return new_linkedlist();
}
void push(mystack stack, int value){            // push a value onto a stack
  push_end_value(stack, value);
}
int pop(linkedlist stack){                      // pop a value from a stack
  return pop_end_value(stack);
}

// --- queue ------------

typedef linkedlist myqueue;                     // define a new type : myqueue
myqueue new_queue(){                            // create a queue
  return new_linkedlist();
}
void enqueue(myqueue queue, int value){         // push a value onto a queue
  push_end_value(queue, value);
}
int dequeue(myqueue queue){                     // pop a value from a queue
  return pop_begin_value(queue);
}

// --- main -----------

int main(){
  mystack stack;
  myqueue queue;
  int i, value;

  printf("-- stack demo --\n");  // ---------- test stack ----------
  stack = new_stack();
  for (i=1; i<=3; i++){
    push(stack, i);
    printf("Push %d onto the stack.\n", i);
  }
  printf("Popping stack until empty: ");
  while (! is_empty(stack)) printf(" %d", pop(stack));
  printf(".\nDone.\n\n");

  printf("-- queue demo --\n");  // -------- test queue ---------------
  queue = new_queue();
  for (i=1; i<=3; i++){
    enqueue(queue, i);
    printf("Push %d onto the queue.\n", i);
  }
  printf("Popping stack until empty: ");
  while (! is_empty(queue)) printf(" %d", dequeue(queue));
  printf(".\nDone.\n\n");

  return 0;
}