# Encapsulates a Stack
In this blog, I will try to encapsulate a stack.
# Definitions
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main principal operations:
- push, which adds an element to the collection, and
- pop, which removes the most recently added element that was not yet removed.
![]()
# Function
This stack contains these functions:
- Getting the size of the stack.
- Checking the stack whether empty or not.
- Inserting element in anywhere in the stack.
- Deleting element in anywhere in the stack.
- Getting element in anywhere in the stack.
- Showing the whole stack.
- Clearing the whole stack.
# Method
I define a class named stack. In the class, it has a private class about DoubleLink , properties,and interface.
# DoubleLink
The object of this class provide some interface to support the function of stack data structure.
let us look the code:
1 2
| private: DoubleLink<T> *link;
|
# Interface
I define these interfaces to realize the functions of the stack;
1 2 3 4 5 6 7 8 9 10
| public: stack(); ~stack(); int size(); void push(T x); void pop(); T top(); bool IsEmpty(); void clear(); void show();
|
# Constructor
Through the constructor, the object will difine a DoubleLink object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| template<class T> stack<T>::stack() { link=new DoubleLink<T>(); } ```
###### Destructor Through the destructor ,we delete the Link. ```C++ template<class T> stack<T>::~stack() { delete link; }
|
# size()
We can get the number of elements in the stack
1 2 3 4 5
| template<class T> int stack<T>::size() { return link->size(); }
|
# IsEmpty()
We can check the stack whether empty or not
1 2 3 4 5
| template<class T> bool stack<T>::IsEmpty() { return link->IsEmpty(); }
|
# push()
We use this function to insert the element to the stack top.
1 2 3 4 5
| template<class T> void stack<T>::push(T x) { link->pushBack(x); }
|
# top()
This function can return data of elements.
1 2 3 4
| T stack<T>::top() { return link->getBack(); }
|
# pop()
These functions can delete elements in stack.
1 2 3 4 5
| template<class T> void stack<T>::pop() { link->popBack(); }
|
# show()
Showing the whole stack.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| template<class T> void stack<T>::show() { link->show(); } ```
###### clear() Remove the stack. ```C++ template<class T> void stack<T>::clear() { link->clear(); }
|
# The Whole Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include <iostream> #include "DoubleLink.h" using namespace std; template<class T> class stack{ private: DoubleLink<T> *link; public: stack(); ~stack(); int size(); void push(T x); void pop(); T top(); bool IsEmpty(); void clear(); void show(); }; template<class T> stack<T>::stack() { link=new DoubleLink<T>(); } template<class T> stack<T>::~stack() { delete link; }
template<class T> int stack<T>::size() { return link->size(); } template<class T> void stack<T>::push(T x) { link->pushBack(x); } template<class T> T stack<T>::top() { return link->getBack(); } template<class T> void stack<T>::pop() { link->popBack(); } template<class T> bool stack<T>::IsEmpty() { return link->IsEmpty(); } template<class T> void stack<T>::clear() { link->clear(); } template<class T> void stack<T>::show() { link->show(); }
int main() { int n; stack<int>* z=new stack<int>(); cout<<"判断是否为空"<<endl; if(z->IsEmpty()) { cout<<"空栈"<<endl; } else { cout<<"非空"<<endl; } cout<<"push元素"<<endl; z->push(5); z->push(7); z->push(6); z->push(4); z->push(3); z->push(9); cout<<"判断是否为空"<<endl; if(z->IsEmpty()) { cout<<"空栈"<<endl; } else { cout<<"非空"<<endl; } cout<<"展示栈的元素"<<endl; z->show(); cout<<"获取栈顶元素"<<endl; cout<<z->top()<<endl; cout<<"获取栈大小"<<endl; cout<<z->size()<<endl; cout<<"删除栈顶元素"<<endl; z->pop(); z->show(); cout<<"删除栈顶元素"<<endl; z->pop(); z->show(); cout<<"插入栈顶元素"<<endl; cout<<"输入元素"<<endl; cin>>n; z->push(n); z->show(); cout<<"清空栈"<<endl; z->clear(); cout<<"判断是否为空"<<endl; if(z->IsEmpty()) { cout<<"空栈"<<endl; } else { cout<<"非空"<<endl; } return 0; }
|