Created
July 6, 2014 22:34
-
-
Save jomasero/faa628a99f1c36390145 to your computer and use it in GitHub Desktop.
FIFO circular de enteros en C
This file contains hidden or 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
| /* Very simple queue | |
| * These are FIFO queues which discard the new data when full. | |
| * | |
| * Queue is empty when in == out. | |
| * If in != out, then | |
| * - items are placed into in before incrementing in | |
| * - items are removed from out before incrementing out | |
| * Queue is full when in == (out-1 + QUEUE_SIZE) % QUEUE_SIZE; | |
| * | |
| * The queue will hold QUEUE_ELEMENTS number of items before the | |
| * calls to QueuePut fail. | |
| */ | |
| /* Queue structure */ | |
| #define QUEUE_ELEMENTS 100 | |
| #define QUEUE_SIZE (QUEUE_ELEMENTS + 1) | |
| int Queue[QUEUE_SIZE]; | |
| int QueueIn, QueueOut; | |
| void QueueInit(void) { | |
| QueueIn = QueueOut = 0; | |
| } | |
| int QueuePut(int new) { | |
| if(QueueIn == (( QueueOut - 1 + QUEUE_SIZE) % QUEUE_SIZE)) { | |
| return -1; /* Queue Full*/ | |
| } | |
| Queue[QueueIn] = new; | |
| QueueIn = (QueueIn + 1) % QUEUE_SIZE; | |
| return 0; // No errors | |
| } | |
| int QueueGet(int *old) { | |
| if(QueueIn == QueueOut) { | |
| return -1; /* Queue Empty - nothing to get*/ | |
| } | |
| *old = Queue[QueueOut]; | |
| QueueOut = (QueueOut + 1) % QUEUE_SIZE; | |
| return 0; // No errors | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment