Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C Language For this weeks snippet create the routine to enqueue and dequeue from a single queue. To do this you first need to

In C Language

For this weeks snippet create the routine to enqueue and dequeue from a single queue. To do this you first need to update our notion of a header to point to the queue_front. The queue_front points to the first thing in the queue and is at the point of service (the next thing to be processed). You must also add a pointer queue_rear to point to the back end of the queue. This is important because it is where things get added to the queue. The nodes are exactly the same, with the next field pointing from first to next to next until the end which has a next pointer of null.

ENQUEUE, has a routine (see text) that checks the front_pointer in the header to see if it is NULL. If it is the new item it is added to the queue by setting the front pointer and the rear pointer to point to the new item (usually pointed to by the newpointer returned from the MALLOC.)

If the front_pointer is not null, then the rear_pointer_next (the next field of the last thing currently in the queue) is set to newpointer and the rear_pointer in the header is set to the newpointer as well. If you are keeping a queue_count then add one to it and the operation is complete.

DEQUEUE, has a routine that takes the data from the first node in the queue (pointed to by the queue_front pointer in the header) and returns it to the calling routine. Of course you would first check to make sure that the queue_front pointer is not null. If it is then you return an empty queue code. Otherwise you set queue_front = queue_frontnext (that is to say the new queue front is assigned the next field value from the old queue front). This eliminates the old first node. The count in the queue is reduced by one. The data from the old first node (previously stored) is returned.

Use positive integer data for each enqueue. Perform a series of enqueues and dequeues to exercise the code (print out the things that are dequeued). When the program encounters a negative input it should terminate and dequeue everything remaining in the queue printing them from first to last. This will illustrate the maintaining of order in the queue.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions