foreword
content
- foreword
- 1.Stack interface and simulation implementation
- 2.Queue interface and simulation implementation
- 3. Brush questions
Preliminary knowledge:
- container
A container is a class template for storing data. Data structures such as variable-length arrays, linked lists, and balanced binary trees are implemented as containers in STL
- container adapter
Adapter is a design pattern that converts the interface of a class into another interface that the client wants
- vector
A vector is a variable-sized sequence that is a container
Our stack and queue, the underlying implementation is to encapsulate the interface of vector, showing some new interfaces that conform to the characteristics of stack and queue, which is a reuse behavior
1.Stack interface and simulation implementation
stack is a stack, in line with the characteristics of LIFO (last in first out)
interface | Function |
---|---|
empty() | Empty |
size() | returns the number of elements |
top() | return the top element of the stack |
push() | push the stack |
pop() | pop the top element of the stack |
swap() | exchange |
Simulation implementation
namespace imdanteking
{
template<class T>
class stack {
public:
stack()
{
}
~stack()
{
}
stack(const stack& st) {
_v(st._v);
}
void push(T data) {
_v.push_back(data);
}
void pop() {
assert(!empty());
_v.pop_back();
}
const T& top() {
assert(!empty());
return _v.back();
}
bool empty() {
return _v.empty();
}
void swap(stack st) {
st._v.swap(_v);
}
size_t size()const {
return _v.size();
}
private:
vector<T> _v;
};
void Test1() {
stack<int> st1;
cout << st1.empty() << endl;
st1.push(1);
cout << "size: " << st1.size() << endl;
st1.push(2);
st1.push(3);
st1.push(4);
st1.push(5);
st1.push(6);
cout <<"size: " << st1.size() << endl;
while (!st1.empty()) {
cout << st1.top() << endl;
st1.pop();
}
cout << st1.empty() << endl;
cout << "size: " << st1.size() << endl;
}
}
2.Queue interface and simulation implementation
Queue is a queue, which conforms to the characteristics of FIFO (first in, first out)
interface | Function |
---|---|
empty() | Empty |
size() | returns the number of elements |
front() | Returns the first element of the queue |
back() | Returns the last element of the queue |
push() | Insert element at the end of the queue |
pop() | remove element from head of queue |
swap() | exchange |
Simulation implementation
namespace imdanteking
{
template <class T>
class queue {
public:
queue() {
}
~queue() {
}
void push(const T& val) {
_v.push_back(val);
}
void pop() {
assert(!_v.empty());
_v.erase(_v.begin());
}
const T& front() const {
assert(!_v.empty());
return _v.front();
}
const T& back() const {
assert(!_v.empty());
return _v.back();
}
bool empty() {
return _v.size() == 0;
}
size_t size() {
return _v.size();
}
void swap(queue q) {
_v.swap(q._v);
}
private:
vector<T> _v;
};
void Test1() {
queue<int> q;
q.push(1);
q.push(2);
cout << q.back() << endl;
q.push(3);
cout << q.back() << endl;
q.push(4);
q.push(5);
cout << "size: " << q.size() << endl;
cout << q.front() << endl;
while (!q.empty()) {
cout << "front: " << q.front() << endl;
cout << "back: " << q.back() << endl;
q.pop();
}
cout << "size: " << q.size() << endl;
}
}
3. Brush questions
The push and pop sequence of the minimum stack stack Implement the
queue with two stacks Implement the stack
with the queue