学习 <<数据结构与面向对象程序设计>>(c++版)(第四版)
刚学的链表,有错请指正,谢谢大家!!!
头文件 Node.h
#ifndef GUO_LING_HANG_TEMPLATE_NODE_
#define GUO_LING_HANG_TEMPLATE_NODE_
#include <cstdlib>
#include <iterator>
template <typename Item>
class node
{
public:
//TYPEDEF
typedef Item value_type;
//构造函数
node(const Item & init_date = Item(), node* init_link = nullptr)
{
data_field = init_date;
link_field = init_link;
}
//函数:非常量成员
//函数:查询对象的数据域
Item & data() { return data_field; }
//函数:查询对象的链接域
node * link() { return link_field; }
//函数:设置新的数据域
void set_data(const Item & new_data) { data_field = new_data; }
//函数:设置新的链接域
void set_link(node* new_link) { link_field = new_link; }
//常量成员函数
//函数:常量查询对象的数据域
const Item & data() { return data_field; }
//函数:常量查询对象的链接域
const node * link() { return link_field; }
private:
Item data_field;
node* link_field;
};
//函数:操纵链表
template <class Item>
void list_clear(node<Item>*& head_ptr);
template <class Item>
void list_copy
(const node<Item>* source_ptr, node<Item>*& head_ptr, node<Item>*& tail_ptr);
template <class Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry);
template <class Item>
void list_head_remove(node<Item>*& head_ptr);
template <class Item>
void list_insert(node<Item>* previous_ptr, const Item& entry);
template <class Item>
std::size_t list_length(const node<Item>* head_ptr);
template <class NodePtr, class SizeType>
NodePtr list_locate(NodePtr head_ptr, SizeType position);
template <class Item>
void list_remove(node<Item>* previous_ptr);
template <class NodePtr, class Item>
NodePtr list_search(NodePtr head_ptr, const Item& target);
#include "node2.template"
#endif
实现文件
node2.template
#include <iostream>
#include <cassert>
#include <cstdlib>
namespace GUO_LING_HANG_
{
template <class Item>
void list_clear(node<Item>*& head_ptr)
{
while (head_ptr != nullptr)
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 = nullptr;
tail_ptr = nullptr;
if (source_ptr = nullptr)
return;
head_ptr = new node(source_ptr->data(), head_ptr);
tail_ptr = head_ptr;
tail_ptr->link() = nullptr;
source_ptr = source_ptr->link();
while (source_ptr != nullptr)
{
list_insert(tail_ptr, source_ptr->data());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
tail_ptr->link() = nullptr;
}
//插入头节点
template <class Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry)
{
head_ptr = new node(entry, head_ptr);
}
//移除头节点
template <class Item>
void list_head_remove(node<Item>*& head_ptr)
{
node<Item>* remove;
remove = head_ptr;
head_ptr = head_ptr->link();
delete remove;
}
//插入节点
template <class Item>
void list_insert(node<Item>* previous_ptr, const Item& entry)
{
previous_ptr->set_link(new node(entry, previous_ptr->link()));
}
//链表长度
template <class Item>
std::size_t list_length(const node<Item>* head_ptr)
{
node<Item>* cursor;
size_t answer;
for (cursor = head_ptr; cursor != nullptr; cursor++)
answer++;
return answer;
}
//根据节点的位置在链表中寻找节点
template <class NodePtr, class SizeType>
NodePtr list_locate(NodePtr head_ptr, SizeType position)
{
size_t i;
NodePtr cursor;
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != nullptr) ; i++)
cursor = cursor->link();
return cursor;
}
//移除非头节点
template <class Item>
void list_remove(node<Item>* previous_ptr)
{
node<Item>* remove;
remove = previous_ptr ->link();
previous_ptr->set_link(remove->link());
delete remove;
}
//在链表中查找节点
template <class NodePtr, class Item>
NodePtr list_search(NodePtr head_ptr, const Item& target)
{
NodePtr cursor;
for (cursor = head_ptr; cursor != nullptr; cursor->link())
if (target == cursor->data())
return cursor;
}
}