master
  1#include "stack.h"
  2#include <stdio.h>
  3#include <stdlib.h>
  4
  5static int MAX_SIZE = 2147483647;
  6
  7/**
  8 * Constructs a new instance of a queue
  9 *
 10 * @return A Queue struct
 11 */
 12static Queue *newQueue(void) {
 13  Queue *queue = malloc(sizeof(Queue));
 14  queue->size = 0;
 15  queue->head = NULL;
 16  return queue;
 17}
 18
 19/**
 20 * Constructs an instance of a Stack.
 21 *
 22 * @return A Stack struct.
 23 */
 24Stack *initialize(void) {
 25  Stack *stack = malloc(sizeof(Stack));
 26  stack->q1 = newQueue();
 27  return stack;
 28}
 29
 30/**
 31 * Constructs a new singly linked list Node to push onto the Stack.
 32 *
 33 * @param data The data to store on the stack.
 34 * @return A Node struct
 35 */
 36Node *newNode(int data) {
 37  Node *node = malloc(sizeof(Node));
 38  node->data = data;
 39  node->next = NULL;
 40  return node;
 41}
 42
 43/**
 44 * Enqueues a new item onto a Queue
 45 *
 46 * @param self The queue to add the data to
 47 * @param data The data to add to the queue
 48 */
 49void enqueue(Queue *self, int data) {
 50  Node *node = newNode(data);
 51  self->size++;
 52
 53  if (self->head == NULL) {
 54    self->head = node;
 55  } else {
 56    Node *tail = self->head;
 57
 58    while (tail->next != NULL)
 59      tail = tail->next;
 60    tail->next = node;
 61  }
 62}
 63
 64/**
 65 * Dequeues the next item from the queue
 66 *
 67 * @param self The queue to dequeue from
 68 * @return The data for the next it to remove from the Queue
 69 */
 70int dequeue(Queue *self) {
 71  if (self->head == NULL)
 72    return -1;
 73
 74  Node *node = self->head;
 75  int data = node->data;
 76
 77  self->head = node->next;
 78  free(node);
 79  return data;
 80}
 81
 82/**
 83 * Pushes an item on to the top of a stack.
 84 *
 85 * @param self The stack to push the data on to
 86 * @param data The data to push on to the stack
 87 */
 88void push(Stack *self, int data) {
 89  if (self->q1->size < MAX_SIZE)
 90    enqueue(self->q1, data);
 91}
 92
 93/**
 94 * Pops off the last item pushed onto the stack.
 95 *
 96 * @param self The stack to pop an item off of
 97 * @return The data associated with the item to pop off the stack
 98 */
 99int pop(Stack *self) {
100  int count = self->q1->size;
101
102  if (count <= 0)
103    return 0;
104
105  Queue *tmp = newQueue();
106
107  for (int i = 0; i < count - 1; i++)
108    enqueue(tmp, dequeue(self->q1));
109
110  int data = dequeue(self->q1);
111  free(self->q1);
112  self->q1 = tmp;
113  return data;
114}
115
116/**
117 * Returns the number of items on the stack
118 *
119 * @param self The stack to investigate
120 * @return The count of items on the stack.
121 */
122int size(Stack *self) { return self->q1->size; }
123
124/**
125 * The equivalent of a destructor to free any memory associated with a Stack.
126 *
127 * @param self The stack to clean up
128 */
129void destroy(Stack *self) {
130  int count = size(self);
131
132  for (int i = 0; i < count; i++)
133    pop(self);
134
135  free(self->q1);
136  free(self);
137}
138
139/**
140 * A helper method used to print the items in a queue
141 *
142 * @param self the queue to print
143 */
144void inspect(Queue *self) {
145  Node *tmp = self->head;
146
147  if (self->size == 0) {
148    printf("[]\n");
149    return;
150  }
151
152  printf("[");
153  for (int i = 0; i < self->size; i++) {
154    printf("%d,", tmp->data);
155    tmp = tmp->next;
156  }
157  printf("\b]\n");
158}