Created
December 11, 2019 19:32
-
-
Save thinkphp/58114bced382332c0a84828abdd8a4ea 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
/** | |
* Queue Data Structure implemented as Array | |
*/ | |
#include <stdio.h> | |
#define QUEUE_MAX 5 | |
//declare my queue and two pointers to this data structure | |
int queue[ QUEUE_MAX ], | |
front = -1, | |
rear = -1; | |
//add new elements in the Queue; | |
void enQueue(int val) { | |
//checks if the queue is full | |
if(rear == QUEUE_MAX - 1) { | |
fprintf(stderr, "%s\n", "Error: Queue overflow! I mean the Queue is full!"); | |
} else { | |
printf("Added: %d\n", val); | |
if(front == -1) front = 0; | |
rear++; | |
queue[rear] = val; | |
} | |
} | |
//remove elements from Queue; | |
int deQueue() { | |
//check if the queue is empty | |
if(front == -1) { | |
fprintf(stderr, "%s", "Error: Queue is empty!"); | |
} else { | |
int tmp = queue[front]; | |
front++; | |
if(front > rear) front = rear = -1; | |
printf("Deleted: %d\n", tmp); | |
return tmp; | |
} | |
} | |
int main() { | |
enQueue(1); | |
enQueue(5); | |
enQueue(34); | |
enQueue(41); | |
enQueue(50); | |
enQueue(6); | |
deQueue(); | |
deQueue(); | |
deQueue(); | |
deQueue(); | |
deQueue(); | |
deQueue(); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment