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

Current File : /home/bafreeman/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> tempstack;

	int index=0;  // counter 
	int index2=0; // counter

	char tempchar; // used to move item from one stack to the other
	char tempchar2;


	if(!astack.IsEmpty())
	{
		while(N!=index+1)                  // Search for Nth position while removing
		{                                  // top item from astack.  Move item to temporary
			tempchar=astack.Top();     // stack.
			astack.Pop();
			tempstack.Push(tempchar);
			index++;
		}
				
		copyitem=astack.Top(); // store item in Nth position
	
		while(index!=index2) // return values from temporary stack to astack
                {
                  tempchar2= tempstack.Top();
                  tempstack.Pop();
                  astack.Push(tempchar2);
                  index2++;
                }

	}
}

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