c++双向链表的实现

namespace ylk
{

	template<class T>
	class Node
	{
		public:
			Node()
			{
				this->front = NULL;
				this->next = NULL;
			}
			Node(T data)
			{
				this->next = NULL;
				this->front = NULL;
				this->data = data;
			}
			Node(Node& n)
			{
				this->front = NULL;
				this->next = NULL;
				this->data = n.data;
			}
		public:
			Node<T>* front;
			Node<T>* next;
			T data;
	};
}



#include <string.h>
#include <iostream>
#include "node.h"
using namespace std;
using namespace ylk;

bool is_odd(int value)
{
	return value%2;
}



template<class T>
class Link
{
public:
	Link();
	Link(Link& l);
	bool insertNode(T data);
	bool deleteNode(T data);
	Link<T>& merge(Link& l);
	void reverse();
	void show();
	void remove_if(bool (*fun)(T));
	void swap(T& a,T& b);
private:
	Node<T>* head;
	Node<T>* tail;
};


template<class T>
void Link<T>::swap(T& a,T& b)
{
	T temp = a;
	a = b;
	b = temp;
}



template<class T>
void Link<T>::reverse()
{
	if(NULL == this->head)
	{
		return;
	}
	Node<T>* p = this->head;
	
	int count = 1;
	while(NULL != p)
	{
		p = p->next;
		count++;
	}
	int i = 1;
	p = this->head;
	Node<T>*q = this->tail;
	while(i<=(count/2))
	{
		swap(p->data,q->data);	
		p = p->next;
		q = q->front;
		i++;
	}

}


template<class T>
Link<T>& Link<T>::merge(Link& l)
{
	Node<T>* p = l.head;
	while(NULL != p)
	{
		this->insertNode(p->data);
		p = p->next;
	}
	return *this;
}



template<class T>
void Link<T>::remove_if(bool (*fun)(T))
{
	Node<T>* p = this->head;
	Node<T>* q = NULL;
	while(NULL != p)
	{
		if(true==fun(p->data))
		{
			if(this->head->data == p->data)
			{
				this->head = this->head->next;
			}
			else if(this->tail->data == p->data)
			{
				this->tail = this->tail->front;
			}
			if(NULL != p->front)
				p->front->next = p->next;
			if(NULL != p->next)
				p->next->front = p->front;
			q = p->next;
			delete p;
			p = q;
			continue;
		}
		p = p->next;
	}
}


template<class T>
Link<T>::Link()
{
	this->head = NULL;
	this->tail = NULL;
}

template<class T>
Link<T>::Link(Link& l)
{
	this->head = NULL;
	this->tail = NULL;
	Node<T>* q = l.head;
	while(NULL != q)
	{
		this->insertNode(q->data);
		q = q->next;
	}
}


template<class T>
void Link<T>::show()
{
	Node<T>* p = this->head;
	while(NULL != p)
	{
		cout<<p->data<<" ";
		p = p->next;
	}
	cout<<endl;
}

template<class T>
bool Link<T>::insertNode(T data)
{
	Node<T>* pNew = new Node<T>(data);

	if(NULL == pNew)
	{
		cerr<<"new fail";
		return false;
	}	

	if(NULL==this->head)
	{
		this->head = pNew;
	}
	else
	{
		this->tail->next = pNew;
		pNew->front = this->tail;
	}
	this->tail = pNew;
	return true;
}

template<class T>
bool Link<T>::deleteNode(T data)
{
	Node<T>* q = NULL;
	Node<T>* p = this->head;
	if(data == this->head->data)
	{
		if(NULL == this->head->next)
		{
			this->head = NULL;
			this->tail = NULL;
		}
		else
		{
			
			this->head = this->head->next;	
			this->head->front = NULL;
		}
		delete p;
		return true;
	}
	while(NULL != p&& p->data != data)
	{
		q = p;
		p = p->next;
	}
	if(NULL == p)
		return false;
	else
	{
		if(this->tail == p)
		{
			this->tail = this->tail->front;
			this->tail->next = NULL;
		}
		else
		{
			q->next = p->next;
			p->next->front = q;
		}
		delete p;
	}
	return true;
}


int main()
{
	Link<int> l;
	l.insertNode(1);
	l.insertNode(2);
	
	l.insertNode(3);
	l.insertNode(4);
	
		
	l.show();
/*		
	l.deleteNode(2);
	l.deleteNode(1);
	l.deleteNode(4);		
*/	
	l.reverse();
	l.show();		

	Link<int> l2(l);
	l2.show();

	l.merge(l2);
	l.show();

	l.remove_if(is_odd);
	l.show();

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值