C++ 单链表实现

#ifndef _SINGLE_LINK_LIST_
#define _SINGLE_LINK_LIST_


template<class T>
class SingleLinkListNode
{
template<class T> friend class SingleLinkList;
public:
typedef SingleLinkListNode<T>* PNode;


SingleLinkListNode(T value, PNode next) :m_value(value), m_next(next){}
~SingleLinkListNode()
{
m_next = NULL;
}


private:
T m_value;
PNode m_next;
};


template<class T>
class SingleLinkList
{
public:
typedef SingleLinkListNode<T>* iterator;
typedef long size_type;
typedef void(*PTraverseFun)(T value);
public:
SingleLinkList() :m_size(0), m_head(NULL), m_tail(NULL){}
~SingleLinkList(){ clear(); };


bool empty(){ return m_size == 0; }
size_type size(){ return m_size; }
iterator begin(){ return m_head; }
iterator end(){ return m_tail; }
void clear()
{
if (m_size == 0)
return;


iterator node = m_head;
while (node != m_tail)
{
iterator it = node->m_next;
delete node;
node = it;
}


m_size = 0;
m_head = NULL;
m_tail = NULL;
}


//add a element to list head
iterator push_front(T value)
{
iterator node = new SingleLinkListNode<T>(value, NULL);
node->m_next = m_head;
m_head = node;
++m_size;


return node;
}
//add a element to list,after the pos
//if pos == null then add element fail, return null
iterator insert_back(T value, iterator pos)
{
if (pos == NULL)
return NULL;


iterator node = new SingleLinkListNode<T>(value, NULL);
node->m_next = pos->m_next;
pos->m_next = node;


++m_size;
return node;
}
//add a element to list,after the pos
//if pos == null or pos invalid then add element fail, return null
iterator insert_back_safe(T value, const iterator pos)
{
if (pos == NULL || m_size == 0)
return NULL;


iterator hit_node = NULL;
iterator itr = m_head;
do 
{
if (itr == pos)
{
hit_node = itr;
break;
}

itr = itr->m_next;
} while (itr != m_tail);

if (hit_node == NULL)
return NULL;


iterator node = new SingleLinkListNode<T>(value, NULL);
node->m_next = hit_node->m_next;
hit_node->m_next = node;


++m_size;
return node;
}
//add a element to list,front the pos
//if pos == null or pos invalid then add element fail, return null
iterator insert_front(T value, const iterator pos)
{
if (pos == NULL || m_size == 0)
return NULL;


iterator pre_node = NULL;
iterator hit_node = NULL;
iterator itr = m_head;
do
{
if (pos == itr)
{
hit_node = itr;
break;
}
else
{
pre_node = itr;
}
itr = itr->m_next;
}while (itr != m_tail);


if (hit_node == NULL)
return NULL;


iterator node = new SingleLinkListNode<T>(value, NULL);
if (pre_node == NULL)
{
node->m_next = m_head;
m_head = node;
}
else
{
node->m_next = hit_node;
pre_node->m_next = node;
}


++m_size;
return node;
}
//remove element from list,will delete node
//if remove_all == true then remove all element where node.m_value == value
//if remove_all == false then remove first element where node.m_value == value from head
void remove(const T value, bool remove_all = false)
{
if (m_size == 0)
return;


iterator pre_node = NULL;
iterator itr = m_head;
do 
{
if (value == itr->m_value)
{
if (pre_node == NULL)
{
m_head = itr->m_next;
}
else
{
pre_node->m_next = itr->m_next;
}
delete itr;
itr = pre_node == NULL ? m_head : pre_node->m_next;
--m_size;


if (!remove_all)
break;
}
else
{
pre_node = itr;
itr = itr->m_next;
}
} while (itr != m_tail);
}
//remove the pos element from list,will delete the node
void remove(const iterator pos)
{
if (pos == NULL || m_size == 0)
return;


iterator pre_node = NULL;
iterator hit_node = NULL;
iterator itr = m_head;
do 
{
if (pos == itr)
{
hit_node = itr;
break;
}
else
{
pre_node = itr;
}
itr = itr->m_next;
} while (itr != m_tail);


if (hit_node == NULL)
return;


if (pre_node == NULL)
{
m_head = hit_node->m_next;
delete hit_node;
hit_node = NULL;
}
else
{
pre_node->m_next = hit_node->m_next;
delete hit_node;
hit_node = NULL;
}


--m_size;
}
//find element from list,where node.m_value == value
//return the first iterator where node.m_value == value from pos
//if pos == null then find element from head
iterator find(const T value, const iterator pos = NULL)
{
if (m_size == 0)
return m_tail;


iterator itr = pos == NULL ? m_head : pos;
do 
{
if (value == itr->m_value)
break;


itr = itr->m_next;
} while (itr != m_tail);


return itr;
}
//traverse the list
void traverse(PTraverseFun fun)
{
if (m_size == 0)
return;


iterator itr = m_head;
do
{
fun(itr->m_value);
itr = itr->m_next;
} while (itr != m_tail);
}
private:
size_type m_size;
iterator m_head;
iterator m_tail;
};
#endif



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值