Question
IF CORRECT I AND MY FREINDS WOULD GIVE A BIG LIKE PROMISE Your task is to modify the code in the files queue.hand queue.c to
IF CORRECT I AND MY FREINDS WOULD GIVE A BIG LIKE PROMISE
Your task is to modify the code in the files queue.hand queue.c to fully implement the following functions.
queue_new: queue_free queue_insert_head queue_insert_tail queue_remove_head queue_size queue_reverse
QUEUE. C
/* * Code for basic C skills diagnostic. * Developed for courses 15-213/18-213/15-513 by R. E. Bryant, 2017 * Modified to store strings, 2018 */
/* * This program implements a queue supporting both FIFO and LIFO * operations. * * It uses a singly-linked list to represent the set of queue elements */
#include
#include "harness.h" #include "queue.h"
/* Create empty queue. Return NULL if could not allocate space. */ queue_t *q_new() { queue_t *q = malloc(sizeof(queue_t)); /* What if malloc returned NULL? */ q->head = NULL; return q; }
/* Free all storage used by queue */ void q_free(queue_t *q) { /* How about freeing the list elements and the strings? */ /* Free queue structure */ free(q); }
/* Attempt to insert element at head of queue. Return true if successful. Return false if q is NULL or could not allocate space. Argument s points to the string to be stored. The function must explicitly allocate space and copy the string into it. */ bool q_insert_head(queue_t *q, char *s) { list_ele_t *newh; /* What should you do if the q is NULL? */ newh = malloc(sizeof(list_ele_t)); /* Don't forget to allocate space for the string and copy it */ /* What if either call to malloc returns NULL? */ newh->next = q->head; q->head = newh; return true; }
/* Attempt to insert an element at the tail of the queue. Return true if successful. Return false if q is NULL or could not allocate space. Argument s points to the string to be stored. The function must explicitly allocate space and copy the string into it. */ bool q_insert_tail(queue_t *q, char *s) { /* You need to write the complete code for this function */ /* Remember: It should operate in O(1) time */ return false; }
/* Attempt to remove element from head of queue. Return true if successful. Return false if queue is NULL or empty. If sp is non-NULL and an element is removed, copy the removed string to *sp (up to a maximum of bufsize-1 characters, plus a null terminator.) The space used by the list element and the string should be freed. */ bool q_remove_head(queue_t *q, char *sp, size_t bufsize) { /* You need to fix up this code. */ q->head = q->head->next; return true; }
/* Return number of elements in queue. Return 0 if q is NULL or empty */ int q_size(queue_t *q) { /* You need to write the code for this function */ /* Remember: It should operate in O(1) time */ return 0; }
/* Reverse elements in queue No effect if q is NULL or empty This function should not allocate or free any list elements (e.g., by calling q_insert_head, q_insert_tail, or q_remove_head). It should rearrange the existing ones. */ void q_reverse(queue_t *q) { /* You need to write the code for this function */ }
QUEUE. H
/* * Code for basic C skills diagnostic. * Developed for courses 15-213/18-213/15-513 by R. E. Bryant, 2017 * Extended to store strings, 2018 */
/* * This program implements a queue supporting both FIFO and LIFO * operations. * * It uses a singly-linked list to represent the set of queue elements */
#include
/************** Data structure declarations ****************/
/* Linked list element (You shouldn't need to change this) */ typedef struct ELE { /* Pointer to array holding string. This array needs to be explicitly allocated and freed */ char *value; struct ELE *next; } list_ele_t;
/* Queue structure */ typedef struct { list_ele_t *head; /* Linked list of elements */ /* You will need to add more fields to this structure to efficiently implement q_size and q_insert_tail */ } queue_t;
/************** Operations on queue ************************/
/* Create empty queue. Return NULL if could not allocate space. */ queue_t *q_new();
/* Free ALL storage used by queue. No effect if q is NULL */ void q_free(queue_t *q);
/* Attempt to insert element at head of queue. Return true if successful. Return false if q is NULL or could not allocate space. Argument s points to the string to be stored. The function must explicitly allocate space and copy the string into it. */ bool q_insert_head(queue_t *q, char *s);
/* Attempt to insert element at tail of queue. Return true if successful. Return false if q is NULL or could not allocate space. Argument s points to the string to be stored. The function must explicitly allocate space and copy the string into it. */ bool q_insert_tail(queue_t *q, char *s);
/* Attempt to remove element from head of queue. Return true if successful. Return false if queue is NULL or empty. If sp is non-NULL and an element is removed, copy the removed string to *sp (up to a maximum of bufsize-1 characters, plus a null terminator.) The space used by the list element and the string should be freed. */ bool q_remove_head(queue_t *q, char *sp, size_t bufsize);
/* Return number of elements in queue. Return 0 if q is NULL or empty */ int q_size(queue_t *q);
/* Reverse elements in queue No effect if q is NULL or empty This function should not allocate or free any list elements (e.g., by calling q_insert_head, q_insert_tail, or q_remove_head). It should rearrange the existing ones. */ void q_reverse(queue_t *q);
OUTPUT:
--- Trace Points +++ TESTING trace trace-01-ops: # Test of insert_head and remove_head --- trace-01-ops 6/6 +++ TESTING trace trace-02-ops: # Test of insert_head, insert_tail, and remove_head --- trace-02-ops 6/6 +++ TESTING trace trace-03-ops: # Test of insert_head, insert_tail, reverse, and remove_head --- trace-03-ops 6/6 +++ TESTING trace trace-04-ops: # Test of insert_head, insert_tail, and size --- trace-04-ops 6/6 +++ TESTING trace trace-05-ops: # Test of insert_head, insert_tail, remove_head reverse, and size --- trace-05-ops 6/6 +++ TESTING trace trace-06-string: # Test of truncated strings --- trace-06-string 7/7 +++ TESTING trace trace-07-robust: # Test operations on NULL queue This queue doesn't exist! This queue doesn't exist! --- trace-07-robust 7/7 +++ TESTING trace trace-08-robust: # Test operations on empty queue --- trace-08-robust 7/7 +++ TESTING trace trace-09-robust: # Test remove_head with NULL argument --- trace-09-robust 7/7 +++ TESTING trace trace-10-malloc: # Test of malloc failure on new can't allocate space for the queue. can't allocate space for the queue. --- trace-10-malloc 7/7 +++ TESTING trace trace-11-malloc: # Test of malloc failure on insert_head Couldn't allocate space for the string! Couldn't allocate space for the string! Couldn't allocate space for the element! Couldn't allocate space for the string! Couldn't allocate space for the string! Couldn't allocate space for the element! Couldn't allocate space for the element! Couldn't allocate space for the string! Couldn't allocate space for the element! --- trace-11-malloc 7/7 +++ TESTING trace trace-12-malloc: # Test of malloc failure on insert_tail Couldn't allocate space for the string! Couldn't allocate space for the element! Couldn't allocate space for the element! Couldn't allocate space for the string! Couldn't allocate space for the element! Couldn't allocate space for the string! Couldn't allocate space for the element! Couldn't allocate space for the element! --- trace-12-malloc 7/7 +++ TESTING trace trace-13-perf: # Test performance of insert_tail --- trace-13-perf 7/7 +++ TESTING trace trace-14-perf: # Test performance of size --- trace-14-perf 7/7 +++ TESTING trace trace-15-perf: # Test performance of insert_tail, size, and reverse --- trace-15-perf 7/7 --- TOTAL 100/100
IF CORRECT I AND MY FREINDS WOULD GIVE A BIG LIKE PROMISE
2 Overview The file queue.h contains declarations of the following structures: /* Linked list element */ typedef struct list_ele { char *value; struct list_ele *next; } list_ele_t; /* Queue structure */ typedef struct { list_ele *head; /* First element in in the queue */ } queue_t; 2 Overview The file queue.h contains declarations of the following structures: /* Linked list element */ typedef struct list_ele { char *value; struct list_ele *next; } list_ele_t; /* Queue structure */ typedef struct { list_ele *head; /* First element in in the queue */ } queue_t
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