#ifndef EXERCISE_H_
#define EXERCISE_H_
#include <iostream>
typedef unsigned short ushort;
template <typename type_t>
class list{
type_t data;
ushort size_;
public:
list<type_t> * head, * tail, * next, * prev;
list();
virtual ~list();
ushort &size();
void setData(type_t);
type_t getData();
virtual void push_back(type_t) = 0;
virtual type_t pop_front() = 0;
virtual void push_front(type_t) = 0;
virtual type_t pop_back() = 0;
virtual type_t front() = 0;
virtual type_t back() = 0;
virtual void print() = 0;
};
// DEFAULT CTOR
template <typename type_t>
list<type_t>::list() : size_(0){ this->head = this->tail = this->next = this->prev = nullptr;}
// DTOR
template <typename type_t>
list<type_t>::~list() {}
template <typename type_t>
ushort & list<type_t>::size(){
return size_;
}
template <typename type_t>
void list<type_t>::setData(type_t value){
this->data = value;
}
template <typename type_t>
type_t list<type_t>::getData(){
return this->data;
}
/*********************\
* DEQUEUE CLASS
\*********************/
template <typename type_t>
class dequeue : public list<type_t> {
public:
void push_back(type_t);
type_t pop_front();
void push_front(type_t);
type_t pop_back();
type_t front();
type_t back();
void print();
};
// PUSH_BACK func
template <typename type_t>
void dequeue<type_t>::push_back(type_t value){
list<type_t> * item = new dequeue;
if (!item) throw std::bad_alloc();
item->setData(value);
if (this->tail) {
this->tail->next = item;
item->prev = this->tail;
}
this->tail = item;
++this->size();
if (!this->head) this->head = this->tail;
}
// POP_BACK func
template <typename type_t>
type_t dequeue<type_t>::pop_back(){
if(!this->head) throw std::out_of_range("List is empty");
list<type_t> * item;
type_t value;
if(this->tail && this->tail != this->head){
item = this->tail;
value = this->tail->getData();
this->tail = this->tail->prev;
this->tail->next = nullptr;
delete item;
}
else{
value = this->head->getData();
delete this->head;
this->head = this->tail = nullptr;
}
--this->size();
return value;
}
template <typename type_t>
void dequeue<type_t>::push_front(type_t value){
list<type_t> * item = new dequeue;
if (!item) throw std::bad_alloc();
item->setData(value);
item->prev = nullptr;
if(this->head){
item->next = this->head;
this->head->prev = item;
}
this->head = item;
++this->size();
if(!this->tail) this->tail = this->head;
}
template <typename type_t>
type_t dequeue<type_t>::pop_front(){
if(!this->head) throw std::out_of_range("Empty list");
list<type_t> * item;
type_t value;
if(this->head && this->head != this->tail){
value = this->head->getData();
item = this->head;
this->head = this->head->next;
this->head->prev = nullptr;
delete item;
}
else{
value = this->tail->getData();
delete this->tail;
this->head = this->tail = nullptr;
}
--this->size();
return value;
}
template <typename type_t>
type_t dequeue<type_t>::front(){
return this->head->getData();
}
template <typename type_t>
type_t dequeue<type_t>::back(){
return this->tail->getData();
}
template <typename type_t>
void dequeue<type_t>::print(){
int count = this->size();
for(int i=0; i < count; i++)
std::cout << pop_front() << " ";
}
#endif /* EXERCISE_H_ */
我创建了一个双端容器。但我对其在性能方面的效率表示怀疑。所以,请你批评代码吗?还有什么可以做的?比如缩短代码?第二件事我想使用STL算法。如何创建自己的迭代器?