Last active
April 3, 2020 10:06
-
-
Save ioanzicu/e8add0f37ada7d5fc12b5bee74585836 to your computer and use it in GitHub Desktop.
Implementation of the Queue using Array.
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> | |
#include<stdlib.h> | |
struct Queue { | |
int size; | |
int front; | |
int rear; | |
int *Q; | |
}; | |
void create(struct Queue *q, int size) { | |
q->size = size; | |
q->front = q->rear = -1; | |
q->Q = (int *) malloc(q->size*sizeof(int)); | |
} | |
void enqueue(struct Queue *q, int value) { | |
if (q->rear == q->size - 1) | |
printf("Queue is Full"); | |
else { | |
q->rear++; | |
q->Q[q->rear] = value; | |
} | |
} | |
int dequeue(struct Queue *q) { | |
int x = -1; | |
if (q->front == q->rear) | |
printf("Queue is Empty\n"); | |
else | |
{ | |
q->front++; | |
x = q->Q[q->front]; | |
} | |
return x; | |
} | |
void display(struct Queue q) { | |
int i; | |
for (i = q.front + 1; i <= q.rear; ++i) | |
printf("%d ", q.Q[i]); | |
printf("\n"); | |
} | |
int main() { | |
struct Queue q; | |
create(&q, 5); | |
enqueue(&q, 10); | |
enqueue(&q, 20); | |
enqueue(&q, 30); | |
display(q); | |
printf("Dequeue: %i", dequeue(&q)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment