Server IP : 172.16.15.8 / Your IP : 3.145.106.7 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/rnlink/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// Test Driver for Queue CopyNth as a client function #include <iostream> // #include "queuearray.h" // to test array implementation #include "queuell.h" // to test using linked implementation using namespace std; //---------------------------------------------------------------------- // CopyNth shows copy of Nth item from top of queue, front being item #1 //---------------------------------------------------------------------- // Accepts: reference to queue to look at // N - logical position of desired item // Returns: copy of item at Nth position from front //---------------------------------------------------------------------- // CopyNth as a client of the QueueType Class void CopyNth (QueueType<char>& aqueue, int N, char& copyitem) { QueueType<char> tempqueue; // a queue for temp storage of values char tmp; // temp. pointer to values if(!aqueue.IsEmpty()) // So long as you're not trying to { // find a value in an empty queue... for(int i=0; i < N-1; i++) // Until the Nth value is at the top { // of aqueue... aqueue.Dequeue(tmp); // dequeue from aqueue and tempqueue.Enqueue(tmp); // store in order in tempqueue. } aqueue.Dequeue(copyitem); // Copy the Nth value by dequeueing tempqueue.Enqueue(copyitem); // enqueueing into tempqueue. while(!aqueue.IsEmpty()) // Then copy the rest of the queue { // in order into tempqueue (so that aqueue.Dequeue(tmp); // the line remains in it's original tempqueue.Enqueue(tmp); // order)... } while(!tempqueue.IsEmpty()) // and then copy the whole line back { // into aqueue with the order intact tempqueue.Dequeue(tmp); aqueue.Enqueue(tmp); } } else cout << "Error - Trying to view an item in an empty stack"; } //--------------------------- Main function ---------------------------- int main () { QueueType<char> chqueue; // test queue char copych; // holds character viewed by CopyNth // Enqueue some items chqueue.Enqueue ('A'); chqueue.Enqueue ('B'); chqueue.Enqueue ('C'); chqueue.Enqueue ('D'); chqueue.Enqueue ('E'); // Do some CopyNth calls - queue has 5 values cout << "\nTesting CopyNth for queue with 5 items"; CopyNth (chqueue, 5, copych); cout << "\n\nThe 5th value should be E and it is " << copych; CopyNth (chqueue, 1, copych); cout << "\n\nThe 1st value should be A and it is " << copych; CopyNth (chqueue, 3, copych); cout << "\n\nThe 3rd value should be C and it is " << copych; // Dequeue 4 items, then Enqueue 3 more chqueue.Dequeue (copych); chqueue.Dequeue (copych); chqueue.Dequeue (copych); chqueue.Dequeue (copych); chqueue.Enqueue ('F'); chqueue.Enqueue ('G'); chqueue.Enqueue ('H'); // Do some CopyNth calls CopyNth (chqueue, 4, copych); cout << "\n\nThe 4th value should be H and it is " << copych; CopyNth (chqueue, 3, copych); cout << "\n\nThe 3rd value should be G and it is " << copych; CopyNth (chqueue, 2, copych); cout << "\n\nThe 2nd value should be F and it is " << copych; CopyNth (chqueue, 1, copych); cout << "\n\nThe 1st value should be E and it is " << copych; chqueue.Dequeue (copych); chqueue.Dequeue (copych); chqueue.Dequeue (copych); chqueue.Dequeue (copych); // Queue should now be empty if (chqueue.IsEmpty()) cout << "\n\nQueue is empty, as it should be\n\n"; else cout << "\n\nQueue isn't empty, but should be - uh-oh!\n\n"; return 0; }