-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
91 lines (87 loc) · 3.08 KB
/
Stack.h
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
//
// Created by Telephone on 2019/10/20 0020.
//
#ifndef STACK_STACK_H
#define STACK_STACK_H
#include <stdexcept>
#include <cstdio>
#include <ArrayStorage.h>
namespace Telephone_DS::arrayBase::Stack //Telephone写的Stack的命名空间
{
template <typename T> class Stack : protected ArrayStorage::ArrayStorage<T>
{
public:
explicit Stack(long scale) //有参数构造
: ArrayStorage::ArrayStorage<T>::ArrayStorage(scale)
{}
explicit Stack() //无参数构造
: ArrayStorage::ArrayStorage<T>::ArrayStorage()
{}
Stack(Stack<T> const& src) //拷贝构造
: ArrayStorage::ArrayStorage<T>::ArrayStorage(src)
{}
Stack(Stack<T>&& src) noexcept //移动构造
: ArrayStorage::ArrayStorage<T>::ArrayStorage(std::move(src))
{}
Stack<T>& operator=(Stack<T> const& right) //拷贝赋值
{
if (this == &right)
return *this;
ArrayStorage::ArrayStorage<T>::operator=(right);
return *this;
}
Stack<T>& operator=(Stack<T>&& right) noexcept //移动赋值
{
ArrayStorage::ArrayStorage<T>::operator=(std::move(right));
return *this;
}
virtual ~Stack() = default; //析构 //虚以派生
int isEmpty() override //if empty , return 1(true)
{
return ArrayStorage::ArrayStorage<T>::isEmpty();
}
long len() override
{
return ArrayStorage::ArrayStorage<T>::len();
}
virtual void push(T const& x)
{
ArrayStorage::ArrayStorage<T>::addAfter(ArrayStorage::ArrayStorage<T>::len() - 1, x);
}
virtual void push(T&& x)
{
ArrayStorage::ArrayStorage<T>::addAfter(ArrayStorage::ArrayStorage<T>::len() - 1,
std::move(x));
}
virtual int pop() //if empty , return -1 ; or else , return 0.
{
if (!ArrayStorage::ArrayStorage<T>::isEmpty())
{
ArrayStorage::ArrayStorage<T>::deleteFrom(ArrayStorage::ArrayStorage<T>::len() -
1, 1);
return 0;
}
return -1;
}
virtual int clear()
{
if (!ArrayStorage::ArrayStorage<T>::isEmpty())
{
ArrayStorage::ArrayStorage<T>::deleteFrom(0, ArrayStorage::ArrayStorage<T>::len());
return 0;
}
return -1;
}
virtual T top()
{
if (!ArrayStorage::ArrayStorage<T>::isEmpty())
{
return ArrayStorage::ArrayStorage<T>::at(ArrayStorage::ArrayStorage<T>::len() - 1);
}
char exp[100];
std::sprintf(exp, "[function top()] Stack is empty!");
throw std::out_of_range(exp);
}
};
}
#endif //STACK_STACK_H