-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path栈的链表实现.cpp
177 lines (175 loc) · 3.91 KB
/
栈的链表实现.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#define NIL NULL
using namespace std;
template<class T>
struct node {
T data;
node* next;
};
//所有下标均从1开始计数
template<class T>
class MyList {
protected:
int n;
node<T>* head;
node<T>* tail;
public:
MyList(){init();} //构造函数, 结点数初始化为0
void init() { //初始化链表
n = 0;
head = new node<T>;
head->next = NIL;
tail = head;
}
void push(T val) { //相当于push_back()
node<T>* p = new node<T>;
p->data = val;
p->next = NIL;
tail->next = p;
tail = p;
n++;
}
bool Insert(int pos,T val) { //插在指定位置处
if(pos>n) return false;
node<T>* p=new node<T>;
p->data=val;
node<T>* pr=prve(pos);
p->next=pr->next;
pr->next=p;
n++;
return true;
}
node<T>* prve(int pos) { //寻找前驱结点
pos--;
node<T>* p=head;
while(pos--) p=p->next;
return p;
}
bool Delete(int pos) { //删除结点
if(pos>n) return false;
node<T>* pr=prve(pos);
node<T>* p=head;
while(pos--) p=p->next;
pr->next=p->next;
delete p;
n--;
//下一句为stack专用语句
tail = pr;
return true;
}
bool Modify(int pos,T val) { //修改结点值
if(pos>n) return false;
node<T>* p=head;
while(pos--) p=p->next;
p->data=val;
return true;
}
int Find(T val) { //按值查找
int cnt=0;
node<T>* p=head->next;
while(p!=NIL) {
cnt++;
if( p->data==val ) {
return cnt;
}
p=p->next;
}
return -1;
}
T At(int pos) { //查询pos位置上的值
node<T>* p=head;
while(pos--) p=p->next;
return p->data;
}
void Display() { //展示链表内容
node<T>* p = head->next;
while(p!=NIL) {
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int how_many() {return n;} //查询结点数
~MyList() { //析构函数
node<T>* p=head;
node<T>* q;
while(p != NIL) {
q=p->next;
delete p;
p=q;
}
n = 0;
}
};
//不继承,而是组合
template<class T>
//栈顶指针比栈顶多1
class MyStack {
protected:
int MAXN;
MyList<T> v;
public:
MyStack():MAXN(10000){}
bool Push(T val) {
if(Size()==MAXN) return false;
v.push(val);
return true;
}
bool Pop() {
if(Size()==0) return false;
v.Delete(v.how_many());
return true;
}
T Top() {
return v.At(v.how_many());
}
bool is_Empty() {
return v.how_many() == 0;
}
bool is_Full() {
return v.how_many() == MAXN;
}
void make_Empty() {
v.~MyList();
v.init();
}
int Size(){return v.how_many();}
~MyStack(){}
};
int main() {
MyStack<int> v;
string op;
while(cin>>op) {
if(op=="pop") {
if(v.Pop()) {
cout<<"pop successfully"<<endl;
} else {
cout<<"error"<<endl;
}
} else if(op=="push") {
int x;
cin>>x;
if(v.Push(x)) {
cout<<"push successfully"<<endl;
} else {
cout<<"error"<<endl;
}
} else if(op=="top") {
cout<<v.Top()<<endl;
} else if(op=="empty?") {
if(v.is_Empty()) {
cout<<"empty"<<endl;
} else {
cout<<"not"<<endl;
}
} else if(op=="full?") {
if(v.is_Full()) {
cout<<"full"<<endl;
} else {
cout<<"not"<<endl;
}
}
}
v.make_Empty();
return 0;
}