Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.224.54.61
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/stackcopynthclient.cpp
/*      File Name:      stackcopynthclient.cpp
        Author:         R.P. Patillo
        Instructor:     Dr. Ames
        Due Date:       Oct. 23, 2008
        Complilation:   g++ stackcopynthclient.cpp -o stackcopynthclient.out
        Execution:      ./stackcopynthclient.out
*/

// 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> tempStack;
 
    for(int i = 0; i < N; i++)		// finds Nth item from building tempStack
     {
        tempStack.Push(astack.Top());
	astack.Pop();
    }

    copyitem = tempStack.Top();		// copys Nth item

    for(int i = 0; i < N; i++)		// properly rebuilds astack
    {
        astack.Push(tempStack.Top());
        tempStack.Pop();
    }
}

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