栈操作
- push()
- pop()
- top()
- empty()
- size()
顺便温习下 模版template, 结构体struct, 链表linked list, 指针pointer.(这是来搞翻译的么…)
贴码:
- 还非得整两个文件,两个文件在一个文件夹中即可,可以g++手动编译下
g++ -o stack main.cpp
- 合并到一个文件中也行。
文件stack.h
// stack.h
#ifndef STACK_H
#define STACK_H
#include <ostream>
using namespace std;
template <class T>
class Stack{
public:
Stack();
Stack(int max);
~Stack();
void Push(T const &x);
void Pop();
const T Top() const;
size_t Size() const { return count; }
bool IsEmpty() const { return count==0; }
private:
struct Node {
T data;
Node *next;
};
typedef Node node; //类型别名
node *top;
size_t count;
int max;
};
template <class T>
Stack<T>::Stack():top(nullptr), count(0), max(-1){
cout << "init, come on!" << endl;
cout << "at the first, count = " << count << endl;
}
template <class T>
Stack<T>::Stack(int max):top(nullptr), count(0) {
this->max = max;
cout << "max = " << max << endl;
}
template <class T>
Stack<T>::~Stack() {
node *current = top;
while (current != nullptr) {
node *next = current->next;
delete current;
current = next;
}
top = nullptr;
}
template <class T>
void Stack<T>::Push(const T &x) { //is equal: T const &x
if (count == max) {
cout << "Stack is full. Can't push." << endl;
return void();
}
node *newTop = new node;
newTop->data = x;
if (top == nullptr) {
newTop->next = nullptr;
}
else {
newTop->next =top;
}
top = newTop;
count++;
cout << "push data: " << top->data << endl;
}
template <class T>
void Stack<T>::Pop() {
if (top == nullptr || count == 0) {
cout << "There have no data. Pop fail." << endl;
return void();
}
node *delNode = top;
top = top->next;
count--;
cout << "pop data: " << delNode->data << endl;
delete delNode;
}
template <class T>
const T Stack<T>::Top() const {
if (top == nullptr || count == 0) {
cout << "There have no data, can't top." << endl;
return 0;
}
return top->data;
}
#endif /*STACK_H*/
文件main.cpp
// main.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main(void) {
try {
Stack<int> s1;
s1.Push(1);
s1.Push(2);
cout << "size: " << s1.Size() << endl;
s1.Pop();
cout << "top: " << s1.Top() << endl;
s1.Pop();
s1.Pop();
cout << s1.Top() << endl;
cout << "size: " << s1.Size() << endl;
cout << "-------------------------------" << endl;
Stack<int> s2(3);
if(s2.IsEmpty()) {
cout << "Stack is empty." << endl;
}
s2.Push(1);
s2.Push(2);
s2.Push(3);
s2.Push(4);
cout << "Stack's size: " << s2.Size() << endl;
cout << "-------------------------------" << endl;
Stack<char> s3;
cout << "size: " << s3.Size() << endl;
s3.Push('a');
s3.Push('b');
cout << "-------------------------------" << endl;
Stack<string> s4(3);
string a = "abc";
cout << "a = " << a << endl;
s4.Push(a);
s4.Push("bcd");
s4.Push("cde");
cout << "top: " << s4.Top() << endl;
s4.Pop();
cout << "top: " << s4.Top() << endl;
// cout << NULL << endl;
} catch (exception const &ex) {
cerr << "Exception: " << ex.what() << endl;
return -1;
}
}
output:
init, come on!
at the first, count = 0
push data: 1
push data: 2
size: 2
pop data: 2
top: 1
pop data: 1
There have no data. Pop fail.
There have no data, can't top.
0
size: 0
-------------------------------
max = 3
Stack is empty.
push data: 1
push data: 2
push data: 3
Stack is full. Can't push.
Stack's size: 3
-------------------------------
init, come on!
at the first, count = 0
size: 0
push data: a
push data: b
-------------------------------
max = 3
a = abc
push data: abc
push data: bcd
push data: cde
top: cde
pop data: cde
top: bcd