Write a C program to (i) implement the following functions of a Queue using an i
ID: 3746982 • Letter: W
Question
Write a C program to (i) implement the following functions of a Queue using an integer array: queue init0: Make the queue empty empty: return true if the queue is empty. Return otherwise enqueue(val): add the item val to the queue dequeueremove the item least recently added to the queue front): return the item least recently added to the queue, but do not remove it. (ii) the program reads in integer values as input for a positive integer input value greater than 0, the program will insert the value into the queue, e. enqueue for any negative integer input value, the program will return the least recently added value from the queue if it is non-empty, i.e. front for the input value 0, the program will remove the least recently added value from the queue if it is non-empty, i.e. dequeue (iii) test your program with the following set of input: Expected Output Error-Queue is em 78 456 78 456 60 60 Error Queue is em Error Queue is emExplanation / Answer
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100 //Maximum Number of elements in queue
int array[MAX];
int front = 0;
int rear = -1;
int coun = 0; //Current count of the number of elements in queue
int front_element() {
return array[front];
}
bool isEmpty() {
return coun == 0;
}
void queue_init()
{
front=0;
rear=-1;
}
void enqueue(int data) {
if(rear == MAX-1) {
rear = -1;
}
array[++rear] = data;
coun++;
}
void dequeue() {
if(front == MAX) {
front = 0;
}
front++;
coun--;
}
int main()
{
while(1) //-999 to exit the program
{
printf("Enter the number "); //Enter the element
int d;
scanf("%d",&d);
if(d>0) //Chcking the condition
{
enqueue(d);
}
else if(d<0&&d!=-999)
{
if(isEmpty()) //Checking the empty condition
{
printf("Error-Queue is empty ");
}
else
printf("%d ",front_element());
}
else if(d==0)
{
if(isEmpty())
{
printf("Error-Queue is empty ");
}
else
{
dequeue();
}
}
else if(d==-999) //Exit the loop
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.