Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write in C language a code for queue using these functions are described as follows: createq: This function should return a pointer to a queue
Write in C language a code for queue using these functions are described as follows:
createq: This function should return a pointer to a queue type allocated from the heap. The head and tail pointers should be initialized to NULL.
On failure, the function should return a NULL pointer which is the default if the malloc function fails and print an error message. To use the malloc
function you should have something similar to:
newqueue queue malloc sizeof queue;
which will result in newqueue a pointer to the type queue having the address of the memory allocated. Return this value to the main program.
enqueue: This function should take as its arguments a pointer to a queue type and the value a person type that is to be added to the tail of the
queue, and return a bool NOTE: bool is defined in the standard library, stdbool.h included in queue.h indicating success or failure, do not
rely on the shortcut of returning an integer value. When you add a value to the queue you should:
Check to make sure the queue pointer points to something ie it is not NULL if it is NULL, print an error message and return a false
Allocate space for newnode, a node type, in the heap, if the malloc function fails, print an error message and return a false
Copy the person into the value field of the new node you will need to include string in your queue.c file
If the queue is empty, set the head to point to the newnode otherwise, set the next filed of the node at the tail pointer to point to newnode
Set the next field of newnode to NULL
Set the tail of the queue to point to newnode
dequeue: This function should take as its argument a pointer to the queue and should return the value from the head of the queue. It should then:
Check to make sure the queue pointer points to something ie it is not NULL if it is NULL, print an error message and return a person
value with an empty string and an age of zero
If the queue is empty, print an error message and return a person value with an empty string and an age of zero
Copy the value of the node at the queue head into a return variable
Set a temp pointer to the value of head
Set the head field to point to the next node.
Free the space associated with the old node.
isemptyqeue: This function should take as its argument a pointer to a queue and returns a true if the queue is empty or if the function was sent a
NULL pointer. Otherwise, a value of false is returned..
Check to make sure the queue pointer points to something ie it is not NULL if it is NULL, print an error message and return true
If the queue is not empty, return a false
Otherwise, return true
emptyq: This function should take as its argument a pointer to a queue and should free up the space associated with the nodes in the queue. This
function should only attempt to free nodes from an existing queue and should use the other functions you have created in this assignment.
Check to make sure the queue pointer points to something ie it is not NULL if it is NULL, print an error message and return false
If the queue is not empty, empty the queue and return true
freeq: This function takes as its argument a pointer to a queue in memory
Check to make sure the queue pointer points to something ie it is not NULL if it is NULL return true
If the queue is empty, free the memory for the queue and return true
If the queue is not empty, return false
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