____________________________________
First: Header file.h (functions prototype).
____________________________________
// File: Stackt.h
// Stack template class definition.
// Dynamic array implementation
#ifndef STACKT_H
#define STACKT_H
template <class Type>
class Stackt
{
public:
Stackt(int nelements = 128); // Constructor
Stackt (const Stackt<Type> &); // Copy Constructor
~Stackt(); // Destructor
// Member Functions
void push(Type ); // Push
void pop(Type &); // Pop
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
bool stackIsFull() const; // Test for Full stack
private:
Type *stack; // pointer to dynamic array
int top, MaxSize;
};
#endif // STACKT_H
#include "Stackt.cpp"
________________________________________________
// Stack template class definition.
// Dynamic array implementation
#ifndef STACKT_H
#define STACKT_H
template <class Type>
class Stackt
{
public:
Stackt(int nelements = 128); // Constructor
Stackt (const Stackt<Type> &); // Copy Constructor
~Stackt(); // Destructor
// Member Functions
void push(Type ); // Push
void pop(Type &); // Pop
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
bool stackIsFull() const; // Test for Full stack
private:
Type *stack; // pointer to dynamic array
int top, MaxSize;
};
#endif // STACKT_H
#include "Stackt.cpp"
________________________________________________
second: Implementation file.cpp
_____________________________________
// File: Stackt.cpp
// Stack template class implementation
#include <iostream>
using namespace std;
// Constructor with argument, size is nelements, default is 128
template <class Type>
Stackt<Type>::Stackt(int nelements)
{ MaxSize = nelements; stack = new Type[MaxSize]; top = -1; }
// Copy Constructor
template <class Type>
Stackt <Type>::Stackt(const Stackt<Type> &original)
:MaxSize(original.MaxSize), top(original.top)
{
stack = new Type[MaxSize];
for (int i = 0; i <= top; i++) stack[i] = original.stack[i];
}
// Destructor
template <class Type>
Stackt<Type>::~Stackt()
{ delete [] stack;}
// Push
template <class Type>
void Stackt<Type>::push(Type v)
{
if(stackIsFull()) cout << "Stack Overflow" << endl;
else stack[++top] = v;
}
// Pop
template <class Type>
void Stackt<Type>::pop(Type &v)
{
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else v = stack[top--];
}
// Retrieve stack top without removing it
template <class Type>
void Stackt<Type>::stackTop(Type &v) const
{
if(stackIsEmpty()) cout << "Stack Underflow";
else v = stack[top];
}
// Test for Empty stack
template <class Type>
bool Stackt<Type>::stackIsEmpty() const
{ return (top < 0); }
// Test for Full stack
template <class Type>
bool Stackt<Type>::stackIsFull() const
{ return (top >= (MaxSize-1)); }
________________________________________________
// Stack template class implementation
#include <iostream>
using namespace std;
// Constructor with argument, size is nelements, default is 128
template <class Type>
Stackt<Type>::Stackt(int nelements)
{ MaxSize = nelements; stack = new Type[MaxSize]; top = -1; }
// Copy Constructor
template <class Type>
Stackt <Type>::Stackt(const Stackt<Type> &original)
:MaxSize(original.MaxSize), top(original.top)
{
stack = new Type[MaxSize];
for (int i = 0; i <= top; i++) stack[i] = original.stack[i];
}
// Destructor
template <class Type>
Stackt<Type>::~Stackt()
{ delete [] stack;}
// Push
template <class Type>
void Stackt<Type>::push(Type v)
{
if(stackIsFull()) cout << "Stack Overflow" << endl;
else stack[++top] = v;
}
// Pop
template <class Type>
void Stackt<Type>::pop(Type &v)
{
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else v = stack[top--];
}
// Retrieve stack top without removing it
template <class Type>
void Stackt<Type>::stackTop(Type &v) const
{
if(stackIsEmpty()) cout << "Stack Underflow";
else v = stack[top];
}
// Test for Empty stack
template <class Type>
bool Stackt<Type>::stackIsEmpty() const
{ return (top < 0); }
// Test for Full stack
template <class Type>
bool Stackt<Type>::stackIsFull() const
{ return (top >= (MaxSize-1)); }
________________________________________________
finally class test
_____________________________________
// File: StacktAppl.cpp
// Applies Stack template Class to reverse a string
#include <iostream>
#include <string>
using namespace std;
#include "Stackt.h"
int main() // Testing he Stackt Class
{ // Reverse a string and stack copy
Stackt<char> s1;
char c;
string instring = "Testing Stack Class";
string outstring = ""; cout << instring << endl;
int L = instring.length();
cout << "Pushing characters on s1\n";
for (int i = 0; i < L; i++) s1.push(instring.at(i));
cout << "Copying s1 to s2\n";
Stackt<char> s2 = s1;
cout << "Popping characters from s1\n";
while(!s1.stackIsEmpty())
{ s1.pop(c); outstring = outstring + c;}
cout << outstring << endl;
cout <<"s1 is now empty. Trying to pop from empty s1\n";
s1.pop(c);
cout << "Now popping contents of s2" << endl;
while(!s2.stackIsEmpty())
{ s2.pop(c); cout << c; }
cout<< endl;
return 0;
}
// Applies Stack template Class to reverse a string
#include <iostream>
#include <string>
using namespace std;
#include "Stackt.h"
int main() // Testing he Stackt Class
{ // Reverse a string and stack copy
Stackt<char> s1;
char c;
string instring = "Testing Stack Class";
string outstring = ""; cout << instring << endl;
int L = instring.length();
cout << "Pushing characters on s1\n";
for (int i = 0; i < L; i++) s1.push(instring.at(i));
cout << "Copying s1 to s2\n";
Stackt<char> s2 = s1;
cout << "Popping characters from s1\n";
while(!s1.stackIsEmpty())
{ s1.pop(c); outstring = outstring + c;}
cout << outstring << endl;
cout <<"s1 is now empty. Trying to pop from empty s1\n";
s1.pop(c);
cout << "Now popping contents of s2" << endl;
while(!s2.stackIsEmpty())
{ s2.pop(c); cout << c; }
cout<< endl;
return 0;
}
No comments:
Post a Comment