#include <iostream>
using namespace std;
class Range{
int B, E, D;
public:
class Iterator{
public:
int index;
Range& out;
Iterator(Range& o, int i): out(o), index(i){}
void operator++(){
index += out.D;
}
int& operator*(){
return index;
}
int operator*()const {
return index;
}
bool operator!=(Iterator i){
return out.D * (i.index - index) > 0;
}
};
Range(int b = 0, int e = 0, int d = 1): B(b), E(e), D(d){}
Iterator begin(){
return Iterator(*this, B);
}
Iterator end(){
return Iterator(*this, E);
}
};
template <class T = int>
class List{
public:
struct Node{
T data;
Node *next, *prior;
Node(const T& data): data(data){prior = next = nullptr;}
};
protected:
size_t length;
Node *head, *tail;
public:
List(){
length = 0;
head = tail = nullptr;
}
class Iterator{
Node* node;
List& list;
public:
Iterator(List& list, Node* node): list(list), node(node){};
void operator++(){node = node->next;}
T& operator*()const { return node->data;}
T operator*(){return node->data;}
bool operator!=(Iterator i){return node != nullptr;}
};
Iterator begin(){return Iterator(*this, head);}
Iterator end(){return Iterator(*this, nullptr);}
virtual size_t size(){ return length;}
virtual bool empty(){return !length;}
virtual T& front(){ return head->data;}
virtual T& back(){ return tail->data;}
virtual void push_back(const T& o){
Node *p = new Node(o);
if(length == 0){
head = tail = p;
}else{
p->prior = tail;
tail->next = p;
tail = tail->next;
}
length++;
}
virtual void push_front(const T& o){
Node *p = new Node(o);
if(length == 0){
head = tail = p;
}else{
p->next = head;
head->prior = p;
head = head->prior;
}
length++;
}
virtual bool pop_back(){
if(length == 0) return false;
else if(length == 1){
delete head;
head = tail = nullptr;
length--;
return true;
}else{
Node* p = tail;
tail = tail->prior;
delete p;
length--;
return true;
}
}
virtual bool pop_front(){
if(length == 0) return false;
else if(length == 1){
delete head;
head = tail = nullptr;
length--;
return true;
}else{
Node* p = head;
head = head->next;
delete p;
length--;
return true;
}
}
};
template <class T = int>
class Stack: protected List<T>{
public:
Stack(): List<T>(){}
virtual size_t size(){return this->length;}
virtual bool empty(){ return !this->length;}
virtual T& top(){ return this->front();}
virtual void push(const T& o){this->push_front(o);}
virtual T& pop(){ this->pop_front(); }
};
template <class T = int>
class Queue: protected List<T>{
public:
Queue(): List<T>(){}
virtual size_t size(){return this->length;}
virtual bool empty(){ return !this->length;}
virtual T& front(){ return this->head->data;}
virtual T& back(){ return this->tail->data;}
virtual void push(const T& o){this->push_back(o);}
virtual T& pop(){ this->pop_front(); }
};
int main() {
cout << endl << "---------------range-----------------" << endl;
for(auto& i: Range(0, 9)) cout << i << " "; cout << endl;
for(auto i: Range(0, 9, 2)) cout << i << " "; cout << endl;
for(auto i: Range(9, -1, -1)) cout << i << " "; cout << endl;
for(auto i: Range(9, -1, -2)) cout << i << " "; cout << endl;
cout << endl << "---------------list-----------------" << endl;
List<int> list;
for(auto i: Range(0, 4))
list.push_back(i);
for(auto item: list)
cout << item << " ";
cout << endl;
while(!list.empty())list.pop_back();
for(auto i: Range(0, 4))
list.push_front(i);
for(auto item: list)
cout << item << " ";
cout << endl;
while(!list.empty())list.pop_front();
for(auto i: Range(0, 4))
list.push_back(i);
for(auto item: list)
cout << item << " ";
cout << endl << "---------------stack-----------------" << endl;
Stack<int> stack;
for(auto i: Range(0, 4))stack.push(i);
cout << stack.size() << endl;
cout << stack.top() << endl;
while(!stack.empty()) {
cout << stack.top() << " ";
stack.pop();
}
cout << endl << "---------------queue-----------------" << endl;
Queue<double> queue;
for(auto i: Range(0, 5)) queue.push(i*1.5);
cout << queue.size() << endl;
cout << queue.front() << " " << queue.back() << endl;
while(!queue.empty()){
cout << queue.front() << " ";
queue.pop();
}
return 0;
}
随便写下,不喜勿喷。
可能有BUG,请自己测试好