环境配置
codeblocks
windows10
项目结构
代码
CDLinkListClass.h
#ifndef _CDLINKLISTCLASS_H_
#define _CDLINKLISTCLASS_H_
#include <iostream>
template <typename T>
class DLinkListNode
{
public:
T data;
DLinkListNode<T> *prior;
DLinkListNode<T> *next;
};
template <typename T>
class CDLinkListClass
{
public:
CDLinkListClass(void);
~CDLinkListClass(void);
bool CreateListF(const T *a, int n) const;
bool CreateListR(const T *a, int n);
void Display(void) const;
int ListLength(void) const;
bool GetElem(int i, T &e) const;
bool LocateElem(const T &e, int &i) const;
bool ListInsert(int i, const T &e) const;
bool ListDelete(int i) const;
protected:
private:
DLinkListNode<T> *rear;
};
template <typename T>
CDLinkListClass<T>::CDLinkListClass(void)
{
rear = new DLinkListNode<T>;
rear->prior = rear;
rear->next = rear;
}
template <typename T>
CDLinkListClass<T>::~CDLinkListClass(void)
{
DLinkListNode<T> *p = rear;
DLinkListNode<T> *q = rear->next;
while (rear != q)
{
delete p;
p = q;
q = q->next;
}
delete p;
}
template <typename T>
bool CDLinkListClass<T>::CreateListF(const T *a, int n) const
{
if (n < 0)
{
return false;
}
DLinkListNode<T> *p;
for (int i = 0; i < n; ++i)
{
p = new DLinkListNode<T>;
p->data = a[i];
p->next = rear->next;
p->next->prior = p;
rear->next = p;
p->prior = rear;
}
return true;
}
template <typename T>
bool CDLinkListClass<T>::CreateListR(const T *a, int n)
{
if (n < 0)
{
return false;
}
DLinkListNode<T> *p = rear; // p向后移动, rear指向首节点
for (int i = 0; i < n; ++i)
{
p->data = a[i];
p->next = new DLinkListNode<T>;
p->next->prior = p;
p = p->next;
}
p->next = rear; // 尾节点和首节点连接
rear->prior = p;
rear = p; // 修改尾指针指向尾节点
return true;
}
template <typename T>
void CDLinkListClass<T>::Display(void) const
{
DLinkListNode<T> *p = rear->next;
while (rear != p)
{
std::cout << p->data << " ";
p = p->next;
}
std::cout << std::endl;
}
template <typename T>
int CDLinkListClass<T>::ListLength(void) const
{
DLinkListNode<T> *p = rear->next;
int i = 0;
while (rear != p)
{
++i;
p = p->next;
}
return i;
}
template <typename T>
bool CDLinkListClass<T>::GetElem(int i, T &e) const
{
if (i < 1)
{
return false;
}
DLinkListNode<T> *p = rear->next;
int j = 1;
while (rear != p && j < i)
{
p = p->next;
++j;
}
if (rear == p)
{
return false;
}
else
{
e = p->data;
return true;
}
}
template <typename T>
bool CDLinkListClass<T>::LocateElem(const T &e, int &i) const
{
DLinkListNode<T> *p = rear->next;
int j = 1;
while (rear != p && e != p->data)
{
p = p->next;
++j;
}
if (rear == p)
{
return false;
}
else
{
i = j;
return true;
}
}
template <typename T>
bool CDLinkListClass<T>::ListInsert(int i, const T &e) const
{
DLinkListNode<T> *p = rear->next; // 前指针
DLinkListNode<T> *q; // 扩展节点
int j = 2; // 插入位置
if (i < 1)
{
return false;
}
else if (i == 1) // 所有循环链表位置1的插入都需要单独处理
{
q = new DLinkListNode<T>;
q->data = e;
q->next = rear->next;
q->next->prior = q;
rear->next = q;
q->prior = rear;
return true;
}
while (rear != p && j < i)
{
p = p->next;
++j;
}
if (rear == p)
{
return false;
}
else
{
q = new DLinkListNode<T>;
q->data = e;
q->next = p->next;
q->next->prior = q;
p->next = q;
q->prior = p;
return true;
}
}
template <typename T>
bool CDLinkListClass<T>::ListDelete(int i) const
{
if (i < 1)
{
return false;
}
DLinkListNode<T> *p = rear; // 前指针
DLinkListNode<T> *q; // 删除节点
int j = 1; // 删除位置
while (rear != p->next && j < i) // 不能删除尾节点
{
p = p->next;
++j;
}
if (rear == p->next)
{
return false;
}
else
{
q = p->next;
p->next = q->next;
p->next->prior = p;
delete q;
return true;
}
}
#endif // _CDLINKLISTCLASS_H_
main.cpp
#include <iostream>
#include "CDLinkListClass.h"
using namespace std;
int main(void)
{
CDLinkListClass<int> L1;
CDLinkListClass<int> L2;
const int n = 10;
int a[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i = 1;
int e;
L1.CreateListF(a, n);
L2.CreateListR(a, n);
cout << "L1: " << endl;
L1.Display();
cout << "L2: " << endl;
L2.Display();
L1.GetElem(i, e);
cout << "L1: i = 1, e = " << e << endl;
L2.LocateElem(e, i);
cout << "L2: e = " << e << ", i = " << i << endl;
L1.ListInsert(11, 0);
L1.ListDelete(1);
L2.ListInsert(1, 0);
L2.ListDelete(11);
cout << "L1: " << endl;
L1.Display();
cout << "L2: " << endl;
L2.Display();
cout << "L1.length = " << L1.ListLength() << endl;
system("PAUSE");
return 0;
}