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;
}