Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please comment this code. queueADT.c #include #include #include \"sqjuggler.h\"

ID: 3719467 • Letter: P

Question

Please comment this code.

queueADT.c

#include
#include
#include "sqjuggler.h"

struct queue_type{
int contents[QUEUE_SIZE];
int last,first,length;
};

static void terminate(const char *message)
{
printf("%s ",message);
exit(EXIT_FAILURE);
}

queue create(void)
{
Queue q=malloc(sizeof(struct queue_type));
if(q==NULL)
terminate("Error in create: queue could not be created.");
q->last=0;
q->first=0;
q->length=0;
return q;
}

void destroy(Queue q)
{
make_empty(q);

free(q);

}

void make_empty(Queue q)
{
q->last=0;
q->first=0;
q->length=0;
}

bool is_empty(Queue q)
{
return q->length==0;
}

bool is_full(Queue q)
{
return q->length==QUEUE_SIZE;
}

void insert(Queue q,int i)
{
if(is_full(q))
terminate("Error in insert: queue is full.");

q->contents[q->last++]=i;
if(q->length==0)
q->first=q->last;
q->length=q->length+1;
if(q->last>=QUEUE_SIZE)
q->last=0;
}

Item remove_que(Queue q)
{
int element=0;

if(is_empty(q))

terminate("Error in removal: queue is empty.");

element=q->contents[(q->first)-1];
q->length=q->length-1;
q->first++;

if(q->first>q->last||q->first>QUEUE_SIZE)
q->first=0;
return element;
}

int first(Queue q)
{
return q->contents[(q->first)-1];
}

int last(Queue q)
{
return q->contents[(q->last)-1];
}

Explanation / Answer

If you have any doubts, please give me comment...

#include "sqjuggler.h"

//defining queue_type structures to stores contents, first, last, length

struct queue_type

{

int contents[QUEUE_SIZE];

int last, first, length;

};

//terminate the program with provided argumemnt of message

static void terminate(const char *message)

{

printf("%s ", message);

exit(EXIT_FAILURE);

}

//creating dynamic memory allocation for queue and initilize values for last, first and length to 0

queue create(void)

{

//creating dynamic memory using malloc

Queue q = malloc(sizeof(struct queue_type));

if (q == NULL)

terminate("Error in create: queue could not be created.");

//initilize values to 0

q->last = 0;

q->first = 0;

q->length = 0;

return q;

}

//destroying queue

void destroy(Queue q)

{

//initially making empty for giving queue

make_empty(q);

//releasing memory of queue q

free(q);

}

//making empty

void make_empty(Queue q)

{

q->last = 0;

q->first = 0;

q->length = 0;

}

//returns true whether queue is empty else false

bool is_empty(Queue q)

{

return q->length == 0;

}

//returns true whether queue is full else false

bool is_full(Queue q)

{

return q->length == QUEUE_SIZE;

}

//inserting element into queue q of value i

void insert(Queue q, int i)

{

//check whether queue is empty or not

if (is_full(q))

terminate("Error in insert: queue is full.");

//appending value into last position in queue contents

q->contents[q->last++] = i;

if (q->length == 0)

q->first = q->last;

q->length = q->length + 1;

if (q->last >= QUEUE_SIZE)

q->last = 0;

}

//removing element in queue

Item remove_que(Queue q)

{

int element = 0;

//checking whether queue is empty or not

if (is_empty(q))

terminate("Error in removal: queue is empty.");

//removing first element in queue

element = q->contents[(q->first) - 1];

q->length = q->length - 1;

//forwarding queue first elemnt after remove

q->first++;

if (q->first > q->last || q->first > QUEUE_SIZE)

q->first = 0;

return element;

}

//returing first element of queue

int first(Queue q)

{

return q->contents[(q->first) - 1];

}

//returning last element of queue

int last(Queue q)

{

return q->contents[(q->last) - 1];

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote