Array Implementation of queue using C
#include<stdio.h>
#include<stdlib.h>
#define size 5
#define TRUE 1
#define FALSE 0
typedef struct {
int a[size];
int front;
int rear;
} queue;
queue *q;
int main() /* main() function begins program execution */ {
int ch; /* variable declarion for choice in switch case */
int num; /* variable declaration for data */
q = (queue*) malloc(sizeof (queue)); /*Memory Alloaction*/
q->front = -1;
q->rear = -1;
while (1) {
printf("\nARRAY IMPLEMENTATION OF CQUEUE.\n");
printf("\n======================\n");
printf("|-- Menu Selection --|\n");
printf("======================\n");
printf("| 1. Enqueue |\n");
printf("| 2. Dequeue |\n");
printf("| 3. IsEmpty |\n");
printf("| 4. IsFull |\n");
printf("| 5. Clear |\n");
printf("| 6. Display |\n");
printf("| 0. Exit |\n");
printf("======================\n");
printf("Enter choice(0-6):");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the value u wanna insert: ");
scanf("%d", &num);
printf("ENQUEUE() OUTPUT:-");
enqueue(num) ? printf("TRUE\n") : printf("FALSE\n");
//printf("%d HAS INSERTED IN Q.\n",num);
break;
case 2:
printf("DQUEUE() OUTPUT:-");
dqueue() ? printf("TRUE\n") : printf("FALSE\n");
break;
case 3:
printf("ISEMPTY() OUTPUT:-");
isempty() ? printf("TRUE\n") : printf("FALSE\n");
break;
case 4:
printf("ISFULL() OUTPUT:-\n\t");
isfull() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 5:
printf("CLEAR() OUTPUT:-\n\t");
clear();
break;
case 6:
printf("DISPLAY() OUTPUT:-\n\t");
display();
break;
case 0:
printf("Goodbye...!!!!\n");
exit(0);
}
}
return 0; //Indicate programme ended succesfully
} //End function main().
int enqueue(int n) {
if (isfull())
return FALSE;
if (q->rear == size - 1) /*rear is at last index*/
q->rear = 0;
else
q->rear++;
q->a[q->rear] = n;
if (q->front == -1)
q->front = 0;
return TRUE;
}
int dqueue() {
int i, data;
if (isempty())
return FALSE;
else {
data = q->a[q->front];
q->a[q->front] = 0;
if (q->front == q->rear)
q->front = q->rear = -1;
else {
if (q->front == size - 1)
q->front = 0;
else
q->front++;
}
return TRUE;
}
}
int display() {
int i;
if (isempty())
printf("Queue is empty....!!!\n");
else {
if (q->rear < q->front) {
printf("Q = ( ");
for (i = 0; i < size; i++) {
printf("%d ", q->a[i]);
}
printf(").");
} else {
printf("Q = ( ");
for (i = q->front; i <= q->rear; i++) {
printf("%d ", q->a[i]);
}
printf(").");
}
}
return 0;
}
int isfull() /* check whether the queue is full. */ {
if (((q->front == 0)&&(q->rear == size - 1)) || (q->front == q->rear + 1))
return TRUE;
else
return FALSE;
}
int isempty() /*check whether the queue is (). */ {
if (q->front == -1)
return TRUE;
else
return FALSE;
}
int clear() /*clear the queue */ {
if (isempty())
printf("Q is already empty....!!!!!\n");
else {
q->front = -1;
printf("Q HASBEEN CLEARED.\n");
}
return 0;
}
#include<stdlib.h>
#define size 5
#define TRUE 1
#define FALSE 0
typedef struct {
int a[size];
int front;
int rear;
} queue;
queue *q;
int main() /* main() function begins program execution */ {
int ch; /* variable declarion for choice in switch case */
int num; /* variable declaration for data */
q = (queue*) malloc(sizeof (queue)); /*Memory Alloaction*/
q->front = -1;
q->rear = -1;
while (1) {
printf("\nARRAY IMPLEMENTATION OF CQUEUE.\n");
printf("\n======================\n");
printf("|-- Menu Selection --|\n");
printf("======================\n");
printf("| 1. Enqueue |\n");
printf("| 2. Dequeue |\n");
printf("| 3. IsEmpty |\n");
printf("| 4. IsFull |\n");
printf("| 5. Clear |\n");
printf("| 6. Display |\n");
printf("| 0. Exit |\n");
printf("======================\n");
printf("Enter choice(0-6):");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the value u wanna insert: ");
scanf("%d", &num);
printf("ENQUEUE() OUTPUT:-");
enqueue(num) ? printf("TRUE\n") : printf("FALSE\n");
//printf("%d HAS INSERTED IN Q.\n",num);
break;
case 2:
printf("DQUEUE() OUTPUT:-");
dqueue() ? printf("TRUE\n") : printf("FALSE\n");
break;
case 3:
printf("ISEMPTY() OUTPUT:-");
isempty() ? printf("TRUE\n") : printf("FALSE\n");
break;
case 4:
printf("ISFULL() OUTPUT:-\n\t");
isfull() ? printf("TRUE.\n") : printf("FALSE.\n");
break;
case 5:
printf("CLEAR() OUTPUT:-\n\t");
clear();
break;
case 6:
printf("DISPLAY() OUTPUT:-\n\t");
display();
break;
case 0:
printf("Goodbye...!!!!\n");
exit(0);
}
}
return 0; //Indicate programme ended succesfully
} //End function main().
int enqueue(int n) {
if (isfull())
return FALSE;
if (q->rear == size - 1) /*rear is at last index*/
q->rear = 0;
else
q->rear++;
q->a[q->rear] = n;
if (q->front == -1)
q->front = 0;
return TRUE;
}
int dqueue() {
int i, data;
if (isempty())
return FALSE;
else {
data = q->a[q->front];
q->a[q->front] = 0;
if (q->front == q->rear)
q->front = q->rear = -1;
else {
if (q->front == size - 1)
q->front = 0;
else
q->front++;
}
return TRUE;
}
}
int display() {
int i;
if (isempty())
printf("Queue is empty....!!!\n");
else {
if (q->rear < q->front) {
printf("Q = ( ");
for (i = 0; i < size; i++) {
printf("%d ", q->a[i]);
}
printf(").");
} else {
printf("Q = ( ");
for (i = q->front; i <= q->rear; i++) {
printf("%d ", q->a[i]);
}
printf(").");
}
}
return 0;
}
int isfull() /* check whether the queue is full. */ {
if (((q->front == 0)&&(q->rear == size - 1)) || (q->front == q->rear + 1))
return TRUE;
else
return FALSE;
}
int isempty() /*check whether the queue is (). */ {
if (q->front == -1)
return TRUE;
else
return FALSE;
}
int clear() /*clear the queue */ {
if (isempty())
printf("Q is already empty....!!!!!\n");
else {
q->front = -1;
printf("Q HASBEEN CLEARED.\n");
}
return 0;
}