Write a segment of code to perform each of the following ope

Write a segment of code to perform each of the following operations. You may call any of the member functions of Que Type. The details of the queue are encapsulated; you may use only the queue operations in the specifications to perform the operations. You may declare additional queue objects. Set last equal to the rear element in the queue, leaving the queue empty. Set second Element to the second element in the queue, leave the queue unchanged. Make two copies of the queue copy1 and copy2, leaving the queue empty. Split the queue into two parts (part1 and part2), leaving the queue unchanged, part1 should contain the first half of queue and part2 should contain the second half.

Solution

struct node                                             // Strucutre of Queue
{
        int d;
        struct node *next;
      
}
splitQueue(struct node *front, struct node *rear)               // front is pointing the first node of queue and rear is pointing to last.
{
        struct node *last, *secondElement, *copy1, *copy2;      // Copy1 for first half part and copy2 for second half part.
        last=rear;                                      // A. set last equal to rear element.
        secondElement = front->next;                    // B. set secondElement to second element of Queue.
        copy1 = front;                                  // C. Making copy of Queue by assigning the front
        copy2 = front;                                  // C. Making copy of Queue by assigning the front
        while(copy2->next!=NULL || copy2 != NULL)       // Finding middle element of Queue.
        {
                copy1 = copy1->next;                    // Moving copy1 queue to one next
                copy2 = copy2->next->next;              // Moving copy2 queue to two next
        }                                               // if copy2 at last position then copy1 should be in middle position of queue.
        copy2 = copy1->next;                            // So copy2 will start from copy1->next
        copy1->next= NULL;                              // copy1 will end with this middle position.
        copy1 = front;                                  // copy1 will start from front.
}

So copy1 is first half part and copy2 is second half part of Queue.

 Write a segment of code to perform each of the following operations. You may call any of the member functions of Que Type. The details of the queue are encapsu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site