C++ stack class template implementation code

  • 2020-06-12 10:11:25
  • OfStack

Recently, I have been reviewing the data structure, which involves the implementation of the stack. The class template can make the storage data type of the stack more flexible. Here is the implementation code of the stack:


#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>
using namespace std;
 
template <typename T>
class MyStack
{
public:
 MyStack(int size);
 ~MyStack();
 bool stackEmpty();// Sentenced to empty 
 bool stackFull();// To the full 
 void clearStack();// empty 
 int stackLength();// The length of the 
 bool push(T elem);// The pressure of stack 
 bool pop(T &elem);// Out of the stack 
 bool stackTop(T &elem);// Return to the top 
 void stackTranverse();// Traversal stack 
 
private:
 T *m_pStack;// The stack pointer 
 int m_iSize;// Capacity of the stack 
 int m_iTop;// To the top of the stack 
};
 
template <typename T>
MyStack<T>::MyStack(int size)
{
 m_iSize = size;
 m_pStack = new T[m_iSize];
 m_iTop = 0;
}
template <typename T>
MyStack<T>::~MyStack()
{
 delete m_pStack;
 m_pStack = NULL;
}
template <typename T>
bool MyStack<T>::stackEmpty() {// Sentenced to empty 
 return m_iTop == 0 ? true : false;
}
template <typename T>
bool MyStack<T>::stackFull() {// To the full 
 return m_iTop == m_iSize ? true : false;
}
template <typename T>
int MyStack<T>::stackLength() {// The length of the stack 
 return m_iTop;
}
template <typename T>
void MyStack<T>::clearStack() {// empty 
 m_iTop = 0;
}
template <typename T>
bool MyStack<T>::push(T elem) {// The pressure of stack 
 if (stackFull()) {
 return false;
 }
 else {
 m_pStack[m_iTop++] = elem;
 return true;
 }
}
template <typename T>
bool MyStack<T>::pop(T &elem) {// Out of the stack 
 if (stackEmpty())
 {
 return false;
 }
 else {
 elem = m_pStack[--m_iTop];
 return true;
 }
}
template <typename T>
bool MyStack<T>::stackTop(T &elem) {// Returns the top element of the stack 
 if (stackEmpty())
 {
 return false;
 }
 else {
 elem = m_pStack[m_iTop-1];
 return true;
 }
}
template <typename T>
void MyStack<T>::stackTranverse() {// Traversal stack 
 int i = 0;
 for (i = 0; i < m_iTop; i++) {
 cout << m_pStack[i];
 }
}
#endif

One thing to note is that the class template needs to write the template definition template before each function < typename T > , and write the class name as MyStack < T > , use T instead of T.

Then I used a coordinate point class Coordinate for the test:

Use the function overload operator in the Coordinate class < < Realize the printing of coordinate points


#include <ostream>
using namespace std;
 
class Coordinate
{
public:
 friend ostream& operator<<(ostream &out, Coordinate &coor);
 Coordinate(int x=0,int y=0)
 {
 m_iX = x;
 m_iY = y;
 }
 
 ~Coordinate()
 {
 }
private:
 int m_iX;
 int m_iY;
};
ostream& operator<<(ostream &out, Coordinate &coor) {
 out << "(" << coor.m_iX << "," << coor.m_iX << ")" << endl;
 return out;
}

Here is the test main function:


#include <iostream>
#include "MyStack.h"
#include "Coordinate.h"
using namespace std;
 
int main() {
 MyStack<Coordinate> *pStack = new MyStack<Coordinate>(5);
 
  pStack->push(Coordinate(3, 5));// Coordinate point push 
 pStack->push(Coordinate(7, 5));
 pStack->push(Coordinate(6, 5));
 pStack->push(Coordinate(4, 5));
 pStack->push(Coordinate(3, 5));
 
 pStack->stackTranverse();// Traversal stack 
 Coordinate t;
 pStack->pop(t);// Out of the stack 
 cout <<" A pop-up t To: "<< t ;
 cout << " Length: " << pStack->stackLength();
 pStack->clearStack();// Empty stack 
 pStack->stackTranverse();
 
  //delete pStack;
 //pStack = NULL;
 
 system("pause");
 return 0;
}

Related articles: