Skip to content

Commit fa29f55

Browse files
Merge pull request wangzheng0822#57 from mvpcaozixiang/master
顺序栈与链式栈CPP实现。
2 parents 9b4dba5 + d69e3d1 commit fa29f55

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// 类模板的声明(line 3),类模板实例化后就是模板类
3+
// 类模板声明的写法 template <class T> class 类名{}
4+
template <class T> class ArrayStack
5+
{
6+
public:
7+
ArrayStack();
8+
ArrayStack(int count);
9+
~ArrayStack();
10+
void push(T data); //入栈
11+
T pop(); //出栈,并删除栈顶元素
12+
T peek(); //返回栈顶元素,不删除栈顶元素,栈顶指针不变
13+
int stackSize();
14+
int stackMaxSize();
15+
16+
private:
17+
int flag; //栈顶标签,指向栈顶
18+
int count ; //栈的容量
19+
T *array; //指针
20+
};
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* 1)链式栈的操作:入栈,出栈以及返回栈的大小;
3+
* 2)采用模板的方法实现存储任意类型的数据
4+
* 3)采用单链表实现栈
5+
* 4)pop和peek 出栈的返回值稍微有点问题,当栈为空的时候,返回null,cpp默认返回的是0。
6+
* * 改进方法就是不用函数的返回值返回栈顶元素,而是采用参数列表的形式返回,这样稍微有点麻烦
7+
* * 或者就是在使用的时候先调用size函数判断以下
8+
* Author:caozx
9+
* time ;2018年10月11日
10+
*/
11+
12+
#include <iostream>
13+
#include "StackBasedOnLinkedList.h"
14+
using namespace std;
15+
16+
template<class T> LinkedListStack<T>::LinkedListStack()
17+
{
18+
this -> count = 0;
19+
this -> head = new LinkedNode;
20+
this -> head -> next = NULL;
21+
}
22+
23+
template<class T> LinkedListStack<T>::~LinkedListStack()
24+
{
25+
LinkedNode * ptr, * temp;
26+
ptr = head;
27+
while(ptr -> next != NULL){
28+
temp = ptr -> next;
29+
ptr -> next = temp -> next;
30+
delete temp;
31+
}
32+
delete head ; //删除头节点
33+
this -> head = NULL;
34+
this -> count = 0;
35+
}
36+
37+
// 入栈
38+
template<class T> void LinkedListStack<T>::push(const T & data)
39+
{
40+
LinkedNode * insertPtr = new LinkedNode;
41+
insertPtr -> data = data;
42+
insertPtr -> next = this -> head -> next;
43+
head -> next = insertPtr;
44+
this -> count ++;
45+
cout << "push data : " << this -> head -> next -> data << endl;
46+
}
47+
48+
//返回栈顶元素,即出栈,不删除栈顶元素
49+
template<class T> T LinkedListStack<T>::peek()
50+
{
51+
if(this -> count == 0 || this -> head -> next == NULL){
52+
cout << " stack is empty, peek fail"<< endl;
53+
return NULL;
54+
}
55+
else{
56+
return this -> head -> next -> data;
57+
}
58+
}
59+
60+
//出栈,删除栈顶元素
61+
template<class T> T LinkedListStack<T>::pop()
62+
{
63+
if(this -> count == 0 || this -> head -> next == NULL){
64+
cout << " stack is empty, pop fail"<< endl;
65+
return NULL;
66+
}
67+
else{
68+
LinkedNode * temp = this -> head -> next;
69+
this -> head -> next = temp -> next;
70+
T data = temp -> data;
71+
delete temp;
72+
this -> count --;
73+
return data;
74+
}
75+
76+
}
77+
78+
//返回栈的大小
79+
template<class T> int LinkedListStack<T>::size() const
80+
{
81+
return this -> count;
82+
}
83+
84+
int main(int argc, char const *argv[])
85+
{
86+
cout << " === StackBasedOnLinkedList test begin ===" << endl;
87+
LinkedListStack <float> stack;
88+
cout << "size==="<<stack.size()<<endl;
89+
stack.push(10.1);
90+
stack.push(20.2);
91+
stack.push(30.);
92+
stack.push(40.4);
93+
stack.push(50.5);
94+
stack.push(60.6);
95+
cout << "size==="<<stack.size()<<endl;
96+
cout << "stack peek " << stack.peek() << endl;
97+
cout << "stack pop " << stack.pop() << endl;
98+
cout << "size==="<<stack.size()<<endl;
99+
cout << "stack pop " << stack.pop() << endl;
100+
cout << "stack pop " << stack.pop() << endl;
101+
cout << "stack pop " << stack.pop() << endl;
102+
cout << "stack pop " << stack.pop() << endl;
103+
cout << "stack pop " << stack.pop() << endl;
104+
cout << "size==="<<stack.size()<<endl;
105+
cout << "stack pop " << stack.pop() << endl;
106+
cout << "stack peek " << stack.peek() << endl;
107+
stack.push(110.1);
108+
stack.push(120.2);
109+
stack.push(130.3);
110+
stack.push(140.4);
111+
stack.push(150.5);
112+
stack.push(160.6);
113+
cout << "size==="<<stack.size()<<endl;
114+
cout << "stack peek " << stack.peek() << endl;
115+
cout << "stack pop " << stack.pop() << endl;
116+
cout << "stack pop " << stack.pop() << endl;
117+
cout << "stack pop " << stack.pop() << endl;
118+
cout << "stack peek " << stack.peek() << endl; //peek
119+
cout << "stack pop " << stack.pop() << endl;
120+
cout << "stack pop " << stack.pop() << endl;
121+
cout << "stack pop " << stack.pop() << endl;
122+
cout << "size==="<<stack.size()<<endl;
123+
cout << "stack peek " << stack.peek() << endl; //peek
124+
cout << "stack pop " << stack.pop() << endl;
125+
system("pause");
126+
return 0;
127+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 类模板的声明,关键字 class 也可以更换成 typename
2+
template<class T> class LinkedListStack
3+
{
4+
public:
5+
LinkedListStack();
6+
~LinkedListStack();
7+
8+
void push(const T & data); //入栈
9+
T peek(); //返回栈顶元素,即出栈,不删除栈顶元素
10+
T pop(); //出栈,删除栈顶元素
11+
int size() const; //返回栈的大小
12+
private:
13+
int count; //存放栈的大小,因为是单链表所以这里不规定栈的最大可承载量
14+
struct LinkedNode
15+
{
16+
T data;
17+
LinkedNode * next;
18+
};
19+
LinkedNode * head; // 单链表的头指针,不带头节点
20+
};

0 commit comments

Comments
 (0)