Skip to content

Instantly share code, notes, and snippets.

@ioanzicu
Last active April 3, 2020 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ioanzicu/e8add0f37ada7d5fc12b5bee74585836 to your computer and use it in GitHub Desktop.
Save ioanzicu/e8add0f37ada7d5fc12b5bee74585836 to your computer and use it in GitHub Desktop.
Implementation of the Queue using Array.
#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