【c++版数据结构】之循环双链表的实现(带头结点以及尾节点)

来源:互联网 发布:造梦西游1装备数据大全 编辑:程序博客网 时间:2024/06/05 16:51

所实现的循环双链表的结构如下图所示:

循环双链表的实现,和第一篇文章单链表的实现思想大致相同点击打开链接具体思想参考该文章。本篇文章在构建节点的同时,初始化构建节点的前驱和后继,具体细节参考下列代码

头文件:DCList.h

#ifndef DCLIST_H#define DCLIST_H#include<iostream>#include<cstdlib>#include<cassert>using namespace std;typedef enum{FALSE,TRUE}Status;template<class Type>class List;template<class Type>class ListNode{template<class Type>friend class List;private:Type data;ListNode<Type> *prio;ListNode<Type> *next;public:ListNode() :data(Type()),prio(NULL), next(NULL){}ListNode(const Type x, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL):data(x), prio(p), next(n){}Type getData()const { return data; }};template<class Type>class List{private:ListNode<Type> *first;ListNode<Type> *last;size_t          size;public:ListNode<Type>* _BuyNode(const Type x, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL){ListNode<Type> *s = new ListNode<Type>(x, p, n);assert(s != NULL);return s;}List(){ListNode<Type> *s = _BuyNode(0, NULL, NULL);first = last = s;last->next = first;size = 0;}~List(){destory();}Status push_back(const Type &x){ListNode<Type> *s = _BuyNode(x, last, first);last->next = s;last = s;size++;return TRUE;}void show_list(){ListNode<Type> *s = first->next;while (s != first){cout << s->data << "->";s = s->next;}cout << "Nul." << endl;}Status push_front(const Type &x){ListNode<Type> *s = _BuyNode(x, first, first->next);if (size == 0){first->next = s;last = s;last->next = first;}else{first->next->prio = s;first->next = s;}size++;return TRUE;}Status pop_back(){if (size == 0){cout << "循环双链表已空,无法尾删" << endl;return FALSE;}ListNode<Type> *s = last->prio;delete last;last = s;last->next = first;size--;return TRUE;}Status pop_front(){if (size == 0){cout << "循环双链表已空,无法头删" << endl;return FALSE;}ListNode<Type> *s = first->next;if (size == 1){last = first;last->next = first;}else{first->next = s->next;s->next->prio = first;}delete s;size--;return TRUE;}Status insert_val(const Type &x){ListNode<Type> *s = first;while (s->next != first && s->next->data < x){s = s->next;}if (s->next == first)//直接尾插即可{ListNode<Type> *p = _BuyNode(x, last, first);last->next = p;last = p;}else//找到了前驱,在其之后插入即可{ListNode<Type> *p = _BuyNode(x, s, s->next);s->next->prio = p;s->next = p;}size++;return TRUE;}size_t lenth(){return size;}ListNode<Type>* find(const Type &x){ListNode<Type> *s = first->next;while (s != first && s->data != x){s = s->next;}if (s == first)return NULL;elsereturn s;}Status delete_val(const Type &x){ListNode<Type> *s = find(x);if (s == NULL){cout << "该元素不存在,无法删除" << endl;return FALSE;}if (s == last){ListNode<Type> *p = last->prio;delete last;last = p;last->next = first;}else{ListNode<Type> *q = s->next;s->data = q->data;s->next = q->next;q->next->prio = s;delete q;}size--;return TRUE;}void sort(){if (size == 0 || size == 1)return;ListNode<Type> *s = first->next;ListNode<Type> *p = s->next;last = s;last->next = first;while (p != first){s = p;p = p->next;ListNode<Type> *q = first;while (q->next != first && q->next->data < s->data){q = q->next;}if (q->next == first){last->next = s;s->prio = last;last = s;last->next = first;}else{s->next = q->next;q->next->prio = s;q->next = s;s->prio = q;}}}void reverse(){if (size == 0 || size == 1)return;ListNode<Type> *s = first->next;ListNode<Type> *p = s->next;last = s;last->next = first;while (p != first){s = p;p = p->next;s->next = first->next;first->next->prio = s;first->next = s;s->prio = first;}}void clear(){if (size == 0)return;ListNode<Type> *s = first->next;while (s != first){if (size == 1){last = first;last->next = first;}else{first->next = s->next;s->next->prio = first;}delete s;size--;s = first->next;//为下一次删除做准备}}void destory(){clear();delete[] first;first = last = NULL;}ListNode<Type>* next(ListNode<Type> *s){if (s == last)return NULL;elsereturn s->next;}ListNode<Type>* prio(ListNode<Type> *s){if (s == first->next) //-------------->第一个节点没有前驱,返回NULLreturn NULL;elsereturn s->prio;}};#endif
测试文件main.cpp

#include"DCList.h"int main(){List<int> mylist;int item;int n;int select = 1;//ListNode<int> *p;while (select){cout << "*************************************** *" << endl;cout << "*[1] push_back           [2] push_front *" << endl;cout << "*[3] show_list           [4] pop_back   *" << endl;cout << "*[5] pop_front           [6] insert_val *" << endl;cout << "*[7] lenth               [8] find       *" << endl;cout << "*[9] merge               [10] delete_val*" << endl;cout << "*[11] sort               [12] reverse   *" << endl;cout << "*[13] next               [14] clear     *" << endl;cout << "*[15] prio               [0] quit_system*" << endl;cout << "请选择:>";cin >> select;switch (select){case 1:cout << "请输入要插入的元素(-1结束):>";while (cin >> item, item != -1){mylist.push_back(item);}break;case 2:cout << "请输入要插入的元素(-1结束):>";while (cin >> item, item != -1){mylist.push_front(item);}break;case 3:mylist.show_list();break;case 4:mylist.pop_back();break;case 5:mylist.pop_front();break;case 6:cout << "请输入要插入的元素:";cin >> item;mylist.insert_val(item);break;case 7:cout << "长度为:" << mylist.lenth() << endl;break;case 8:cout << "请输入要查找的元素:";cin >> item;if (mylist.find(item))cout << "it's found" << endl;elsecout << "it's not exist" << endl;break;case 9:cout << "请输入要删除的位置:";cin >> n;//mylist.delete_pos(n,item);break;case 10:cout << "请输入要删除的元素:";cin >> item;mylist.delete_val(item);break;case 11:mylist.sort();break;case 12:mylist.reverse();break;case 14:mylist.clear();break;default:break;}}system("pause");return 0;}




1 0
原创粉丝点击