#include "CircularLinkedList.cpp"
#include <iostream>
using namespace std;
int main()
{
int r[5] = {1, 2, 3, 4, 5};
CircularLinkedList<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 _CIRCULARLINKEDLIST_H
#define _CIRCULARLINKEDLIST_H 1
template <class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};
template <class DataType>
class CircularLinkedList
{
public:
CircularLinkedList();
CircularLinkedList(DataType a[], int n);
~CircularLinkedList();
int Locate(DataType x);
void Insert(int i, DataType x);
DataType Delete(int i);
void PrintList();
int Length();
private:
Node<DataType> *first;
int length;
};
#endif
#include "CircularLinkedList.h"
#include <iostream>
using namespace std;
template <typename DataType>
CircularLinkedList<DataType>::CircularLinkedList()
{
first = new Node<DataType>;
first->next = NULL;
length = 0;
}
template <typename DataType>
CircularLinkedList<DataType>::CircularLinkedList(DataType a[], int n)
{
first = new Node<DataType>;
first->next = NULL;
Node<DataType> *p = first, *s;
for (int i = 0; i < n; i++)
s = new Node<DataType>{a[i], NULL}, p->next = s, p = s;
p->next = first->next;
length = n;
}
template <typename DataType>
CircularLinkedList<DataType>::~CircularLinkedList()
{
if (first->next != NULL)
{
Node<DataType> *p = first->next, *s;
do
{
s = p->next;
delete p;
p = s;
} while (p != first->next);
}
delete first;
}
template <typename DataType>
int CircularLinkedList<DataType>::Locate(DataType x)
{
if (first->next != NULL)
{
int cnt = 0;
Node<DataType> *p = first->next, *s;
do
{
cnt++;
if (p->data == x)
return cnt;
p = p->next;
} while (p != first->next);
}
return 0;
}
template <typename DataType>
void CircularLinkedList<DataType>::Insert(int i, DataType x)
{
if (i <= 0)
throw "位置";
int k = i % length;
if (k <= 1)
k += length;
Node<DataType> *p = first, *pre = NULL;
for (int j = 0; j < k; j++)
pre = p, p = p->next;
Node<DataType> *s = new Node<DataType>{x, NULL};
s->next = p, pre->next = s;
length++;
}
template <typename DataType>
DataType CircularLinkedList<DataType>::Delete(int i)
{
if (i <= 0)
throw "位置";
int k = i % length;
if (k <= 1)
k += length;
Node<DataType> *p = first, *pre = NULL;
for (int j = 0; j < k; j++)
pre = p, p = p->next;
DataType ans = p->data;
pre->next = p->next;
if (p == first->next)
first->next = p->next;
delete p;
length--;
return ans;
}
template <typename DataType>
void CircularLinkedList<DataType>::PrintList()
{
if (first->next != NULL)
{
Node<DataType> *p = first->next;
do
{
cout << p->data << " ";
p = p->next;
} while (p != first->next);
}
cout << endl;
}
template <typename DataType>
int CircularLinkedList<DataType>::Length() { return length; }