基于链表的容器类bag实现

链表类头文件:
#ifndef MAIN_WTF_NODE1_H
#define MAIN_WTF_NODE1_H
#include <cstdlib>


namespace main_wtf_5
{
class node
{
public:
typedef double value_type;


node(const value_type& init_data = value_type(),node* init_link = NULL)
{
data_field = init_data;
link_field = init_link;
}


void set_data(const value_type& new_data)
{
data_field = new_data;
}


void set_link(node* new_link)
{
link_field = new_link;
}


value_type data() const
{
return data_field;
}


const node* link() const
{
return link_field;
}


node* link()
{
return link_field;
}


private:
value_type data_field;
node* link_field;
};


std::size_t list_length(const node* head_ptr);
void list_head_insert(node*& head_ptr,const node::value_type& entry);
void list_insert(node*& previous,const node::value_type& entry);
node* list_search(node* head_ptr,const node::value_type& target);
const node* list_search(const node* head_ptr,const node::value_type& target);
void list_head_remove(node*& head_ptr);
void list_remove(node* previous_ptr);
void list_clear(node*& head_ptr);
const node* list_locate(const node* head_ptr,std::size_t position);
void list_copy(const node* source_ptr,node*& head_ptr,node*& tail_ptr);




}


#endif


链表类实现文件:

#include"node1.h"


namespace main_wtf_5
{


std::size_t list_length(const node* head_ptr)
{
size_t answer = 0;
const node* cursor;
for(cursor = head_ptr; cursor != NULL; cursor = cursor->link())
{
++answer;
}
return answer;
}


void list_head_insert(node*& head_ptr,const node::value_type& entry)
{
head_ptr = new node(entry,head_ptr);
}




void list_insert(node*& previous,const node::value_type& entry)
{
node* insert_ptr;


insert_ptr = new node;
insert_ptr->set_data(entry);
insert_ptr->set_link(previous->link());
previous->set_link(insert_ptr);


}


node* list_search(node* head_ptr,const node::value_type& target)
{
node* cursor;
for(cursor = head_ptr;cursor != NULL;cursor = cursor->link())
if(target == cursor->data())
return cursor;
return NULL;
}


const node* list_search(const node* head_ptr,const node::value_type& target)
{
const node* cursor;
for(cursor = head_ptr;cursor != NULL;cursor = cursor->link())
if(target == cursor->data())
return cursor;
return NULL;
}


void list_head_remove(node*& head_ptr)
{
node* remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link();
delete remove_ptr;
}


void list_remove(node* previous_ptr)
{
node* remove_ptr;
remove_ptr = previous_ptr->link();
previous_ptr->set_link(remove_ptr->link());
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();
while(source_ptr != NULL)
{
list_insert(tail_ptr,source_ptr->data());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
}


const node* list_locate(const node* head_ptr,std::size_t position)
{


const node* cursor;
cursor = head_ptr;


for(int i=1;(i<position) && (cursor != NULL);i++)
cursor = cursor->link();
return cursor;
}


}


bag类头文件:

#ifndef MAIN_WTF_BAG3_H
#define MAIN_WTF_BAG3_H
#include "node1.h"
namespace main_wtf_5
{
class bag
{
public:
typedef std::size_t size_type;
typedef node::value_type value_type;
bag():head_ptr(NULL),many_nodes(0){};
bag(const bag& source);
~bag();
size_type erase(const value_type& target);
bool erase_one(const value_type& target);
void insert(const value_type& entry);
void operator +=(const bag& addend);
void operator = (const bag& source);
size_type size() const { return many_nodes; }
size_type count(const value_type& target) const;
value_type grab() const;
private:
node* head_ptr;
size_type many_nodes;


};
bag operator +(const bag& b1,const bag& b2);
}



#endif



bag类实现文件:
#include"bag3.h"
#include<cassert>


namespace main_wtf_5
{
bag::bag(const bag& source)
{
node* tail_ptr;
list_copy(source.head_ptr,head_ptr,tail_ptr);
many_nodes = source.many_nodes;
}


void bag::operator=(const bag& 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;
}


bag::~bag()
{
list_clear(head_ptr);
many_nodes = 0;
}


bool bag::erase_one(const value_type& target)
{
node* 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;
}


bag::size_type bag::erase(const value_type& target)
{
bag::size_type answer = 0;
while(erase_one(target))
answer++;
return answer;
}


void bag::insert(const value_type& entry)
{
list_head_insert(head_ptr,entry);
many_nodes++;
}


void bag::operator+=(const bag& 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;
}


bag::size_type bag::count(const value_type& target) const
{
size_type answer;
const node* cursor;


answer = 0;
cursor = list_search(cursor,target);
while(cursor != NULL)
{
++answer;
cursor = cursor->link();
cursor = list_search(cursor,target);
}
return answer;
}


bag::value_type bag::grab() const
{
size_type i;
const node* cursor;


assert(size()>0);
i = (rand() % size()) +1;
cursor = list_locate(head_ptr,i);
return cursor->data();
}


bag operator +(const bag& b1,const bag& b2)
{
bag answer;
answer += b1;
answer += b2;
return answer;
}
}

猜你喜欢

转载自blog.csdn.net/u014080185/article/details/51997845