SoFunction
Updated on 2025-04-06

C++ stack template implementation code

Recently, I am 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. The following 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();//Notification bool stackFull();//The full sentence void clearStack();//Clear int stackLength();//length bool push(T elem);//Press up the stack bool pop(T &elem);//Open the stack bool stackTop(T &elem);//Return to the top of the stack void stackTranverse();//Transfer the stack 
private:
 T *m_pStack;//Stack pointer int m_iSize;//Stack capacity int m_iTop;//Top of 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() {//Notification return m_iTop == 0 ? true : false;
}
template <typename T>
bool MyStack<T>::stackFull() {//The full sentence return m_iTop == m_iSize ? true : false;
}
template <typename T>
int MyStack<T>::stackLength() {//Stack length return m_iTop;
}
template <typename T>
void MyStack<T>::clearStack() {//Clear m_iTop = 0;
}
template <typename T>
bool MyStack<T>::push(T elem) {//Press up the stack if (stackFull()) {
 return false;
 }
 else {
 m_pStack[m_iTop++] = elem;
 return true;
 }
}
template <typename T>
bool MyStack<T>::pop(T &elem) {//Open the stack if (stackEmpty())
 {
 return false;
 }
 else {
 elem = m_pStack[--m_iTop];
 return true;
 }
}
template <typename T>
bool MyStack<T>::stackTop(T &elem) {//Return to 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() {//Transfer the stack int i = 0;
 for (i = 0; i < m_iTop; i++) {
 cout << m_pStack[i];
 }
}
#endif

It should be noted that the class template needs to be written with template definition template <typename T> before each function, and the class name is MyStack<T>, and T is used instead when the function involves the use of classes.

Then I used a coordinate point class Coordinate to do the test:

Use function overload operator << in Coordinate class to implement 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 main test function:

#include &lt;iostream&gt;
#include ""
#include ""
using namespace std;
 
int main() {
 MyStack&lt;Coordinate&gt; *pStack = new MyStack&lt;Coordinate&gt;(5);
 
 pStack-&gt;push(Coordinate(3, 5));//Coordinate points are entered into the stack pStack-&gt;push(Coordinate(7, 5));
 pStack-&gt;push(Coordinate(6, 5));
 pStack-&gt;push(Coordinate(4, 5));
 pStack-&gt;push(Coordinate(3, 5));
 
 pStack-&gt;stackTranverse();//Transfer the stack Coordinate t;
 pStack-&gt;pop(t);//Open the stack cout &lt;&lt;"The pop-up t is:"&lt;&lt; t ;
 cout &lt;&lt; "length:" &lt;&lt; pStack-&gt;stackLength();
 pStack-&gt;clearStack();//Clear the stack pStack-&gt;stackTranverse();
 
 //delete pStack;
 //pStack = NULL;
 
 system("pause");
 return 0;
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.