Write a generic queue in C using a \"void * \" implementation. The implementatio
ID: 3796659 • Letter: W
Question
Write a generic queue in C using a "void * " implementation. The implementation should use an array to hold the queue elements. Use a "circular" implementation that wraps the end of the queue around to the front of the array once it reaches the end of the array. Your implementation will need to keep track of two indices-one that points to the front of the queue and one that points to the end of the queue. If you have the Scott text, you can look at Figure 8.4 on page 413 to get an idea of the implementation I want. The API that you will implement is: I have included several files for this problem: queueDriver.c: the driver program queue.h: the .h file you should use. Notice that it does not contain a struct for queue. You should place your queue struct into queue.c so that you achieve information hiding. If you are confused about how to do information hiding in C, then re-watch my video on Modules-C-C++ or look at my notes about modules in C and C++, queuelnput: some sample input. You should test your program with other input, but do not worry about bad input, input that overflows the queue, input that tries to access an empty queue, etc. If you have questions about what your output should look like, I have placed a C executable of queueDriver in/home/bv2/cs365/hw/hws. The queue in question 1 requires that you write downcasts in order to extract values of a specific type, such as int. from the queue. This can be dangerous because the downcasts are not checked at run time to ensure that they are safe. Given an example of how this failure to check the downcasts could lead to a catastrophic result (i.e., a core dump or totally non-sensical output).Explanation / Answer
I have made a program for some other person , may this is help ful for you
/*Implementation of De-queue using arrays*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 10
struct dequeue
{
int front,rear;
int arr[MAX];
}dq;
/*If flag is zero, insertion is done at beginning
else if flag is one, insertion is done at end.
*/
void enqueue(dq *q,int x,int flag)
{
int i;
if(q->rear==MAX-1)
{
printf(“nQueue overflow!”);
exit(1);
}
if(flag==0)
{
for(i=q->rear;i>=q->front;i–)
q->arr[i+1]=q->arr[i];
q->arr[q->front]=x;
q->rear++;
}
else if(flag==1)
{
q->arr[++q->rear]=x;
}
else
{
printf(“nInvalid flag value…!”);
return;
}
}
void de_queue(dq *q,int flag)
{
int i;
/*front is initialized with zero, then rear=-1
indicates underflow*/
if(q->rear<q->front)
{
printf(“nQueue Underflow…!”);
exit(1);
}
if(flag==0)/*deletion at beginning*/
{
for(i=q->front;i<=q->rear;i++)
q->arr[i]=q->arr[i+1];
q->arr[q->rear]=0;
q->rear–;
}
else if(flag==1)
{
q->arr[q->rear–]=0;
}
else
{
printf(“nInvalid flag value…!”);
return;
}
}
void display(dq *q)
{
int i;
for(i=q->front;i<=q->rear;i++)
printf(“%dt”,q->arr[i]);
}
void main()
{
dq q;
q.front=0;
q.rear=-1;
int ch,num;
while(1)
{
clrscr();
printf(“nMenu-Double Ended Queue”);
printf(“nn1. Enqueue – Begin”);
printf(“n2. Enqueue – End”);
printf(“n3. Dequeue – Begin”);
printf(“n4. Dequeue – End”);
printf(“n5. Display”);
printf(“n6. Exit”);
printf(“nnEnter your choice?”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“nEnter the number?”);
scanf(“%d”,&num);
enqueue(&q,num,0);
break;
case 2:
printf(“nEnter the number?”);
scanf(“%d”,&num);
enqueue(&q,num,1);
break;
case 3:
printf(“nDeleting element from beginning…!”);
de_queue(&q,0);
break;
case 4:
printf(“nDeleting element from end…!”);
de_queue(&q,1);
break;
case 5:
display(&q);
break;
case 6:
exit(0);
default:
printf(“nInvalid Choice…!”);
}
getch();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.