introduction to algorithms 菜鸟笔记 list
来源:互联网 发布:贵金属网络销售技巧 编辑:程序博客网 时间:2024/04/30 14:35
//author:yydrewdrew
#define ERROR -1
template <class T>
struct node
{
node *next;
T value;
};
template <class T>
class list
{
public:
explicit list(const T &t,const size_t num);
explicit list();
explicit list(const T *p,const size_t num);
T *Insert(const T &t,uint pos);
T Delete(uint pos);
size_t size()const;
void clear();
list(const list<T> &obj);
list<T> &operator = (const list<T> &obj);
bool operator == (const list<T> &obj)const;
bool operator != (const list<T> &obj)const;
T &operator[](const uint pos);
virtual ~list();
private:
void Destory();
size_t number;
node<T> *head;
};
template <class T>
list<T>::list<T>(const T &t,const size_t num)
{
if (num <= 0)
{
head = NULL;
number = 0;
}
else
{
head = new node<T>;
head->value = t;
head->prev = NULL;
head->next = NULL;
node<T> *p = head;
number = 1;
while(number < num)
{
p->next = new node<T>;
p->next->value = t;
p->next->prev = p;
p = p->next;
p->next = NULL;
++number;
}
}
}
template<class T>
list<T> &list<T>::operator = (const list<T> &obj)
{
if (this != &obj)
{
if (obj.head == NULL)
{
Destory();
head = NULL;
number = 0;
}
else
{
node<T> *p = new node<T>;
node<T> *p_ = obj.head;
p->value = p_->value;
p->prev = NULL;
p->next = NULL;
node<T> *_head = p;
while (p_->next != NULL)
{
p_ = p_->next;
p->next = new node<T>;
p->next->value = p_->value;
p->next->prev = p;
p->next->next = NULL;
p = p->next;
}
Destory();
head = _head;
number = obj.number;
}
}
return *this;
}
template <class T>
list<T>::list<T>():number(0),head(NULL){}
template<class T>
bool list<T>::operator == (const list<T> &obj)const
{
if (number != obj.number)
{
return false;
}
bool is = true;
node<T> *p = head;
node<T> *_p = obj.head;
while (p != NULL && _p != NULL)
{
if (p->value != _p->value)
{
is = false;
break;
}
p = p->next;
_p = _p->next;
}
return is;
}
template<class T>
void list<T>::Destory()
{
while (head != NULL)
{
node<T> *_p = head->next;
delete head;
head = _p;
}
number = 0;
}
template <class T>
T *list<T>::Insert(const T& t,const uint pos)
{
if (pos > number)
{
return NULL;
}
if (number == 0)
{
head = new node<T>;
head->value = t;
head->prev = NULL;
head->next = NULL;
++number;
return &head->value;
}
uint tem = 0;
node<T> *p = head;
while(tem < pos)
{
++tem;
p = p->next;
}
node<T> *ptem = new node<T>;
ptem->value = t;
ptem->next = p->next;
ptem->prev = p;
p->next = ptem;
++number;
return &ptem->value;
}
template<class T>
T &list<T>::operator[](const uint pos)
{
if (pos < 0 || pos > number - 1)
{
throw ERROR;
}
if (pos == 0)
{
return head->value;
}
uint tem = 0;
node<T> *p = head;
while (tem < pos - 1)
{
++tem;
p = p->next;
}
return p->next->value;
}
template <class T>
T list<T>::Delete(uint pos)
{
if (pos < 0 || pos >= number)
{
throw ERROR;
}
node<T> *p = head;
while (pos--)
{
p = p->next;
}
node<T> *_p = p->next;
T _t = _p->value;
p->next = _p->next;
if (_p->next != NULL)
{
_p->next->prev = p;
}
delete _p;
--number;
return _t;
}
template<class T>
list<T>::~list<T>()
{
Destory();
}
template<class T>
list<T>::list<T>(const list& obj)
{
if (obj.head == NULL)
{
head = NULL;
number = 0;
}
else
{
node<T> *p = new node<T>;
node<T> *p_ = obj.head;
p->value = p_->value;
p->next = NULL;
p->prev = NULL;
head = p;
number = obj.number;
while (p_->next != NULL)
{
p_ = p_->next;
p->next = new node<T>;
p->next->value = p_->value;
p->next->next = NULL;
p->next->prev = p;
p = p->next;
}
}
}
template<class T>
list<T>::list<T>(const T *p,const size_t num)
{
if (p == NULL || num <=0)
{
head == NULL;
number = 0;
}
else
{
number = 1;
head = new node<T>;
head->value = p[0];
head->next = NULL;
head->prev = NULL;
node<T> *_p = head;
while (number < num)
{
_p->next = new node<T>;
_p->next->value = p[number];
_p->next->prev = _p;
_p->next->next = NULL;
_p = _p->next;
++number;
}
}
}
template <class T>
size_t list<T>::size()const
{
return number;
}
template <class T>
bool list<T>::operator != (const list<T> &obj)const
{
if (number != obj.number)
{
return true;
}
bool is = false;
node<T> *p = head;
node<T> *_p = obj.head;
while (p != NULL && _p != NULL)
{
if (p->value != _p->value)
{
is = true;
break;
}
p = p->next;
_p = _p->next;
}
return is;
}
template <class T>
void list<T>::clear()
{
Destory();
}
- introduction to algorithms 菜鸟笔记 list
- introduction to algorithms 菜鸟笔记 sort algorithms
- introduction to algorithms 菜鸟笔记 stack
- introduction to algorithms 菜鸟笔记 queue
- introduction to algorithms 菜鸟笔记 vector
- introduction to algorithms 菜鸟笔记 slist
- introduction to algorithms 菜鸟笔记 binfindtree
- introduction to algorithms 菜鸟笔记 rbtree(红黑树)
- introduction to algorithms 菜鸟笔记 sattree(统计树)
- introduction to algorithms 笔记 AVLtree
- Introduction to Algorithms算法导论笔记-Lecture1
- [Introduction to Algorithms 学习笔记] 单源最短路径
- About Introduction to Algorithms
- Introduction to Algorithms
- Introduction to Algorithms
- BUBBLESORT: INTRODUCTION TO ALGORITHMS
- Introduction to Algorithms
- introduction to algorithms 笔记 interval tree(区间树)
- 太原面试题1- java读取文本指定内容
- SQL之left join/right join/inner join
- CHeaderCtrl用法
- It is really Awesome!
- 开始走自己的道路了
- introduction to algorithms 菜鸟笔记 list
- 痛苦的网上购物经历
- Everything Begins In A New Point
- SYBASE中生成所有建表语句的过程
- poj3176
- introduction to algorithms 菜鸟笔记 slist
- 发现一个学习web技术的网站
- insert后获得插入数据的编号
- 怎样向iframe中的表单传值