Skip to content

Commit d69e3d1

Browse files
Create StackBasedOnLinkedList.cpp
1 parent 84802a4 commit d69e3d1

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
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+
}

0 commit comments

Comments
 (0)