头文件
#include<iostream>
using namespace std;
template<class DataType>
struct Node
{
public:
DataType data;
Node* next, * prior;
};
template <class DataType>
class CDoubleCircleList
{
public:
CDoubleCircleList();
CDoubleCircleList(DataType a[], int n); //建立n个元素的双链表
~CDoubleCircleList();
void PrintfList(); //遍历操作
void Empty(); //判空
DataType Get(int i); //按位查找
int Locate(DataType x); //按值查找
void Insert(int i, DataType x); //插入操作
DataType Delete(int i); //删除操作
void Modify(int i, DataType x); //修改操作
private:
Node<DataType> *first;
int length;
};
cpp文件
#include "DoubleCircleList.h"
template<class DataType>
CDoubleCircleList<DataType>::~CDoubleCircleList()
{
if (first != NULL)
{
Node<DataType>* p;
p = first->next;
while (p != first)
{
Node<DataType>*temp = p;
p = p->next;
delete temp;
}
delete first;
first = NULL;
}
}
//建立n个元素的双链表
template<class DataType>
CDoubleCircleList<DataType>::CDoubleCircleList(DataType a[], int n)
{
if (n < 0)
cout << "您输入的长度错误!" << endl;
else
{
length = n;
first = new Node<DataType>;
first->next = first; //初始化为循环单链表
Node<DataType>*p, *q;
p = first;
for (int i = 0; i < n; i++)
{
q = new Node<DataType>;
q->data = a[i];
q->next = first;
q->prior = p;
p->next = q;
p = q;
}
}
}
//遍历操作
template<class DataType>
void CDoubleCircleList<DataType>::PrintfList()
{
Node<DataType>* p;
p = first->next;
while(p !=first)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
//判空
template<class DataType>
void CDoubleCircleList<DataType>::Empty()
{
if (first->next == NULL)
cout << "当前链表为空" << endl;
else
cout << "当前链表不为空" << endl;
}
//按位查找
template<class DataType>
DataType CDoubleCircleList<DataType>::Get(int i)
{
Node<DataType>* p = first->next;
int count = 1;
while (p != NULL && count < i)
{
p = p->next;
count++;
}
if (p == NULL)
cout << "查找位置错误" << endl;
else
return p->data;
}
//修改
template<class DataType>
void CDoubleCircleList<DataType>::Modify(int i, DataType x)
{
Node<DataType>* p = first->next;
int count = 1;
while (p != NULL && count < i)
{
p = p->next;
count++;
}
if (p == NULL)
cout << "位置错误" << endl;
else
p->data = x;
}
//按值查找
template<class DataType>
int CDoubleCircleList<DataType>::Locate(DataType x)
{
Node<DataType>* p = first->next;
int count = 1;
while (p != NULL)
{
if (p->data == x)
return count; //查找成功返回序号
p = p->next;
count++;
}
return 0;//退出循环表示查找失败
}
//插入操作
template<class DataType>
void CDoubleCircleList<DataType>::Insert(int i, DataType x)
{
Node<DataType>* p = first, * s = NULL;
int count = 0;
while (p != NULL && count < i - 1) //查找第i-1个结点
{
p = p->next; //工作指针p后移
count++;
}
if (p == NULL)
cout << "插入位置错误" << endl; //没有找到第i-1个节点
else
{
s = new Node<DataType>; //申请节点s,数据域为x
s->data = x;
s->next = p->next;
s->prior = p;
p->next = s;
}
}
//删除操作
template<class DataType>
DataType CDoubleCircleList<DataType>::Delete(int i)
{
DataType x;
Node<DataType>* p = first, * q = NULL;
int count = 0;
while (p != NULL && count < i - 1) //查找第i-1个节点
{
p = p->next;
count++;
}
if (p == NULL || p->next == NULL) //结点p不存在或p的后继结点不存在
cout << "删除位置错误" << endl;
else
{
q = p->next;
x = q->data;
p->next = q->next;
q->next->prior = p;
delete q;
return x;
}
}
int main()
{
int arr[] = { 1, 3, 5, 7, 8, 10}, data;
int size = sizeof(arr) / sizeof(*arr);
CDoubleCircleList<int>list(arr, size);
cout<<endl;
cout << "循环链表为:" << endl;
list.PrintfList();
cout<<endl;
list.Insert(1, 38);
cout << "在 1 号位置插入 数据 38:" << endl;
list.PrintfList();
cout<<endl;
cout << "删除 2 号位置的数据:" << endl;
list.Delete(2);
list.PrintfList();
cout<<endl;
cout << "修改 3 号位置的数据:为100" << endl;
list.Modify(3, 100);
list.PrintfList();
cout<<endl;
cout << "按位查找----4号位的值:" << endl;
data = list.Get(4);
cout << "3 号位置的数据:" <<data<< endl;
cout<<endl;
list.PrintfList();
cout << "按值查找----值 38 位置:" << endl;
data = list.Locate(38);
cout << "该值的位置:" <<data<< endl;
return 0;
}