Created
December 11, 2019 19:33
-
-
Save thinkphp/177e53eb9222593fa8337d784a482ea2 to your computer and use it in GitHub Desktop.
Queue Data Structure
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
#include <stdio.h> | |
#define Q_MAX 10 | |
struct Queue { | |
int queue[Q_MAX]; | |
int rear, | |
front; | |
}; | |
typedef struct Queue Queue; | |
void Queue_Init(Queue *Q) { | |
Q->rear = Q->front = -1; | |
}; | |
void enQueue(Queue *Q, int value) { | |
//check if the Queue is full; | |
if(Q->rear == Q_MAX - 1) { | |
fprintf(stderr, "%s", "Error: The Queue is full!"); | |
} else { | |
if(Q->front == -1) Q->front = 0; | |
Q->queue[++Q->rear] = value; | |
} | |
} | |
int deQueue(Queue *Q) { | |
if(Q->front == -1) { | |
fprintf(stderr, "%s", "Error: The Queue is empty!"); | |
} else { | |
int tmp = Q->queue[Q->front]; | |
Q->front++; | |
if(Q->front > Q->rear) Q->front = Q->rear = -1; | |
printf("deleted: %d\n", tmp); | |
return tmp; | |
} | |
} | |
int main(int argc, char *argv[]) { | |
Queue Q; | |
Queue_Init(&Q); | |
enQueue(&Q, 198); | |
enQueue(&Q, 90); | |
enQueue(&Q, 32); | |
deQueue(&Q); | |
deQueue(&Q); | |
deQueue(&Q); | |
deQueue(&Q); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment