模板容器类及迭代器的实现二(基于链表)
2016-07-27 15:20
351 查看
节点类头文件node.h:
#ifndef MAIN_WTF_NODE1_H
#define MAIN_WTF_NODE1_H
#include <cstdlib>
namespace main_wtf_6B
{
template<class Item>
class node
{
public:
typedef Item value_type;
node(const Item& init_data = Item(),node* init_link = NULL)
{
data_field = init_data;
link_field = init_link;
}
void set_data(const Item& new_data)
{
data_field = new_data;
}
void set_link(node* new_link)
{
link_field = new_link;
}
Item data() const
{
return data_field;
}
const node* link() const
{
return link_field;
}
node* link()
{
return link_field;
}
private:
Item data_field;
node* link_field;
};
template<class Item>
std::size_t list_length(const node<Item>* head_ptr);
template<class Item>
void list_head_insert(node<Item>*& head_ptr,const Item& entry);
template<class Item>
void list_insert(node<Item>*& previous,const Item& entry);
template<class NodePtr,class Item>
NodePtr list_search(NodePtr head_ptr,const Item& target);
template<class Item>
void list_head_remove(node<Item>*& head_ptr);
template<class Item>
void list_remove(node<Item>* previous_ptr);
template<class Item>
void list_clear(node<Item>*& head_ptr);
template<class NodePtr>
NodePtr list_locate(NodePtr head_ptr,std::size_t position);
template<class Item>
void list_copy(const node<Item>* source_ptr,node<Item>*& head_ptr,node<Item>*& tail_ptr);
template <class Item>
class node_iterator: public std::iterator<std::forward_iterator_tag,Item>
{
public:
node_iterator(node<Item>* init = NULL)
{ current = init; }
Item& operator*() const
{ return current->data(); }
node_iterator& operator++()
{
current = current->link();
return *this;
}
node_iterator& operator++(int)
{
node_iterator original(current);
current = current->link();
return original;
}
bool operator ==(const node_iterator other) const
{ return current == other.current; }
bool operator !=(const node_iterator other) const
{ return current != other.current;}
private:
node<Item>* current;
};
template <class Item>
class const_node_iterator: public std::iterator<std::forward_iterator_tag,Item>
{
public:
const_node_iterator(node<Item>* init = NULL)
{ current = init; }
Item& operator*() const
{ return current->data(); }
const_node_iterator& operator++()
{
current = current->link();
return *this;
}
const_node_iterator& operator++(int)
{
const_node_iterator original(current);
current = current->link();
return original;
}
bool operator ==(const const_node_iterator other) const
{ return current == other.current; }
bool operator !=(const const_node_iterator other) const
{ return current != other.current;}
private:
const node<Item>* current;
};
}
#include "node.template"
#endif
节点类实现文件node.template:
#include"node2.h"
namespace main_wtf_6B
{
template<class Item>
std::size_t list_length(const node<Item>* head_ptr)
{
size_t answer = 0;
const node<Item>* cursor;
for(cursor = head_ptr; cursor != NULL; cursor = cursor->link())
{
++answer;
}
return answer;
}
template<class Item>
void list_head_insert(node<Item>*& head_ptr,const Item& entry)
{
head_ptr = new node<Item>(entry,head_ptr);
}
template<class Item>
void list_insert(node<Item>*& previous,const Item& entry)
{
node<Item>* insert_ptr;
insert_ptr = new node<Item>;
insert_ptr->set_data(entry);
insert_ptr->set_link(previous->link());
previous->set_link(insert_ptr);
}
template<class NodePtr,class Item>
NodePtr list_search(NodePtr head_ptr,const Item& target)
{
NodePtr cursor;
for(cursor = head_ptr;cursor != NULL;cursor = cursor->link())
if(target == cursor->data())
return cursor;
return NULL;
}
/*
const node<Item>* list_search(const node<Item>* head_ptr,const node<Item>::value_type& target)
{
const node<Item>* cursor;
for(cursor = head_ptr;cursor != NULL;cursor = cursor->link())
if(target == cursor->data())
return cursor;
return NULL;
}
*/
template<class Item>
void list_head_remove(node<Item>*& head_ptr)
{
node<Item>* remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link();
delete remove_ptr;
}
template<class Item>
void list_remove(node<Item>* previous_ptr)
{
node<Item>* remove_ptr;
remove_ptr = previous_ptr->link();
previous_ptr->set_link(remove_ptr->link());
delete remove_ptr;
}
template<class Item>
void list_clear(node<Item>*& head_ptr)
{
while(head_ptr != NULL)
list_head_remove(head_ptr);
}
template<class Item>
void list_copy(const node<Item>* source_ptr,node<Item>*& head_ptr,node<Item>*& tail_ptr)
{
head_ptr = NULL;
tail_ptr = NULL;
if(source_ptr == NULL)
return;
list_head_insert(head_ptr,source_ptr->data());
tail_ptr = head_ptr;
source_ptr = source_ptr->link();
while(source_ptr != NULL)
{
list_insert(tail_ptr,source_ptr->data());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
}
template<class NodePtr>
NodePtr list_locate(NodePtr head_ptr,std::size_t position)
{
NodePtr cursor;
cursor = head_ptr;
for(std::size_t i=1;(i<position) && (cursor != NULL);i++)
cursor = cursor->link();
return cursor;
}
}
容器类头文件bag.h:
#ifndef MAIN_WTF_GAB5_H
#define MAIN_WTF_GAB5_H
#include<cstdlib>
#include"node2.h"
namespace main_wtf_6B
{
template <class Item>
class bag
{
public:
typedef std::size_t size_type;
typedef Item value_type;
typedef node_iterator<Item> iterator;
typedef const_node_iterator<Item> const_iterator;
bag();
bag(const bag& source);
~bag();
size_type erase(const Item& target);
bool erase_one(const Item& target);
void insert(const Item& entry);
void operator +=(const bag& addend);
void operator = (const bag& source);
size_type size() const { return many_nodes; }
size_type count(const Item& target) const;
Item grab() const;
iterator begin()
{ return iterator(head_ptr); }
const_iterator begin() const
{ return const_iterator(head_ptr); }
iterator end()
{ return iterator(); }
iterator end() const
{ return const_iterator(); }
private:
node<Item>* head_ptr;
size_type many_nodes;
};
template<class Item>
bag<Item> operator +(const bag<Item>& b1,const bag<Item>& b2);
}
#include "bag5.template"
#endif
容器类实现文件bag.template:
#include"bag5.h"
#include<cassert>
namespace main_wtf_6B
{
template<class Item>
bag<Item>::bag()
{
head_ptr = NULL;
many_nodes = 0;
}
template<class Item>
bag<Item>::bag(const bag<Item>& source)
{
node* tail_ptr;
list_copy(source.head_ptr,head_ptr,tail_ptr);
many_nodes = source.many_nodes;
}
template<class Item>
void bag<Item>::operator=(const bag<Item>& source)
{
node* tail_ptr;
if(this == &source)
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr,head_ptr,tail_ptr);
many_nodes = source.many_nodes;
}
template<class Item>
bag<Item>::~bag()
{
list_clear(head_ptr);
many_nodes = 0;
}
template<class Item>
bool bag<Item>::erase_one(const Item& target)
{
node<Item>* target_node;
target_node = list_search(head_ptr,target);
if(target_node == NULL)
return false;
target_node->set_data(head_ptr->data());
list_head_remove(head_ptr);
--many_nodes;
return true;
}
template<class Item>
std::size_t bag<Item>::erase(const Item& target)
{
bag<Item>::size_type answer = 0;
while(erase_one(target))
answer++;
return answer;
}
template<class Item>
void bag<Item>::insert(const Item& entry)
{
list_head_insert(head_ptr,entry);
many_nodes++;
}
template<class Item>
void bag<Item>::operator+=(const bag<Item>& addend)
{
node* cursor;
cursor = addend.head_ptr;
while(cursor != NULL)
{
list_head_insert(head_ptr,cursor->data());
cursor = cursor->link();
}
many_nodes += addend.many_nodes;
}
template<class Item>
std::size_t bag<Item>::count(const Item& target) const
{
size_type answer;
const node<Item>* cursor;
answer = 0;
cursor = list_search(cursor,target);
while(cursor != NULL)
{
++answer;
cursor = cursor->link();
cursor = list_search(cursor,target);
}
return answer;
}
template<class Item>
Item bag<Item>::grab() const
{
size_type i;
const node<Item>* cursor;
assert(size()>0);
i = (rand() % size()) +1;
cursor = list_locate(head_ptr,i);
return cursor->data();
}
template<class Item>
bag<Item> operator +(const bag<Item>& b1,const bag<Item>& b2)
{
bag<Item> answer;
answer += b1;
answer += b2;
return answer;
}
}
#ifndef MAIN_WTF_NODE1_H
#define MAIN_WTF_NODE1_H
#include <cstdlib>
namespace main_wtf_6B
{
template<class Item>
class node
{
public:
typedef Item value_type;
node(const Item& init_data = Item(),node* init_link = NULL)
{
data_field = init_data;
link_field = init_link;
}
void set_data(const Item& new_data)
{
data_field = new_data;
}
void set_link(node* new_link)
{
link_field = new_link;
}
Item data() const
{
return data_field;
}
const node* link() const
{
return link_field;
}
node* link()
{
return link_field;
}
private:
Item data_field;
node* link_field;
};
template<class Item>
std::size_t list_length(const node<Item>* head_ptr);
template<class Item>
void list_head_insert(node<Item>*& head_ptr,const Item& entry);
template<class Item>
void list_insert(node<Item>*& previous,const Item& entry);
template<class NodePtr,class Item>
NodePtr list_search(NodePtr head_ptr,const Item& target);
template<class Item>
void list_head_remove(node<Item>*& head_ptr);
template<class Item>
void list_remove(node<Item>* previous_ptr);
template<class Item>
void list_clear(node<Item>*& head_ptr);
template<class NodePtr>
NodePtr list_locate(NodePtr head_ptr,std::size_t position);
template<class Item>
void list_copy(const node<Item>* source_ptr,node<Item>*& head_ptr,node<Item>*& tail_ptr);
template <class Item>
class node_iterator: public std::iterator<std::forward_iterator_tag,Item>
{
public:
node_iterator(node<Item>* init = NULL)
{ current = init; }
Item& operator*() const
{ return current->data(); }
node_iterator& operator++()
{
current = current->link();
return *this;
}
node_iterator& operator++(int)
{
node_iterator original(current);
current = current->link();
return original;
}
bool operator ==(const node_iterator other) const
{ return current == other.current; }
bool operator !=(const node_iterator other) const
{ return current != other.current;}
private:
node<Item>* current;
};
template <class Item>
class const_node_iterator: public std::iterator<std::forward_iterator_tag,Item>
{
public:
const_node_iterator(node<Item>* init = NULL)
{ current = init; }
Item& operator*() const
{ return current->data(); }
const_node_iterator& operator++()
{
current = current->link();
return *this;
}
const_node_iterator& operator++(int)
{
const_node_iterator original(current);
current = current->link();
return original;
}
bool operator ==(const const_node_iterator other) const
{ return current == other.current; }
bool operator !=(const const_node_iterator other) const
{ return current != other.current;}
private:
const node<Item>* current;
};
}
#include "node.template"
#endif
节点类实现文件node.template:
#include"node2.h"
namespace main_wtf_6B
{
template<class Item>
std::size_t list_length(const node<Item>* head_ptr)
{
size_t answer = 0;
const node<Item>* cursor;
for(cursor = head_ptr; cursor != NULL; cursor = cursor->link())
{
++answer;
}
return answer;
}
template<class Item>
void list_head_insert(node<Item>*& head_ptr,const Item& entry)
{
head_ptr = new node<Item>(entry,head_ptr);
}
template<class Item>
void list_insert(node<Item>*& previous,const Item& entry)
{
node<Item>* insert_ptr;
insert_ptr = new node<Item>;
insert_ptr->set_data(entry);
insert_ptr->set_link(previous->link());
previous->set_link(insert_ptr);
}
template<class NodePtr,class Item>
NodePtr list_search(NodePtr head_ptr,const Item& target)
{
NodePtr cursor;
for(cursor = head_ptr;cursor != NULL;cursor = cursor->link())
if(target == cursor->data())
return cursor;
return NULL;
}
/*
const node<Item>* list_search(const node<Item>* head_ptr,const node<Item>::value_type& target)
{
const node<Item>* cursor;
for(cursor = head_ptr;cursor != NULL;cursor = cursor->link())
if(target == cursor->data())
return cursor;
return NULL;
}
*/
template<class Item>
void list_head_remove(node<Item>*& head_ptr)
{
node<Item>* remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link();
delete remove_ptr;
}
template<class Item>
void list_remove(node<Item>* previous_ptr)
{
node<Item>* remove_ptr;
remove_ptr = previous_ptr->link();
previous_ptr->set_link(remove_ptr->link());
delete remove_ptr;
}
template<class Item>
void list_clear(node<Item>*& head_ptr)
{
while(head_ptr != NULL)
list_head_remove(head_ptr);
}
template<class Item>
void list_copy(const node<Item>* source_ptr,node<Item>*& head_ptr,node<Item>*& tail_ptr)
{
head_ptr = NULL;
tail_ptr = NULL;
if(source_ptr == NULL)
return;
list_head_insert(head_ptr,source_ptr->data());
tail_ptr = head_ptr;
source_ptr = source_ptr->link();
while(source_ptr != NULL)
{
list_insert(tail_ptr,source_ptr->data());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
}
template<class NodePtr>
NodePtr list_locate(NodePtr head_ptr,std::size_t position)
{
NodePtr cursor;
cursor = head_ptr;
for(std::size_t i=1;(i<position) && (cursor != NULL);i++)
cursor = cursor->link();
return cursor;
}
}
容器类头文件bag.h:
#ifndef MAIN_WTF_GAB5_H
#define MAIN_WTF_GAB5_H
#include<cstdlib>
#include"node2.h"
namespace main_wtf_6B
{
template <class Item>
class bag
{
public:
typedef std::size_t size_type;
typedef Item value_type;
typedef node_iterator<Item> iterator;
typedef const_node_iterator<Item> const_iterator;
bag();
bag(const bag& source);
~bag();
size_type erase(const Item& target);
bool erase_one(const Item& target);
void insert(const Item& entry);
void operator +=(const bag& addend);
void operator = (const bag& source);
size_type size() const { return many_nodes; }
size_type count(const Item& target) const;
Item grab() const;
iterator begin()
{ return iterator(head_ptr); }
const_iterator begin() const
{ return const_iterator(head_ptr); }
iterator end()
{ return iterator(); }
iterator end() const
{ return const_iterator(); }
private:
node<Item>* head_ptr;
size_type many_nodes;
};
template<class Item>
bag<Item> operator +(const bag<Item>& b1,const bag<Item>& b2);
}
#include "bag5.template"
#endif
容器类实现文件bag.template:
#include"bag5.h"
#include<cassert>
namespace main_wtf_6B
{
template<class Item>
bag<Item>::bag()
{
head_ptr = NULL;
many_nodes = 0;
}
template<class Item>
bag<Item>::bag(const bag<Item>& source)
{
node* tail_ptr;
list_copy(source.head_ptr,head_ptr,tail_ptr);
many_nodes = source.many_nodes;
}
template<class Item>
void bag<Item>::operator=(const bag<Item>& source)
{
node* tail_ptr;
if(this == &source)
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr,head_ptr,tail_ptr);
many_nodes = source.many_nodes;
}
template<class Item>
bag<Item>::~bag()
{
list_clear(head_ptr);
many_nodes = 0;
}
template<class Item>
bool bag<Item>::erase_one(const Item& target)
{
node<Item>* target_node;
target_node = list_search(head_ptr,target);
if(target_node == NULL)
return false;
target_node->set_data(head_ptr->data());
list_head_remove(head_ptr);
--many_nodes;
return true;
}
template<class Item>
std::size_t bag<Item>::erase(const Item& target)
{
bag<Item>::size_type answer = 0;
while(erase_one(target))
answer++;
return answer;
}
template<class Item>
void bag<Item>::insert(const Item& entry)
{
list_head_insert(head_ptr,entry);
many_nodes++;
}
template<class Item>
void bag<Item>::operator+=(const bag<Item>& addend)
{
node* cursor;
cursor = addend.head_ptr;
while(cursor != NULL)
{
list_head_insert(head_ptr,cursor->data());
cursor = cursor->link();
}
many_nodes += addend.many_nodes;
}
template<class Item>
std::size_t bag<Item>::count(const Item& target) const
{
size_type answer;
const node<Item>* cursor;
answer = 0;
cursor = list_search(cursor,target);
while(cursor != NULL)
{
++answer;
cursor = cursor->link();
cursor = list_search(cursor,target);
}
return answer;
}
template<class Item>
Item bag<Item>::grab() const
{
size_type i;
const node<Item>* cursor;
assert(size()>0);
i = (rand() % size()) +1;
cursor = list_locate(head_ptr,i);
return cursor->data();
}
template<class Item>
bag<Item> operator +(const bag<Item>& b1,const bag<Item>& b2)
{
bag<Item> answer;
answer += b1;
answer += b2;
return answer;
}
}
相关文章推荐
- 基于RxJava实现酷炫启动页
- Spring MVC之@RequestMapping 详解
- 《时代三部曲》感悟二
- C++ text file process summary
- HDU3538 A sample Hamilton path
- Spring Boot Document Part I
- SSM+DUBBO/ZK异常
- tarjan 强连通 模板
- ios数据存储——对象归档
- HDU-4533 威威猫系列故事——晒被子(区间更新)
- html往js文件传递参数
- 类似智联招聘选择职位的弹出框效果,可做选择城市等信息
- win7 iis 未能加载文件或程序集“Oracle.DataAccess, Version=4.112.3.0...
- JDBC数据源连接池的配置和使用实例
- Baidu地图开发,定位,检索
- Java数据库连接池的几种配置方法(以MySQL数据库为例)
- behave-Tutorial01-来个简单示例
- 工作杂记
- servlet 具体实现
- python3 匹配任意字符