Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.190.253.56
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/stackcopynthclient.cpp
// Test Driver for Stack CopyNth as a client function

#include <iostream>
#include "stackarray.h"  // to test with array implementation
//#include "stackll.h"     // to test with linked implementation
using namespace std;

//---------------------------------------------------------------------
// CopyNth shows copy of Nth item from top of stack, top being item #1
//---------------------------------------------------------------------
// Accepts:  reference to stack to look at
//           N - logical position of desired item
// Returns:  copy of item at Nth position from top
//---------------------------------------------------------------------

void CopyNth (StackType<char>& astack, int N, char& copyitem)
{
    StackType<char> tmpstack;	// temp stack to put top chars in 
    char tch1;			 
    char tch2;	
 
    for( int i = 0; i < (N - 1); i ++)
    {
        tch1 = astack.Top();   // copies the top item in the stack
        astack.Pop();	       // deletes the item
        tmpstack.Push(tch1);   // pushes the tmp char on to the 
			       // temp stack
    }

    copyitem = astack.Top();   // copies the top item in the stack
 
    for( int j = 0; j < (N - 1); j ++)
    {
        tch2 = tmpstack.Top(); // copies the top item in the temp stack
        tmpstack.Pop();        // removes the top item in the temp stack
        astack.Push(tch2);     // pushes the item back on the the 
                               // original stack
    }

}

//------------------------ Main function ------------------------------

int main ()
{

StackType<char> chstack;  // test stack

char copych;              // holds character viewed by CopyNth

// Push some items

chstack.Push ('A');
chstack.Push ('B');
chstack.Push ('C');
chstack.Push ('D');
chstack.Push ('E');
chstack.Push ('F');

// Do some CopyNth calls - stack has 6 values

cout << "\nTesting CopyNth for stack with 6 items";

CopyNth (chstack, 6, copych);
cout << "\n\nThe 6th value should be A and it is " << copych;

CopyNth (chstack, 1, copych);
cout << "\n\nThe 1st value should be F and it is " << copych;

CopyNth (chstack, 3, copych);
cout << "\n\nThe 3rd value should be D and it is " << copych;

//  Pop until stack has 4 values

chstack.Pop ();
chstack.Pop ();

//  Do some CopyNth calls

CopyNth (chstack, 4, copych);
cout << "\n\nThe 4th value should be A and it is " << copych;

CopyNth (chstack, 3, copych);
cout << "\n\nThe 3rd value should be B and it is " << copych;

CopyNth (chstack, 2, copych);
cout << "\n\nThe 2nd value should be C and it is " << copych;

CopyNth (chstack, 1, copych);
cout << "\n\nThe 1st value should be D and it is " << copych;

chstack.Pop ();
chstack.Pop ();
chstack.Pop ();
chstack.Pop ();

// Stack should now be empty

if (chstack.IsEmpty())
   cout << "\n\nStack is empty, as it should be\n\n";
else
   cout << "\n\nStack isn't empty, but should be - uh-oh!\n\n";

return 0;

}


Stv3n404 - 2023