具体概念可以参考数据结构与算法等诸多书籍
1.双向循环链表基本操作的实现
.hpp
#pragma once #includeusing namespace std; const int ERROR = 0; const int OK = 1; typedef int ElemType; class Lnode//定义结点 { public: ElemType data; Lnode* prior; Lnode* next; }; class MyList//定义双向循环列表 { public: MyList(); ~MyList(); void list_Init();//创建双向循环链表 Lnode* list_search(ElemType x); //查找元素x,返回所在一个指向该位置的指针 Lnode* list_search_pos(int pos);//查找pos位置上的元素x,返回x的指针 bool list_insert(ElemType record, int pos); //插入元素x到指定位置pos bool list_delete(ElemType x); //删除元素x void traverse(); //遍历链表 void list_destory(); void list_combine(MyList &L); private: Lnode* head; Lnode* tail; }; MyList::MyList() { this->head = new Lnode; head->data = NULL; head->next = this->head; head->prior = this->head; this->tail = this->head; } void MyList::list_Init()//认为插入5个元素 { cout << "尾插法创建循环双向链表" << endl;//尾插法一定要创建一个尾指针,别让头指针跑掉 for (int i = 0; i < 5; i++) { Lnode* temp; temp = new Lnode; cout << "输入数据域:"; cin >> temp->data; temp->prior = this->tail; temp->next = this->head; this->head->prior = temp; tail->next = temp; tail = temp; } } Lnode * MyList::list_search(ElemType x) { if (head == tail) { return NULL; } Lnode* temp; temp = this->head->next; while (temp!= this->head) { if (temp->data == x) return temp; else temp = temp->next; } if (temp == this->head) return NULL; } Lnode* MyList::list_search_pos(int pos) { if (head == tail) { return NULL; } Lnode* temp; temp = this->head->next; int num = 1; while ( temp!= this->head) { if (num == pos) return temp; else { num++; temp = temp->next; } } if (temp == this->head) return NULL; } bool MyList::list_insert(ElemType record, int pos) { Lnode* p; p=this->list_search_pos(pos); if (!p) return false; Lnode* temp; temp = new Lnode; temp->data = record; temp->prior = p->prior; temp->next = p; p->prior->next = temp; p->prior = temp; return true; } bool MyList::list_delete(ElemType x) { Lnode* p; p = this->list_search(x); if (!p) return false; if (p == this->tail)//如果说要删除的是尾指针处的结点 一定要把尾指针赋给前一个结点,同时更改头结点的prior域,指向新的为节点 { this->tail = p->prior; this->head->prior = this->tail; } p->prior->next = p->next; p->next->prior = p->prior; delete p; return true; } void MyList::traverse() { Lnode* p; p = this->head->next; while (p!=this->head) { cout << p->data << "t"; p = p->next; } cout << endl; } void MyList::list_destory() { Lnode* p; p = this->tail; while (this->tail != this->head) { this->tail = this->tail->prior; delete p; p = this->tail; } this->head->next = this->head; } void MyList::list_combine(MyList& L) { Lnode* p1; Lnode* p2; p1 = this->head; p2 = L.head; p1->prior->next = p2->next; p2->next->prior = p1->prior; p1->prior = p2->prior; p2->prior->next = p1; cout <<"新链表的最后一个结点数据:" << p1->prior->data << endl; cout << "this链表的最后一个结点数据:" << p2->next->prior->data << endl; this->tail = L.tail; L.tail = NULL; L.head = NULL; cout << "尾指针的指向:" << this->tail->data << endl; delete p2; } MyList::~MyList() { this->list_destory(); if (head) { delete this->head; this->head = NULL; this->tail = NULL; } }
.cpp
#includeusing namespace std; #include "循环链表和双向链表.hpp" void test1()//基本操作 { MyList L; L.list_Init();//1 2 3 4 5 bool result; result = L.list_insert(999, 5); cout << boolalpha << result << endl; if (result) L.traverse(); result = L.list_delete(5); if (result) L.traverse(); } void test2()//两个双向循环链表的合并(不利用尾指针) { MyList L1, L2; L1.list_Init(); L2.list_Init(); L1.list_combine(L2); L1.traverse(); } int main() { test1(); system("pause"); return 0; }



