Question
(20pt) Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h #include genlib.h typedef void *queueElementT; typedef struct queueCDT
(20pt) Recall queueADT structure which had the following queue.h interface:
/* queue.h */
#ifndef _queue_h
#define _queue_h
#include "genlib.h
typedef void *queueElementT;
typedef struct queueCDT *queueADT;
queueADT NewQueue(void);
void FreeQueue(queueADT queue);
void Enqueue(queueADT queue, queueElementT element);
queueElementT Dequeue(queueADT queue);
bool QueueIsEmpty(queueADT queue);
bool QueueIsFull(queueADT queue);
int QueueLength(queueADT queue);
queueElementT GetQueueElement(queueADT queue, int index);
#endif
Suppose its implementation is available as queue.o, so you can use all the functions in
queue.h, but you cannot change their implementation.
Now you are asked to complete the driver/application program in the next page by using the
above queue.h interface and implementing the necessary piece of codes needed for your
driver/application as defined below.
The driver/application simply reads all the double numbers from a file whose name is given
as a command line argument (this part is already done for you) and inserts (Enqueue) them
into the queue (you will do this part). Please note that queueElementT is defined to be
void *. So you cannot directly enqueue the double numbers into the queue. In this case,
remember you should allocate memory for your double numbers and then enqueue their
addresses in the queue!
Then you are asked to find and print the sum of the double values in the queue, but do not
dequeue or remove the numbers from the queue. So the queue still contains all the values.
Finally, before exiting from the program, make sure all the dynamically allocated memory
spaces and structures are released/freed.
/* driver.c */ /* assume all the necessary standard C libraries and booklibs are included here too */
#include "queue.h"
int main(int argc, char *argv[])
{
FILE *fp;
queueADT myQ;
double value, *ptr, sum;
int i;
if(argc!=2 || (fp=fopen(argv[1],"r")) == NULL){
printf("not enough argument or file cannot be opened "); exit(0);
}
myQ = NewQueue();
// [8pt] get each double value from the file, insert it into myQ
while(fscanf(fp, "%lf", &value) == 1){
}
// [7pt] find/print the sum of the values in myQ.
// but do not dequeue or remove the numbers from myQ.
// [5pt] release/free all the dynamically allocated memory spaces
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started