dlist.h
#ifndef TEMPLATE_DLIST_H_
#define TEMPLATE_DLIST_H_
#include <iostream>
using std::cout;
using std::endl;
namespace dlist
{
template<typename T>
struct Node
{
T key;
Node<T> *prev;
Node<T> *next;
};
template<typename T>
class DList
{
public:
DList();
~DList();
void EnDList(T x);
void DeDList(T x);
Node<T> * SearchDList(T x);
void DisplayDList();
private:
Node<T> *head;
};
template<typename T>
DList<T>::DList()
{
head = new Node<T>;
if (NULL != head)
head->prev = head->next = NULL;
}
template<typename T>
DList<T>::~DList()
{
Node<T> *tmp = head->next;
Node<T> *p = NULL;
while (NULL != tmp)
{
p = tmp->next;
delete tmp;
tmp = p;
}
if (NULL != head)
{
delete head;
head = NULL;
}
}
template<typename T>
void DList<T>::EnDList(T x)
{
Node<T> *tmp = new Node<T>;
if (NULL == tmp)
return;
tmp->key = x;
Node<T> *p = head->next;
if (NULL == p)
{
head->next = tmp;
tmp->prev = head;
tmp->next = NULL;
}
else
{
head->next = tmp;
tmp->prev = head;
p->prev = tmp;
tmp->next = p;
}
}
template<typename T>
void DList<T>::DeDList(T x)
{
Node<T> *dst = SearchDList(x);
if (NULL == dst)
return;
Node<T> *p = dst->prev;
Node<T> *r = dst->next;
p->next = r;
r->prev = p;
delete dst;
dst = NULL;
}
template<typename T>
Node<T> * DList<T>::SearchDList(T x)
{
Node<T> *tmp = head->next;
while (NULL != tmp)
{
if (x == tmp->key)
break;
tmp = tmp->next;
}
return tmp;
}
template<typename T>
void DList<T>::DisplayDList()
{
Node<T> *tmp = head->next;
while (NULL != tmp)
{
cout << tmp->key << " ";
tmp = tmp->next;
}
cout << endl;
}
}
#endif
main.cpp
#include "dlist.h"
using namespace dlist;
int main()
{
DList<int> dl;
for (int i = 0; i < 10; ++i)
dl.EnDList(i+10000);
dl.DisplayDList();
dl.DeDList(10003);
dl.DisplayDList();
return 0;
}
#ifndef TEMPLATE_DLIST_H_
#define TEMPLATE_DLIST_H_
#include <iostream>
using std::cout;
using std::endl;
namespace dlist
{
template<typename T>
struct Node
{
T key;
Node<T> *prev;
Node<T> *next;
};
template<typename T>
class DList
{
public:
DList();
~DList();
void EnDList(T x);
void DeDList(T x);
Node<T> * SearchDList(T x);
void DisplayDList();
private:
Node<T> *head;
};
template<typename T>
DList<T>::DList()
{
head = new Node<T>;
if (NULL != head)
head->prev = head->next = NULL;
}
template<typename T>
DList<T>::~DList()
{
Node<T> *tmp = head->next;
Node<T> *p = NULL;
while (NULL != tmp)
{
p = tmp->next;
delete tmp;
tmp = p;
}
if (NULL != head)
{
delete head;
head = NULL;
}
}
template<typename T>
void DList<T>::EnDList(T x)
{
Node<T> *tmp = new Node<T>;
if (NULL == tmp)
return;
tmp->key = x;
Node<T> *p = head->next;
if (NULL == p)
{
head->next = tmp;
tmp->prev = head;
tmp->next = NULL;
}
else
{
head->next = tmp;
tmp->prev = head;
p->prev = tmp;
tmp->next = p;
}
}
template<typename T>
void DList<T>::DeDList(T x)
{
Node<T> *dst = SearchDList(x);
if (NULL == dst)
return;
Node<T> *p = dst->prev;
Node<T> *r = dst->next;
p->next = r;
r->prev = p;
delete dst;
dst = NULL;
}
template<typename T>
Node<T> * DList<T>::SearchDList(T x)
{
Node<T> *tmp = head->next;
while (NULL != tmp)
{
if (x == tmp->key)
break;
tmp = tmp->next;
}
return tmp;
}
template<typename T>
void DList<T>::DisplayDList()
{
Node<T> *tmp = head->next;
while (NULL != tmp)
{
cout << tmp->key << " ";
tmp = tmp->next;
}
cout << endl;
}
}
#endif
main.cpp
#include "dlist.h"
using namespace dlist;
int main()
{
DList<int> dl;
for (int i = 0; i < 10; ++i)
dl.EnDList(i+10000);
dl.DisplayDList();
dl.DeDList(10003);
dl.DisplayDList();
return 0;
}