Created
June 28, 2016 16:16
-
-
Save jerweb63/4913a3484468ffdf5e131bf6f623c3a4 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#define CAPACITY 10 | |
typedef enum | |
{ | |
false, | |
true | |
} | |
bool; | |
typedef struct | |
{ | |
int head; | |
int length; | |
int elements[CAPACITY]; | |
} | |
queue; | |
bool dequeue(queue* q, int* element); | |
int | |
main (int argc, char *argv[]) | |
{ | |
int b; // dequeued element | |
queue q; | |
q.head = 0; | |
q.length = 4; | |
q.elements[0] = 6; | |
q.elements[1] = 2; | |
q.elements[2] = 3; | |
q.elements[3] = 1; | |
dequeue(&q, &b); | |
dequeue(&q, &b); | |
if ( b == true) printf("dequeue() success\n"); | |
for (int i = q.head; i <= q.length + 1; i++) | |
{ | |
printf("%d ", q.elements[i]); | |
} | |
return 0; | |
} | |
bool dequeue(queue* q, int* element) | |
{ | |
if (q->length > 0) | |
{ | |
*element = q->elements[q->head]; | |
q->head = (q->head + 1) % CAPACITY; | |
q->length--; | |
printf("dequeue(): %d\n", *element); | |
printf("head is: %d\n", q->head); | |
return true; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment