Created
August 26, 2015 17:32
-
-
Save brunoalano/ac88acf151f515e0d49d to your computer and use it in GitHub Desktop.
Circular Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Circular Queue Implementation | |
* | |
* This queue will handle any type of objects only changing | |
* the typedef above. | |
* | |
* @author Bruno Alano Medina | |
* @version 1.0.0 | |
*/ | |
/* Standard Libraries */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
/* Queue Library */ | |
#include "queue.h" | |
Queue *createQueue(size_t size) | |
{ | |
Queue *q = malloc(sizeof(Queue)); | |
q->vector = malloc(sizeof(Element) * size); | |
q->size = size; | |
q->head = 0; | |
q->tail = 0; | |
return q; | |
} | |
void deallocateQueue(Queue *q) | |
{ | |
free(q->vector); | |
free(q); | |
} | |
bool full(Queue *q) | |
{ | |
return (q->head == ((q->tail + 1) % q->size)); | |
} | |
bool empty(Queue *q) | |
{ | |
return (q->tail == q->head); | |
} | |
void enqueue(Queue *q, Element n) | |
{ | |
if (full(q)) | |
{ | |
printf("Fila cheia!"); | |
exit(0); | |
} | |
q->vector[q->tail] = n; | |
q->tail = (q->tail + 1) % q->size; | |
} | |
Element dequeue(Queue *q) | |
{ | |
if (empty(q)) | |
{ | |
printf("Fila vazia"); | |
exit(0); | |
} | |
Element temp = q->vector[q->head]; | |
q->head = (q->head + 1) % q->size; | |
return temp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Circular Queue Implementation | |
* | |
* This queue will handle any type of objects only changing | |
* the typedef above. | |
* | |
* @author Bruno Alano Medina | |
* @version 1.0.0 | |
*/ | |
/* Standard Libraries */ | |
#include <stdbool.h> | |
#ifndef _QUEUE_H_INCLUDED | |
#define _QUEUE_H_INCLUDED | |
/** | |
* The type of element that we are storing in the | |
* queue | |
*/ | |
typedef int Element; | |
/** | |
* Queue Structure | |
*/ | |
typedef struct _queue { | |
/* Store the vector */ | |
Element *vector; | |
/* The element in the head of queue */ | |
size_t head; | |
/* The last element in queue */ | |
size_t tail; | |
/* Size of queue */ | |
size_t size; | |
} Queue; | |
/** | |
* Creates a Queue with Size | |
* | |
* @param size Length of vector of Element's | |
* @return return a Queue pointer (you shoud free it) | |
*/ | |
Queue *createQueue(size_t size); | |
/** | |
* Dealloc a Queue | |
* | |
* @param q Queue object | |
*/ | |
void deallocateQueue(Queue *q); | |
/** | |
* Add Element into Queue | |
* | |
* @param q Queue object | |
* @param n Element to be add | |
*/ | |
void enqueue(Queue *q, Element n); | |
/** | |
* Retrieve Element from Queue | |
* | |
* @param q Queue object | |
* @return Element (see typedef definition) | |
*/ | |
Element dequeue(Queue *q); | |
/** | |
* Is this queue full? | |
* | |
* @param q Queue object | |
* @return boolean (true if full) | |
*/ | |
bool full(Queue *q); | |
/** | |
* Is this queue empty? | |
* | |
* @param q Queue object | |
* @return boolean (true if empty) | |
*/ | |
bool empty(Queue *q); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment