queue.c |
| #include <stdio.h> #include <stdlib.h> #include "mylib.h" #include "queue.h" #include "graph.h" /* The queue structure */ struct queue_c { int *the_q; int front; int end; int size; }; //static int size = 0; /* creates a new queue */ queue queue_new(int size){ queue q = emalloc(sizeof *q); q->the_q = emalloc(size * sizeof q->the_q[0]); q->size=size; q->front=0; q->end=0; return q; } /* returns the size of the queue this should be the number of items actually in the queue */ int queue_size(queue q){ return q->end - q->front; } /* inserts an item onto the queue if there is room returns 1 for "all good" if the queue is full returns 0 for "all is not so good" */ void queue_enqueue(queue q, int i){ if(queue_size(q)<q->size){ q->the_q[q->end%q->size]=i; q->end++; // return 1; } else { printf("Sorry queue full\n"); // return 0; } } /* returns the item at the front of the queue if there is an item to return it returns it, if there is not then it returns -1 ******************************************** returning -1 is ok for the implementation of the graph but if we want to put something onto the stack that could possibly return -1, then we will have to exit the program if there is nothing on the queue to return, else make sure that error checking is preformed before this is called. */ int queue_dequeue(queue q){ if(queue_size(q)>0){ int value = q->the_q[q->front%q->size]; q->front++; return value; } else { printf("Sorry queue empty\n"); return -1; /* I don't really feel -1 is satisfacory value to return */ } } |
James Little |