Server IP : 172.16.15.8 / Your IP : 3.15.203.242 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/bafreeman/ |
[ 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 //---------------------------------------------------------------------- void CopyNth (QueueType<char>& aqueue, int N, char& copyitem) { QueueType<char> tempqueue; // temporary queue that will store items // from aqueue int i=0; // counter char otheritem; // item used to move items from one queue to the other if(!aqueue.IsEmpty()) { while(N!=i) // Search for Nth position and move items { // from aqueue to temporary queue until aqueue.Dequeue(copyitem); // position is found. tempqueue.Enqueue(copyitem); i++; } while(!aqueue.IsEmpty()) // continue to empty aqueue { aqueue.Dequeue(otheritem); tempqueue.Enqueue(otheritem); } while(!tempqueue.IsEmpty()) // move items from temprary queue back { // to aqueue tempqueue.Dequeue(otheritem); aqueue.Enqueue(otheritem); } } } //--------------------------- 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; }