您的位置:首页 > 编程语言 > C语言/C++

单链表(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 ;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 单链表 c++