Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Complete two C functions in Deque.c marked with the comment /* FIX ME */ 1 Problem 1: Print Deque Complete the C function for printing
Complete two C functions in Deque.c marked with the comment /* FIX ME */ 1 Problem 1: Print Deque Complete the C function for printing on the screen elements of Deque starting from the front. An input argument of the function is the pointer to Deque. Your implementation should allow that indices of the front and back of Deque may wrap around the block of memory occupied by Deque. 2 Problem 2: Remove Back Deque Complete the C function for removing either one element at the back of Deque, or all repetitions of the element at the back until you encounter a different value which will then become the new back element of Deque. Input arguments of the function include the pointer to Deque, and flag that indicates one removal if flag = 1, or removal of all repetitions from the back of Deque if flag = 1. #include #include #include #include #define TYPE int #define EQ(a,b) (a == b) struct Deque{ TYPE *data; int size; /* number of elements in data */ int capacity; /* memory capacity of the deque */ int front; /* index of the front end of deque */ }; /*----------------------------------------------*/ void initDeque(struct Deque * dq, int cap); void addFrontDeque(struct Deque *dq, TYPE val); void printDeque(struct Deque *dq); void removeBackAllDeque(struct Deque *dq, int flag); /*----------------------------------------------*/ void _doubleCapacityDeque (struct Deque *dq); /*----------------------------------------------*/ int main(int argc, char **argv){ struct Deque dq; int i; TYPE val; int capacity = atoi(argv[1]); int size = atoi(argv[2]); int flag = atoi(argv[3]); assert(capacity > 0 && size >= 0); srand(time(NULL)); /*initialize random generator*/ initDeque(&dq, capacity); /*initialize Deque*/ for(i=1; i<=size; i++) { val = (TYPE) rand() % 5; addFrontDeque(&dq, val); /*add val to Deque*/ } printf("Deque before remove: "); printDeque(&dq); /*print all elements*/ removeBackAllDeque(&dq, flag); printf("Deque after remove: "); printDeque(&dq); /*print all elements*/ return 0; } /*----------------------------------------------*/ /* Initialize Deque */ /* input arguments: dq -- pointer to deque cap = capacity of a memory block to be allocated */ void initDeque(struct Deque * dq, int cap) { assert (dq); dq->capacity = cap; dq->size = dq->front = 0; dq->data = (TYPE *) malloc(dq->capacity * sizeof(TYPE)); assert (dq->data != 0); } /*----------------------------------------------*/ /* input arguments: dq -- pointer to deque val -- value of the data element to be added */ void addFrontDeque(struct Deque *dq, TYPE val) { assert(dq); if (dq->size == dq->capacity) _doubleCapacityDeque(dq); dq->front--; if (dq->front < 0) dq->front += dq->capacity; dq->data[dq->front] = val; dq->size ++; } /*----------------------------------------------*/ /* Double the memory capacity of a deque */ void _doubleCapacityDeque (struct Deque *dq) { assert (dq); int j; TYPE * oldData = dq->data; /*memorize old data*/ int oldFront = dq->front; /*memorize old index of the front end of deque*/ int oldSize = dq->size; /*memorize old size*/ int oldCapacity = dq->capacity; /*memorize old capacity*/ initDeque(dq, 2 * oldCapacity); /*new memory allocation*/ for (j = 0 ; j < oldSize; j++) {/*copy back old data*/ dq->data[j] = oldData[oldFront++]; if (oldFront >= oldCapacity) oldFront = 0; } free(oldData); dq->size = oldSize; } /*----------------------------------------------*/ /* Print all elements of a deque starting from the front */ /* input arguments: dq -- pointer to deque */ void printDeque(struct Deque * dq) { assert (dq); /* FIX ME */ } /*----------------------------------------------*/ /* Remove one or all element(s) from the back of Deque */ /* input arguments: dq -- pointer to deque flag -- flag = 1:remove only one element at the back flag = -1:remove all repetitions of the element at the back */ void removeBackAllDeque(struct Deque *dq, int flag) { assert(dq && (flag == 1 || flag == -1)); /* FIX ME */ }
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