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

来源:互联网 发布:php模板引擎与java 编辑:程序博客网 时间:2024/06/02 12:51

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


双链表的实现,和第一篇文章单链表的实现大致相同点击打开链接

本篇文章在构建节点的同时,初始化构建节点的前驱和后继,具体细节参考下列代码

头文件:DList.h

#ifndef DLIST_H_#define DLIST_H_typedef enum{FALSE,TRUE}Status;#include<iostream>#include<cassert>using namespace std;template<class Type>class List;template<class Type>class ListNode{friend class List<Type>;private:Type data;ListNode<Type> *next;ListNode<Type> *prio;public:ListNode() :data(Type()), next(NULL), prio(NULL){}ListNode(Type d, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL) :data(d), 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);first = last = s;size = 0;}~List(){destory();}Status push_back(const Type &x){ListNode<Type> *s = _BuyNode(x, last, NULL);last->next = s;last = s;size++;return TRUE;}void show_list(){ListNode<Type> *s = first->next;while (s != NULL){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){last->next = s;last = s;}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 = NULL;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 = NULL;}else{first->next = s->next;s->next->prio = first;}delete s;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 = NULL;---------->1//}//else//{////first->next = s->next;---------->2  1,2作用相同合并为3//s->next->prio = first;//}//first->next = s->next; -------->3//delete s;//size--;//}Status insert_val(const Type &x){ListNode<Type> *s = first;while (s->next != NULL && s->next->data < x)//找到所插入元素的前一个节点,然后在它的后面插入{s = s->next;}if (s->next == NULL)push_back(x);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 != NULL && s->data != x){s = s->next;}return s;}Status delete_val(const Type &x){ListNode<Type> *s = find(x);if (s == NULL){cout << "该元素不存在,无法删除" << endl;return FALSE;}if (s == last){pop_back();       //最后一个节点的next为NULL,NULL没有前驱,所以s->next->prio = s->prio; 此句不成立}else{s->next->prio = s->prio;s->prio->next = s->next;delete s;size--;}return TRUE;}void sort(){if (size == 0 || size == 1)return;ListNode<Type> *s = first->next;ListNode<Type> *p = s->next;s->next = NULL;//s->next->prio = NULLlast = s;while (p != NULL){s = p;p = p->next;//将s按值插入到链表ListNode<Type> *q = first;while (q->next != NULL && q->next->data < s->data)//寻找前一个节点的位置,在该节点的后面插入该节点{q = q->next;}if (q->next == NULL)//没有比插入节点大得节点,进行尾插即可{last->next = s;s->prio = last;last = s;last->next = NULL;}else//寻找到前一个节点的位置,在该节点的后面插入该节点{s->prio = q;s->next = q->next;q->next->prio = s;q->next = s;}}}void reserve(){if (size == 0 || size == 1)return;ListNode<Type> *s = first->next;ListNode<Type> *p = s->next;s->next = NULL;//s->next->prio = NULLlast = s;while (p != NULL){s = p;p = p->next;//将s头插到链表s->next = first->next;first->next->prio = s;s->prio = first;first->next = s;}}void clear(){if (size == 0)return;ListNode<Type> *s = first->next;while (s != NULL){if (size == 1){last = first;last->next = NULL;}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){return s->next;}ListNode<Type>* prio(ListNode<Type> *s){if (s == first->next) //-------------->第一个节点没有前驱,返回NULLreturn NULL;elsereturn s->prio;}};#endif
测试文件main.cpp

#include"DList.h"#include<cstdlib>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] reserve   *" << 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.reserve();break;case 14:mylist.clear();break;default:break;}}system("pause");return 0;}


1 0
原创粉丝点击