#ifndef _dclist_#define _dclist_#include<iostream>using namespace std; #include <assert.h>template < Class Type>class Dclist;template<class type> class Node{friend class dclist<type>;p ublic:node ():d ata (0 ), Prio (null), Next (null) {}node (Type d,node<type>*p=null,node<type>*n=null):d ata (d), Prio (p), next (n) {} ~node () {}void SetData (Type d) {data = D;} Type GetData () const{return data;} private:node<type>* Prio; Node<type>* Next; Type data;}; Template <class type> class Dclist{public:dclist () {node<type>*s = _buynode (0); first = Last=s;first-> Prio = Last;last->next = First;size = 0;} ~dclist () {}node<type>* _buynode (const type&x) {node<type>*s = new node<type> (x); assert (s! = NULL ); return s;} Public:bool push_back (const type&x) {node<type>* s = _buynode (x); S->prio = Last;last->next = S;last = s;s- >next = First;first->prio = Last;size++;return true;} BOOL Push_front (const Type &x) {if(size = = 0) push_back (x); node<type>* s = _buynode (x); s->next = First->next;first->next->prio = S;first->next = S;s->prio = First;size++;return true;} void Show_list () {if (size = = 0) return; Node<type>*p = First;while (p->next! = first) {p = p->next;cout << p->data << "<==>";} cout << "ok!" << Endl;} void Pop_back () {if (size = = 0) return; Node<type>*p = Last;last = Last->prio;last->next = First;first->prio = Last;delete p;size--;} void Pop_front () {if (size = = 0) return;if (size = = 1) pop_back (); else{node<type>*p = First->next;first->next = P->next;p->next->prio = First;delete p;size--;}} BOOL Insert_val (const TYPE&X) {if (size = = 0) push_back (x); else{node<type>*p = _buynode (x); Node<type>*q = First->next;while (q! = First && Q->data < p->data) {q = Q->next;} if (q = = first) {push_back (x);} Else{p->prio = Q->prio;q->prio->next = P;p->next = Q;Q->PRio = p;size++;}} return true;} node<type>* Find (const type&x) {if (size = = 0) {cout << "linked list is empty" << endl;return null;} Node<type>*q = _buynode (x); node<type>*p = First->next;while (P! = First && Q->data! = p->data) p = p->next;if (p = = first) { cout << "failed to find in linked list" << x <<endl;return NULL;} return p;} BOOL Delete_val (const TYPE&X) {if (size = = 0) return false; Node<type>*p = Find (x), if (p = = NULL) {cout << "linked list does not exist" << x << "Cannot delete" << Endl;return false;} if (p = = last) {Pop_back ();} Else{p->prio->next = P->next;p->next->prio = P->prio;delete p;size--;} return true;} void sort () {if (size = = 0 | | size = = 1) return; Node<type>*s = first->next; Node<type>*p = S->next;last = S;s->next = First;first->prio = S;while (P! = first) {s = p;p = p->next; Node<type>*q = First->next;while (q! = First && Q->data < s->data) {q = Q->next;} if (q = = first) puSh_back (s->data); Else{s->prio = Q->prio;q->prio->next = S;s->next = Q;q->prio = S;}}} void Resver () {if (size = = 0 | | size = = 1) return; Node<type>*s = first->next; Node<type>*p = S->next;last = S;s->next = First;first->prio = S;while (P! = first) {s = p;p = P->next;pus H_front (S->data);}} int Length () {return size;} BOOL Next (const TYPE&X) {if (size = = 0) return false; Node<type>*p;p=find (x), if (p = = NULL) {cout << "not present in the list" << x << endl;return false;} if (p = = last) {cout << x << "successor is head node" << Endl;//return true;} Else{cout << x << "successor is << p->next->data << Endl;} return true;} BOOL Prio (const TYPE&X) {if (size = = 0) return false; Node<type>*p;p = Find (x), if (p = = NULL) {cout << "linked list not present" << x << endl;return false;} if (p = = first->next) {cout << x << "precursor is head node" << Endl;//return true;} Else{cout << x << "The precursor is" << P->p rio->data << Endl;} return true;} void Clear () {if (size = = 0) return; node<type>*p = First->next;while (P! = first) {//node<type>*q = p;//p = P->next;first->next = P->n Ext;p->next->prio = First;delete p;p = first->next;size--;} last = first;//first=last again open space will be less open size = 0;} private:node<type>* first; node<type>* last;size_t size;}; #endif
#include "DCList.h" void Main () {dclist<int> mylist;int Item;int select = 1; node<int>* p;while (Select) {cout << "************************************" << endl;cout << "* [ 1] push_back [2] Push_front * "<< endl;cout <<" * [3] show_list [4] pop_back * "<< Endl;cout & lt;< "* [5] pop_fornt [6] insert_val *" << endl;cout << "* [7] length [8] Find *" << Endl;cout << "* [9] merge [ten] delete_val*" << endl;cout << "* [one] sort [a] resver * "<< endl;cout <<" * [[] next [] prio * "<< endl;cout <<" * [] clear [0 ] quit_system* "<< endl;cout <<" ************************************ "<< endl;cout <<" Please select a service item :> ", Cin >> Select;switch (SELECT) {case 1:cout <<" Enter the data to be inserted (-1 end):> "; while (CIN >> Item, item! =- 1) {mylist.push_back (Item);} Break;case 2:cout << "Please enter the data to be inserted (-1 end): ≫ "; 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 << "Please enter the value to insert:>"; Cin >> Item;mylist.insert_val (Item); Break;case 7:cout << "The length of the sequential table is:>" << Mylist.length () << endl;break;case 8:cout << "Please enter the value to find:>"; cin >> item;p = Mylist.find (Item); if (p = = NULL) {cout << "the data to find does not exist." << Endl;} Break;case 9:break;case 10:cout << "Please enter the value to delete:>"; Cin >> Item;mylist.delete_val (Item); Break;case 11: Mylist.sort (); break;case 12:mylist.resver () break;case 13:cout << "Please enter the number of successors to be checked"; Cin >> Item;mylist.next ( Item); Break;case 14:cout << "Please enter the number of precursors to check"; Cin >> Item;mylist.prio (item); Break;case 15:mylist.clear (); break ;d Efault:break;}} System ("Pause");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Double-loop linked list (C + +)