Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.147.65.111
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/grpatillo/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/grpatillo/queuecopynthclient.cpp
/*      File Name:      queuecopynthclient.cpp
        Author:         R.P. Patillo
        Instructor:     Dr. Ames
        Due Date:       Oct. 23, 2008
        Complilation:   g++ queueclient.cpp -o queueclient.out
        Execution:      ./queuecopynthclient.out
*/

// 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;
    char tempitem;

    for(int i=0; i < N; i++)			// finds Nth item
    {
	aqueue.Dequeue(tempitem);
	tempQueue.Enqueue(tempitem);  
    }

    copyitem = tempitem;			// copys Nth item

    while(!aqueue.IsEmpty())			// fully deconstructs aqueue
    {
	aqueue.Dequeue(tempitem);
	tempQueue.Enqueue(tempitem);
    }

    while(!tempQueue.IsEmpty())			// properly constructs aqueue again
    {
	tempQueue.Dequeue(tempitem);
	aqueue.Enqueue(tempitem);
    }

}
  
//--------------------------- 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