doublyLinkedCircularList.h
#include "iostream"
using namespace std;
typedef struct DuLNode {
int data;
struct DuLNode *prior, *next;
}DuLNode, *DuLinkedCirList;
bool createDuLinkedCirList(DuLinkedCirList &L, int n) {
if (n<1)
{
cout << "输入不合法!" << endl;
return false;
}
L = (DuLinkedCirList)malloc(sizeof(DuLNode));
if (!L)
{
cout << "内存分配失败" << endl;
exit(EXIT_FAILURE);
}
L->next = L->prior = L;
DuLinkedCirList p, q;
p = L;
for (int i = 0; i < n; i++)
{
q = (DuLinkedCirList)malloc(sizeof(DuLNode));
if (!q)
{
cout << "内存分配失败" << endl;
exit(EXIT_FAILURE);
}
cin >> q->data;
p->next = q;
q->prior = p;
L->prior = q;
q->next = L;
p = q;
}
return true;
}
//获取链表长度
int getLinkedLength(DuLinkedCirList &L) {
DuLinkedCirList p = L->next;
int i = 0;
while (p != L)
{
p = p->next;
i++;
}
return i;
}
//输出链表
void printDoublyCircularList(DuLinkedCirList &L) {
if (L->next == L)
{
cout << "链表为空!" << endl;
}
int i;
DuLinkedCirList p;
p = L->next;
for (i = 0; i <getLinkedLength(L); i++)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
//从i个位置后面插入元素
bool insertElem(DuLinkedCirList &L, int i, int a) {
if (i<1 || i>getLinkedLength(L) + 1)
{
cout << "输入不合法!" << endl;
exit(EXIT_FAILURE);
}
DuLinkedCirList p, q;
p = L;
q = (DuLinkedCirList)malloc(sizeof(DuLNode));
q->data = a;
int j;
for (j = 0; j < i; j++)
{
p = p->next; //将p定位到第i个元素
}
q->next = p->next;
p->next->prior = q;
q->prior = p;
p->next = q;
return true;
}
//删除第i个元素
int deleteDoublyCircularElem(DuLinkedCirList &L, int i) {
if (i<1 || i>getLinkedLength(L))
{
cout << "输入不合法!" << endl;
exit(EXIT_FAILURE);
}
DuLinkedCirList p;
p = L;
for (int j = 0; j < i; j++)
{
p = p->next;
}
int e = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
return e;
}
//修改第i个元素的值
bool modifyDoublyCircularElem(DuLinkedCirList &L, int i, int a) {
if (i<1 || i>getLinkedLength(L))
{
cout << "输入不合法!" << endl;
exit(EXIT_FAILURE);
}
DuLinkedCirList p;
p = L;
for (int j = 0; j < i; j++)
{
p = p->next;
}
p->data = a;
return true;
}
//查询值,并返回该值的位置
int searchElem(DuLinkedCirList &L, int e) {
DuLinkedCirList p;
p = L->next;
int i = 1;
while ((p->data != e) && (i <= getLinkedLength(L)))
{
i++;
p = p->next;
}
if (i <= getLinkedLength(L))
{
return i;
}
else
{
cout << "无该值!";
return false;
}
}
//获取链表第i个值
int getElem(DuLinkedCirList &L, int i) {
if (i<1 || i>getLinkedLength(L))
{
cout << "输入不合法!" << endl;
/* exit(EXIT_FAILURE);*/
}
DuLinkedCirList p;
p = L;
int j;
for (j = 0; j < i; j++)
{
p = p->next;
}
return p->data;
}
//销毁链表
bool destroyDoublyLinkedCircularList(DuLinkedCirList &L) {
DuLinkedCirList p, q;
if (L == NULL)
{
return false;
}
p = q = L->next;
while (p != L)
{
p = p->next;
free(q);
q = p;
}
free(L);
return true;
}
//清空链表
bool clearLinkedList(DuLinkedCirList &L) {
DuLinkedCirList p, q;
p = q = L->next;
while (p != L)
{
p = p->next;
free(q);
q = p;
}
L->next = L->prior = L;;
return true;
}
main.cpp
#include "stdafx.h"
#include "doublyLinkedCircularList.h"
int main()
{
DuLinkedCirList list1;
int length, i, elem;
cout << "请输入要创建双向循环链表的长度:";
cin >> length;
createDuLinkedCirList(list1,length);
cout << "当前链表长度为:";
cout << getLinkedLength(list1)<<endl;
cout << "打印当前链表:";
printDoublyCircularList(list1);
cout << "请输入要插入的位置:";
cin >> i;
cout << "请输入要插入的元素值:";
cin >> elem;
insertElem(list1, i, elem);
cout << "打印当前链表:";
printDoublyCircularList(list1);
cout << "请输入要删除的元素的位置:";
cin >> i;
deleteDoublyCircularElem(list1, i);
cout << "打印当前链表:";
printDoublyCircularList(list1);
cout << "请输入要修改的位置:";
cin >> i;
cout << "请输入修改后的元素值:";
cin >> elem;
modifyDoublyCircularElem(list1,i,elem);
cout << "打印当前链表:";
printDoublyCircularList(list1);
cout << "请输入要查询的值:";
cin >> elem;
cout << "该值所在的位置为:";
cout << searchElem(list1, elem)<<endl;
cout << "请输入要获取的元素的位置:";
cin >> i;
cout << "该位置上元素值为:";
cout << getElem(list1, i) << endl;
cout << "清空当前链表"<<endl;
clearLinkedList(list1);
cout << "打印当前链表:";
printDoublyCircularList(list1);
return 0;
}