Skip to content

Commit 0d89d2b

Browse files
Create StackBasedOnArray.cpp
1 parent 1e0e0e6 commit 0d89d2b

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* 1)顺序栈的操作:入栈和出栈;
3+
* 2)采用模板的方法实现存储任意类型的数据
4+
* 3)采用数组的栈,支持动态扩容,每次扩容1.5 倍,初始栈的大小是 10 。
5+
*
6+
* Author:caozx
7+
* time ;2018年10月11日
8+
*/
9+
10+
#include <iostream>
11+
#include "StackBasedOnArray.h"
12+
using namespace std;
13+
14+
//构造函数,创建栈
15+
//类模板成员函数的写法 template<class T> 返回值类型 类名<T>::成员函数名(参数列表){}
16+
template<class T> ArrayStack<T>::ArrayStack()
17+
{
18+
this -> count = 10;
19+
this -> flag = 0;
20+
this -> array = new T[this -> count];
21+
if (! this -> array){
22+
cout << "array malloc memory failure" << endl;
23+
}
24+
}
25+
26+
27+
//有参构造函数,创建栈
28+
template<class T> ArrayStack<T>::ArrayStack(int count)
29+
{
30+
this -> count = count;
31+
this -> flag = 0;
32+
this -> array = new T[this -> count];
33+
if (! this -> array){
34+
cout << "array malloc memory failure" << endl;
35+
}
36+
}
37+
38+
//析构函数,销毁栈
39+
template <class T> ArrayStack<T>::~ArrayStack(){
40+
this -> count = 0;
41+
this -> flag = 0;
42+
if(this -> array){
43+
delete [] this -> array;
44+
this -> array = NULL;
45+
}
46+
47+
}
48+
49+
// 入栈
50+
template<class T> void ArrayStack<T>::push(T data){
51+
if(this -> flag == this -> count){
52+
cout << "The stack is full , so need to enlarge 1.5x! "<< endl;
53+
this -> count = int (1.5 * this -> count);
54+
T * temp = new T [this -> count];
55+
for(int i = 0; i < this -> flag ; i++){
56+
temp[i] = this -> array[i];
57+
//cout << temp[i] <<endl;
58+
}
59+
delete [] this -> array; //释放原来的空间
60+
temp[this -> flag] = data;
61+
this -> flag ++;
62+
this -> array = temp;
63+
}
64+
else{
65+
this -> array [this -> flag] = data;
66+
this -> flag ++ ;
67+
}
68+
}
69+
70+
//出栈,并删除栈顶元素
71+
template<class T> T ArrayStack<T>::pop(){
72+
this -> flag --;
73+
T temp = this -> array[this -> flag];
74+
return temp;
75+
}
76+
77+
//出栈,不删除栈顶元素
78+
template<class T> T ArrayStack<T>::peek(){
79+
T temp = this -> array[this -> flag - 1];
80+
return temp;
81+
}
82+
83+
template<class T> int ArrayStack<T>::stackSize(){
84+
return this -> flag;
85+
}
86+
87+
template<class T> int ArrayStack<T>::stackMaxSize(){
88+
return this -> count;
89+
}
90+
91+
int main(int argc, char const *argv[])
92+
{
93+
cout << " === test begin ===" << endl;
94+
ArrayStack <int> arrstack(12);
95+
arrstack.push(10);
96+
arrstack.push(20);
97+
arrstack.push(30);
98+
arrstack.push(40);
99+
arrstack.push(50);
100+
arrstack.push(60);
101+
arrstack.push(70);
102+
arrstack.push(80);
103+
arrstack.push(90);
104+
arrstack.push(100);
105+
arrstack.push(110);
106+
arrstack.push(120);
107+
arrstack.push(130);
108+
arrstack.push(140);
109+
arrstack.push(150);
110+
111+
cout << "peek , not delete " << arrstack.peek() << endl;
112+
cout << "pop , delete " << arrstack.pop()<<endl;
113+
114+
arrstack.push(210);
115+
arrstack.push(220);
116+
117+
cout << "peek , not delete " << arrstack.peek() << endl;
118+
cout << "pop , delete " << arrstack.pop()<<endl;
119+
120+
system("pause");
121+
return 0;
122+
}

0 commit comments

Comments
 (0)