Question

Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h...

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\n"); 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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* 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\n"); exit(0);
}

myQ = NewQueue();

// [8pt] get each double value from the file, insert it into myQ
while(fscanf(fp, "%lf", &value) == 1){
ptr = (double *)malloc(sizeof(double));
*ptr = value;
Enqueue(myQ, ptr);
}
fclose(fp);
  
// [7pt] find/print the sum of the values in myQ.
// but do not dequeue or remove the numbers from myQ.
sum = 0;
i = QueueLength(myQ);
while(i > 0){
ptr = GetQueueElement(myQ, i-1);
sum += *ptr;
i--;
}

printf("Sum = %lf\n", sum);


// [5pt] release/free all the dynamically allocated memory spaces
FreeQueue(myQ);

}

Add a comment
Know the answer?
Add Answer to:
Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q);...

    typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q); int dequeue(Queue q); int queueSize(Queue q); void queueDestroy(Queue q)Exercise 1: Writing Assert Based Black Box Unit Tests for a Queue For this exercise you will need a copy of Queue.h, the queue interface for this exercise. You can download it or use the following command to copy it into your current directory cp «dp1091/public_html/2012/tlb/11/Queue.h. And a copy of the testQueue.c, the testing code we...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • Balment a la medicul Quoc that speciala a circular que has the following private data members...

    Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. The circular que simplemented using an atay. Your submission should consist of four separate files the three source code file header file.implementation file and main program or routine and the program otput. When making the submission, please do not submit it as a file Private data members: int the tray int current size oprema prinete the first met of the...

  • Objective: In this assignment you will do the following: (1) manipulate pointers, (2) allocate me...

    Objective: In this assignment you will do the following: (1) manipulate pointers, (2) allocate memory dynamically, (3) implement a default constructor, copy constructor and destructor, (4) use only one pointer to add to the back and to dequeue from the front of the queue. Assignment Description: You will implement a doubly-linked circular queue of integers. Consider the following class declarations when implementing BQUEUE. As always, you must comment your declaration and implementation files, "BQUEUE.h" and "BQUEUE.cpp", respectively. class bqnode {...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • I need this to be in C Write the implementation file, priority queue.c, for the interface...

    I need this to be in C Write the implementation file, priority queue.c, for the interface in the given header file, priority queue.h. Turn in your priority queue.c file and a suitable main program, main.c, that tests the opaque object. priority queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT