单链表(C++实现)
2014-05-19 14:05
399 查看
list.h
#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H
#include <cstdlib>
//template <class Item>
class node
{
public:
typedef double valueType;
node(const valueType& init_data = valueType( ), node *init_link = NULL)
{
data_field = init_data; link_field = init_link;
}
void set_data(const valueType& new_data)//the node now contain the specified new data
{
data_field = new_data;
}
void set_link(node *new_link)// the node now contain the specified new link
{
link_field = new_link;
}
valueType data( ) const {return data_field; } //the return value is the data form this node
const node* link( ) const {return link_field ;}// the return value is the link form this node
node *link {return link_field ;} //non-const
private:
valueType data_field ;
node *link_field ;
};
std::size_t list_length( const node *head_ptr) ; //the value returned is the number of nodes in the list
void list_head_insert(node*& head_ptr, const node::valueType& entry) ;//A new node containing the given entry has been
//added at the head of the linked list
void list_insert(node * previous, const node::valueType& entry);//A new node containing the given entry has been added
//after the node that previous points to
node* list_search(node* head_ptr, const node::valueType& target ); //
const node* list_search(const node* head_ptr, const node::valueType& target);//返回与target值相同的node
node* list_locate(node* head_ptr, std::size_t position) ;//返回在poisition处的node
const node* list_locate(const node* head_ptr, std::size_t position) ;
void list_head_remove(node*& head_ptr );//去除头结点
void list_remove(node* previous);//去除previous后一个node
void list_clear(node*& head_ptr );//链表清空
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);//list 复制
};
#endif
list.cpp
#include "list.h"
size_t list_length(const node *head_ptr)
{
const node *cursor ;
size_t len ;
len = 0;
for (cursor = head_ptr; cursor != NULL; cursor++)
{
++len;
}
return len ;
}
void list_head_insert(node*& head_ptr, const node::valueType& entry)
{
head_ptr = new node(entry, head_ptr) ;
}
void list_insert(node* previous, const node::valueType& entry)
{
node *insert_ptr ;
insert_ptr = new node ;
insert_ptr->set_data(entry) ;
insert_ptr->set_link(previous->link_field) ;
previous->set_link(insert_ptr) ;
}
node* list_search(node* head_ptr, const node::valueType& target)
{
node *cursor ;
for (cursor = head_ptr; cursor != NULL ; cursor)
{
if (target == cursor->data() )
{
return cursor ;
}
}
return NULL ;
}
const node* list_search(const node* head_ptr, const node::valueType& target)
{
const node *cursor ;
for (cursor = head_ptr; cursor != NULL ; cursor)
{
if (target == cursor->data() )
{
return cursor ;
}
}
return NULL ;
}
node* list_locate(node* head_ptr, std::size_t position)
{
node * cursor = head_ptr ;
for (int i =0; (i < position) &&(cursor != NULL); i++)
{
cursor = cursor->link_field ;
}
return cursor ;
}
const node* list_locate(const node* head_ptr, std::size_t position)
{
const node * cursor = head_ptr ;
for (int i =0; (i < position) &&(cursor != NULL); i++)
{
cursor = cursor->link_field ;
}
return cursor ;
}
void list_head_remove( node*& head_ptr)
{
node *remove_ptr ;
remove_ptr = head_ptr ;
head_ptr = head_ptr->link_field ;
delete remove_ptr ;
}
void list_clear(node*& head_ptr )
{
while (head_ptr != NULL)
{
list_head_remove(head_ptr) ;
}
}
void list_copy(const node* source_ptr, node*& head_ptr, node*& 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_field ;
while (source_ptr != NULL)
{
list_insert(tail_ptr , source_ptr->data( ));
tail_ptr = tail_ptr->link_field ;
source_ptr = source_ptr->link_field ;
}
}
#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H
#include <cstdlib>
//template <class Item>
class node
{
public:
typedef double valueType;
node(const valueType& init_data = valueType( ), node *init_link = NULL)
{
data_field = init_data; link_field = init_link;
}
void set_data(const valueType& new_data)//the node now contain the specified new data
{
data_field = new_data;
}
void set_link(node *new_link)// the node now contain the specified new link
{
link_field = new_link;
}
valueType data( ) const {return data_field; } //the return value is the data form this node
const node* link( ) const {return link_field ;}// the return value is the link form this node
node *link {return link_field ;} //non-const
private:
valueType data_field ;
node *link_field ;
};
std::size_t list_length( const node *head_ptr) ; //the value returned is the number of nodes in the list
void list_head_insert(node*& head_ptr, const node::valueType& entry) ;//A new node containing the given entry has been
//added at the head of the linked list
void list_insert(node * previous, const node::valueType& entry);//A new node containing the given entry has been added
//after the node that previous points to
node* list_search(node* head_ptr, const node::valueType& target ); //
const node* list_search(const node* head_ptr, const node::valueType& target);//返回与target值相同的node
node* list_locate(node* head_ptr, std::size_t position) ;//返回在poisition处的node
const node* list_locate(const node* head_ptr, std::size_t position) ;
void list_head_remove(node*& head_ptr );//去除头结点
void list_remove(node* previous);//去除previous后一个node
void list_clear(node*& head_ptr );//链表清空
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);//list 复制
};
#endif
list.cpp
#include "list.h"
size_t list_length(const node *head_ptr)
{
const node *cursor ;
size_t len ;
len = 0;
for (cursor = head_ptr; cursor != NULL; cursor++)
{
++len;
}
return len ;
}
void list_head_insert(node*& head_ptr, const node::valueType& entry)
{
head_ptr = new node(entry, head_ptr) ;
}
void list_insert(node* previous, const node::valueType& entry)
{
node *insert_ptr ;
insert_ptr = new node ;
insert_ptr->set_data(entry) ;
insert_ptr->set_link(previous->link_field) ;
previous->set_link(insert_ptr) ;
}
node* list_search(node* head_ptr, const node::valueType& target)
{
node *cursor ;
for (cursor = head_ptr; cursor != NULL ; cursor)
{
if (target == cursor->data() )
{
return cursor ;
}
}
return NULL ;
}
const node* list_search(const node* head_ptr, const node::valueType& target)
{
const node *cursor ;
for (cursor = head_ptr; cursor != NULL ; cursor)
{
if (target == cursor->data() )
{
return cursor ;
}
}
return NULL ;
}
node* list_locate(node* head_ptr, std::size_t position)
{
node * cursor = head_ptr ;
for (int i =0; (i < position) &&(cursor != NULL); i++)
{
cursor = cursor->link_field ;
}
return cursor ;
}
const node* list_locate(const node* head_ptr, std::size_t position)
{
const node * cursor = head_ptr ;
for (int i =0; (i < position) &&(cursor != NULL); i++)
{
cursor = cursor->link_field ;
}
return cursor ;
}
void list_head_remove( node*& head_ptr)
{
node *remove_ptr ;
remove_ptr = head_ptr ;
head_ptr = head_ptr->link_field ;
delete remove_ptr ;
}
void list_clear(node*& head_ptr )
{
while (head_ptr != NULL)
{
list_head_remove(head_ptr) ;
}
}
void list_copy(const node* source_ptr, node*& head_ptr, node*& 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_field ;
while (source_ptr != NULL)
{
list_insert(tail_ptr , source_ptr->data( ));
tail_ptr = tail_ptr->link_field ;
source_ptr = source_ptr->link_field ;
}
}
相关文章推荐
- 在单链表的基础上用c++实现的链栈,并使用进制转换,表达式求值两个小程序来测试
- C++中用模板类(结点类,链表类)实现的单链表的合并操作!
- 单链表的逆置-C++实现
- 数据结构c++实现----单链表
- 单链表的逆置-C++实现
- 单链表的C++代码实现
- 单链表的逆置-C++实现
- C++实现单链表常用功能(不带头结点)
- 数据结构之单链表实现队列C++
- 单链表基础操作C++实现
- 单链表的插入和删除 C++实现
- 单链表c++简单模板实现
- C++模版编程——单链表的实现
- C++ 单链表实现
- 一个单链表C++简单的实现版本-转自chinaunix
- 数据结构之单链表 C++ 实现
- C++单链表的实现
- C++实现单链表操作
- 单链表之C++实现
- C++实现单链表的逆转