#include "DoublyCircularLinkedList.cpp"
#include <iostream>
using namespace std;
int main()
{
int r[5] = {1, 2, 3, 4, 5};
DoublyCircularLinkedList<int> L(r, 5);
cout << "链表长度:" << L.Length() << endl;
cout << "执行插入操作前数据为:" << endl;
L.PrintList();
try
{
L.Insert(2, 3);
}
catch (const char *s)
{
cout << s << endl;
}
cout << "执行插入操作后数据为:" << endl;
L.PrintList();
cout << "值为5的元素位置为:";
cout << L.Locate(5) << endl;
cout << "执行删除操作前数据为:" << endl;
L.PrintList();
try
{
L.Delete(1);
}
catch (const char *s)
{
cout << s << endl;
}
cout << "执行删除操作后数据为:" << endl;
L.PrintList();
return 0;
}
#ifndef _DOUBLYCIRCULARLINKEDLIST_H
#define _DOUBLYCIRCULARLINKEDLIST_H 1
template <class DataType>
struct Node
{
DataType data;
Node<DataType> *prev, *next;
};
template <class DataType>
class DoublyCircularLinkedList
{
public:
DoublyCircularLinkedList();
DoublyCircularLinkedList(DataType a[], int n);
~DoublyCircularLinkedList();
int Locate(DataType x);
void Insert(int i, DataType x);
DataType Delete(int i);
void PrintList();
int Length();
private:
Node<DataType> *first, *last;
int length;
};
#endif
#include "DoublyCircularLinkedList.h"
#include <iostream>
using namespace std;
template <typename DataType>
DoublyCircularLinkedList<DataType>::DoublyCircularLinkedList()
{
first = new Node<DataType>, last = new Node<DataType>;
first->prev = last->next = NULL;
first->next = last;
last->prev = first;
length = 0;
}
template <typename DataType>
DoublyCircularLinkedList<DataType>::DoublyCircularLinkedList(DataType a[], int n)
{
if (n < 0)
throw "length < 0";
first = new Node<DataType>, last = new Node<DataType>;
first->prev = last->next = NULL;
first->next = last;
last->prev = first;
Node<DataType> *p = first, *s;
for (int i = 0; i < n; i++)
s = new Node<DataType>{a[i], p, NULL}, p->next = s, p = s;
p->next = last;
last->prev = p;
first->next->prev = last->prev;
last->prev->next = first->next;
length = n;
}
template <typename DataType>
DoublyCircularLinkedList<DataType>::~DoublyCircularLinkedList()
{
if (!length)
{
delete first, last;
return;
}
for (Node<DataType> *p = first->next, *pre = first;; pre = p, p = p->next)
{
delete pre;
if (p == last->prev)
break;
}
delete last;
}
template <typename DataType>
int DoublyCircularLinkedList<DataType>::Locate(DataType x)
{
Node<DataType> *p = first->next;
for (int i = 1; i <= length; i++, p = p->next)
if (p->data == x)
return i;
return 0;
}
template <typename DataType>
void DoublyCircularLinkedList<DataType>::Insert(int i, DataType x)
{
if (i <= 0)
throw "位置";
if (!length)
{
Node<DataType> *p = new Node<DataType>{x, NULL, NULL};
p->prev = p->next = p;
first->next = last->prev = p;
length = 1;
return;
}
i %= length;
if (!i)
i = length;
Node<DataType> *p;
if (i <= length / 2)
{
p = last->prev;
for (int j = 1; j <= i; j++)
p = p->next;
}
else
{
p = first->next;
for (int j = length; j >= i; j--)
p = p->prev;
}
Node<DataType> *s = new Node<DataType>{x, p->prev, p};
p->prev = p->prev->next = s;
length++;
if (i == 1)
first->next = s;
if (i == length)
last->prev = s;
}
template <typename DataType>
DataType DoublyCircularLinkedList<DataType>::Delete(int i)
{
if (i <= 0)
throw "位置";
if (!length)
throw "表空";
i %= length;
if (!i)
i = length;
Node<DataType> *p;
if (i <= length / 2)
{
p = last->prev;
for (int j = 1; j <= i; j++)
p = p->next;
}
else
{
p = first->next;
for (int j = length; j >= i; j--)
p = p->prev;
}
DataType ans = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
if (i == 1)
first->next = p->next;
if (i == length)
last->prev = p->prev;
delete p;
length--;
return ans;
}
template <typename DataType>
void DoublyCircularLinkedList<DataType>::PrintList()
{
if (!length)
{
cout << "\n";
return;
}
for (Node<DataType> *p = first->next;; p = p->next)
{
cout << p->data << " ";
if (p == last->prev)
break;
}
cout << "\n";
}
template <typename DataType>
int DoublyCircularLinkedList<DataType>::Length() { return length; }