/** * array-queue.c * Array based queues. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For malloc #include "linear.h" // Signatures of the methods we provide. // +-------+---------------------------------------------------------- // | Types | // +-------+ struct linear { char **contents; // The contents of the queue int front; // The front of the queue int back; // The back of the queue int size; // The size of the queue int capacity; // The capacity of the queue }; // +---------+-------------------------------------------------------- // | Helpers | // +---------+ // +----------------------------+------------------------------------- // | Constructors & Destructors | // +----------------------------+ struct linear * construct (int minimum_capacity) { // Allocate the enclosing structure struct linear *linear = (struct linear *) malloc (sizeof (struct linear)); if (linear == NULL) return NULL; // Allocate the array of strings linear->contents = calloc (minimum_capacity, sizeof (char *)); if (linear->contents == NULL) { free (linear); return NULL; } // Fill in the fields. (Note that calloc zeroes memory, so we don't // have to set things to 0.) linear->capacity = minimum_capacity; linear->size = 0; linear->front = 0; linear->back = 0; // And we're done return linear; } // construct void destruct (struct linear *linear) { // STUB } // destruct // +-----------+------------------------------------------------------ // | Observers | // +-----------+ int is_empty (struct linear *linear) { return 0; // STUB } // is_empty int is_full (struct linear *linear) { return 0; // STUB } // is_full char * peek (struct linear *linear) { if (linear->size == 0) return NULL; return linear->contents[linear->front]; } // peek // +----------+------------------------------------------------------- // | Mutators | // +----------+ int add (struct linear *linear, char *str) { linear->contents[linear->back++] = str; return 1; } // add char * get (struct linear *linear) { return linear->contents[linear->front++]; } // get void clear (struct linear *linear) { linear->size = 0; } // clear