一:棧模版(數組版):
stack.h
#ifndef MYSTACK_ARRAY_H
#define MYSTACK_ARRAY_H
#include <iostream>
using namespace std;
template<class T>
class MyStack
{
public:
MyStack(int capacity) :
m_capacity(capacity),
m_top(0)
{
m_element = new T[capacity];
}
~MyStack()
{
delete []m_element;
}
void push(T value); //入棧
T pop(); //出棧
T top(); //獲取棧頂元素
int capacity(); //獲取容量
int counts(); //獲取棧內元素個數
bool isEmpty(); //判斷棧是否爲空
void print(); //打印棧
private:
T *m_element;
int m_top;
int m_capacity;
};
template<class T>
void MyStack<T>::push(T value)
{
if(m_top >= m_capacity)
{
cout << "push failed, the stack is full." << endl;
}
else
{
m_element[m_top++] = value; //如何實現new空間的動態增長?
}
}
template<class T>
T MyStack<T>::pop()
{
if(0 == m_top)
{
cout << "pop failed, the stack is empty." << endl;
T value;
return value;
}
return m_element[--m_top];
}
template<class T>
T MyStack<T>::top()
{
if(0 == m_top)
{
cout << "top failed, the stack is empty." << endl;
T value;
return value;
}
return m_element[m_top-1];
}
template<class T>
int MyStack<T>::capacity()
{
return m_capacity;
}
template<class T>
int MyStack<T>::counts()
{
return m_top;
}
template<class T>
bool MyStack<T>::isEmpty()
{
return m_top;
}
template<class T>
void MyStack<T>::print()
{
if(0 == m_top)
{
cout << "print failed, the stack is empty." << endl;
}
else
{
for(int i = m_top-1; i >= 0; i--)
{
cout << m_element[i] << endl;
}
}
}
#endif // MYSTACK_ARRAY_H
main.cpp
#include "mystack_array.h"
int main()
{
MyStack<char> myStack(2);
myStack.push('a');
myStack.push('b');
myStack.push('c');
myStack.print();
myStack.pop();
myStack.pop();
myStack.push('e');
myStack.push('f');
myStack.print();
cout << myStack.top() << endl;
cout << myStack.counts() << endl;
cout << myStack.isEmpty() << endl;
return 0;
}
二:棧模版(單鏈表版):
stack.h
#ifndef MYSTACK_LIST_H
#define MYSTACK_LIST_H
#include <iostream>
using namespace std;
template<class T>
struct Node
{
Node() :
m_pNext(nullptr)
{
}
Node(T value) :
m_value(value), m_pNext(nullptr)
{
}
T m_value;
Node<T> *m_pNext;
};
template<class T>
class MyStack
{
public:
MyStack()
{
m_capacity = 0;
m_pHead = new Node<T>;
}
void push(T value); //入棧
T pop(); //出棧
T top(); //獲取棧頂元素
int counts(); //獲取棧內元素個數
bool isEmpty(); //判斷棧是否爲空
void print(); //打印棧
private:
int m_capacity; //容量
Node<T> *m_pHead;//棧頭
};
template<class T>
void MyStack<T>::push(T value)
{
Node<T> *pnode = new Node<T>(value);
pnode->m_pNext = m_pHead->m_pNext;//先將頭節點所指向的下一個節點(也就是最後的空節點)賦值給新建節點的下一個節點,使得新建節點的next指針指向最後節點
m_pHead->m_pNext = pnode;//然後將新建節點賦值給頭節點的next指針,使得頭結點的next指針指向新建節點
m_capacity++;
}
template<class T>
T MyStack<T>::pop()
{
if( nullptr == m_pHead->m_pNext )
{
cout << "pop failed, the stack is empty." << endl;
T value;
return value;
}
else
{
Node<T> *temp = m_pHead->m_pNext;//先獲取頭結點的下一個節點(即所需要出棧的節點)
m_pHead->m_pNext = m_pHead->m_pNext->m_pNext;//然後將出棧的節點的下一節點賦值給頭節點的next指針,即頭指針指向出棧節點的下一節點,這樣出棧節點就從鏈表中剔除了
T value = temp->m_value;
delete temp;
temp = NULL;
m_capacity--;
return value;
}
}
template<class T>
T MyStack<T>::top()
{
if( nullptr == m_pHead->m_pNext )
{
cout << "top failed, the stack is empty." << endl;
T value;
return value;
}
else
{
return m_pHead->m_pNext->m_value;
}
}
template<class T>
int MyStack<T>::counts()
{
return m_capacity;
}
template<class T>
bool MyStack<T>::isEmpty()
{
return m_capacity;
}
template<class T>
void MyStack<T>::print()
{
if( nullptr == m_pHead->m_pNext )
{
cout << "print failed, the stack is empty." << endl;
return;
}
else
{
Node<T> *temp = m_pHead;
while(temp->m_pNext != nullptr)
{
temp = temp->m_pNext;
cout << temp->m_value << endl;
}
}
}
#endif // MYSTACK_LIST_H
main.cpp
#include "mystack_list.h"
int main()
{
MyStack<char> myStack;
myStack.push('a');
myStack.push('b');
myStack.push('c');
myStack.print();
myStack.pop();
myStack.pop();
myStack.push('e');
myStack.push('f');
myStack.print();
cout << myStack.top() << endl;
cout << myStack.counts() << endl;
cout << myStack.isEmpty() << endl;
return 0;
}