Question
Need help writing this code for my computer class in c programming language. HELP is greatly Appreciated! you are to prepare a program that will
Need help writing this code for my computer class in c programming language. HELP is greatly Appreciated!
"you are to prepare a program that will provide you with a me nu to manipulate queues.
Your program should allow you to add to a queue, delete from a queue, list the contents of the queue, and quit."
the NOTES/PSUEDO CODE
"Queue A collection of data along with operations that access that data on a FIFO basis. FIFO first-in, first-out Structures for a queue: a single item in the queue consists of the data item and a pointer to the next queuenode structure typedef struct queuenode { int data; struct queuenode *next; } *node_pointer; to keep track of both ends of the queue, we will need an additional structure that contains two pointers that will point to the front and back queuenode: typedef struct endpointer { node_pointer front; node_pointer back; } *queue;
Subroutines are required for the following: Note: I'm assuming you have declared a variable in the main program using queue q; where q is a pointer to the endpointer structure (which points to the front and back queue nodes).
1. Initializing the queue (uses pass by reference) allocate memory the size of the endpointer structure and assign it to q set the front and back pointers to NULL (ie (*q)->front = NULL)
2. Checking to see if queue is full (no need to pass anything) Checks to see if enough memory is available for another queuenode structure. We try to allocate memory the size of a queuenode structure. If successful, temp will be pointing to that memory which now needs to be freed. If not, temp will be equal to NULL. Returns TRUE or FALSE
3. Checking to see if queue is empty (uses pass by value) If both front and back pointers are pointing to NULL then the queue is empty. NOTE: If one of them is NULL the other one should be too! Returns TRUE or FALSE
4. Adding new items to the queue (uses pass by reference) You must make sure queue is not full before calling this subroutine. Receives queue via pass by reference and data to be added via pass by value Allocates memory for new queuenode structure and assigns it to newnode (a nodepointer) sets the data portion equal to the item to be added sets the next pointer to point to the NULL NOTE: Since we always add to the back of the queue, the new node will always point to NULL. If the queue is empty ? we need to set both front and back to this new node (ie (*q)->front = newnode) Otherwise ? We need to attach it to to the next pointer of the back node (ie set (*q)->back->next to the new node). ? We also need to move back pointer to this new node since it is now the last node in the queue (ie (*q)->back = newnode)
5. Fetching items from the queue (uses pass by reference) You must make sure the queue is not empty before calling this subroutine. Receives queue via pass by reference Uses a temporary nodepointer called temp. set temporary nodepointer equal to the front of the queue sent via pass by reference set data to be removed equal to temp's data if the queuenode that is about to be removed is the only one in the queue (ie (*q)->front is equal to (*q)->back), ? then we need to make sure that both front and back get set to NULL... since it will be empty once removed. Otherwise, ? move front of queue to next queuenode structure NOTE: Can use (*q)->front->next or temp->next) free temp (ie free memory used for queuenode structure temp is pointing to) return data item that was fetched
6. Listing contents of the queue (uses pass by value) You must make sure queue is not empty prior to calling this subroutine. This subroutine will receive the queue via pass by value and list the contents without altering them. Create a temporary variable of type nodepointer set temporary variable to front queuenode (ie temp = q->front) while temp is not equal to NULL ? print data portion of temp ? move temp to next queuenode (temp = temp->next)"
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