From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/Stack/Tst_test.cpp | 50 ++++++++++++++++++++++++++++++++++++ Bachelor/Prog2/Stack/Tstack1.h | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Bachelor/Prog2/Stack/Tst_test.cpp create mode 100644 Bachelor/Prog2/Stack/Tstack1.h (limited to 'Bachelor/Prog2/Stack') diff --git a/Bachelor/Prog2/Stack/Tst_test.cpp b/Bachelor/Prog2/Stack/Tst_test.cpp new file mode 100644 index 0000000..5e9e9f8 --- /dev/null +++ b/Bachelor/Prog2/Stack/Tst_test.cpp @@ -0,0 +1,50 @@ +// Test driver for Stack template. +// Function main uses a function template to manipulate objects of type Stack< T >. +// Modification: use non-type parameter 'elements' in Stack-template + +#include +using std::cout; +using std::cin; +using std::endl; + +#include +using std::string; + +#include "tstack1.h" + +// Function template to manipulate Stack< T > +template< class T, int elements > +void testStack( + Stack< T, elements > &theStack, // reference to the Stack< T > + T value, // initial value to be pushed + T increment, // increment for subsequent values + const char *stackName ) // name of the Stack < T > object +{ + cout << "\nPushing elements onto " << stackName << '\n'; + + while ( theStack.push( value ) ) { // success true returned + cout << value << ' '; + value += increment; + } + + cout << "\nStack is full. Cannot push " << value + << "\n\nPopping elements from " << stackName << '\n'; + + while ( theStack.pop( value ) ) // success true returned + cout << value << ' '; + + cout << "\nStack is empty. Cannot pop\n"; +} + +int main() +{ + Stack< double, 5 > doubleStack; + Stack< int, 10 > intStack; + Stack< string, 5 > stringStack; + + testStack( doubleStack, 1.1, 1.1, "doubleStack" ); + testStack( intStack, 1, 1, "intStack" ); + testStack( stringStack, string("Eins"), string("UndEins"), "stringStack" ); + + return 0; +} \ No newline at end of file diff --git a/Bachelor/Prog2/Stack/Tstack1.h b/Bachelor/Prog2/Stack/Tstack1.h new file mode 100644 index 0000000..8cbc5d5 --- /dev/null +++ b/Bachelor/Prog2/Stack/Tstack1.h @@ -0,0 +1,53 @@ +// tstack1.h +// Class template Stack + +#ifndef TSTACK1_H +#define TSTACK1_H + +template< class T, int elements > // # of elements in the stack +class Stack { +public: + Stack(); // default constructor + // ~Stack() { delete [] stackPtr; } // destructor (only for dynamic version) + bool push( const T& ); // push an element onto the stack + bool pop( T& ); // pop an element off the stack +private: + int top; // location of the top element + T stackHolder[elements]; // static array for stack + // T *stackPtr; // pointer to the stack (only for dynamic version) + bool isEmpty() const { return top == -1; } // utility + bool isFull() const { return top == elements - 1; } // functions +}; + +// Constructor with default size 10 +template< class T, int elements > +Stack< T, elements>::Stack() +{ + top = -1; // Stack is initially empty + // stackPtr = new T[ elements ]; // dynamically allocate space for elements (not used) +} + +// Push an element onto the stack +// return 1 if successful, 0 otherwise +template< class T, int elements > +bool Stack< T, elements >::push( const T &pushValue ) +{ + if ( !isFull() ) { + stackHolder[ ++top ] = pushValue; // place item in Stack + return true; // push successful + } + return false; // push unsuccessful +} + +// Pop an element off the stack +template< class T, int elements > +bool Stack< T, elements >::pop( T &popValue ) +{ + if ( !isEmpty() ) { + popValue = stackHolder[ top-- ]; // remove item from Stack + return true; // pop successful + } + return false; // pop unsuccessful +} + +#endif \ No newline at end of file -- cgit v1.2.3