Header file:
#pragma once#include <iostream> #include <assert.h>using namespace Std;template<class type>class list;//Node class Template<class Type>class Nodelist{friend class list<type>;p ublic:nodelist (); NodeList (Type d, nodelist<type> *n = null, nodelist<type> *m = null);p rivate:type data; Nodelist<type>* Next; nodelist<type>* prio;};/ /List Class Template<class Type>class list{public:list (); ~list ();p ublic:void show_list (); void Tail_insert (Type const &x), void Head_insert (Type const &x), void sort (), void Head_delete (), void Tail_delete (), void Val_insert (type Const &X); nodelist<type>* Find (Type const &x); void Val_delete (Type const &x); Type length (), void reverse (), void Clear (), void Destroy (), void Quit_system (Type &x);p Rotected:nodelist<type >* Buynode (Type x = Type ()) {nodelist<type>* p = new nodelist<type> (x); assert (P! = NULL); return p;} private:nodelist<type>* first; nodelist<type>* last;};/ /Node class constructor TeMplate<class type>nodelist<type>::nodelist () {data = Type (); next = Null;prio = NULL;} Template<class Type>nodelist<type>::nodelist (Type d, nodelist<type> *n = NULL, NodeList<Type> *m = NULL) {data = D;next = N;prio = m;} The constructor of the list class Template<class Type>list<type>::list () {first = last = Buynode (); first->prio = NULL;} A destructor for the list class Template<class Type>list<type>::~list () {Destroy ();} Show Template<class type>void list<type>::show_list () {nodelist<type> *p = First->next;while (P! = NULL) {cout << p->data << ",";p = P->next;} cout << "NULL" << Endl;} Tail plug Template<class type>void list<type>::tail_insert (Type const& x) {nodelist<type> *p = BuyNode (x); last->next = P;p->prio = Last;last = p;first->data++;} Head plug Template<class type>void list<type>::head_insert (Type const &x) {nodelist<type> *p = BuyNode (x); if (First->data = = 0) {First->neXT = P;p->prio = First;last = P;} Else{p->next = First->next;first->next = P;p->prio = First;p->next->prio = P;last = p;for (int i = 0; i < first->data; ++i) {last = Last->next;}} first->data++;} Sort Template<class type>void List<type>::sort () {if (First->data = = 0) {return;} if (First->data = = 1) {show_list ();} nodelist<type> *t = first->next; nodelist<type> *p = t->next; nodelist<type> *q = Null;t->next = Null;p->prio = Null;last = T;first->data = 1;while (P! = NULL) {Val_inser T (p->data); q = p;p = P->next;delete q;}} Header Delete Template<class type>void list<type>::head_delete () {if (First->data = = 0) {cout << "the List is E Mpty,cannot delete! "<< Endl;return;} nodelist<type> *p = first->next;if (First->data = = 1) {last = First;first->next = NULL;} Else{first->next = P->next;p->next->prio = First;} Delete p;first->data--;} Tail Template<class type>void LIST< Type>::tail_delete () {if (First->data = = 0) {cout << "the list is Empty,cannot delete!" << Endl;return;} nodelist<type> *s = last;if (First->data = = 1) {First->next = Null;first->prio = NULL;last = First;} Else{last = Last->prio;last->next = Null;first->prio = NULL;} Delete s;first->data--;} Insert Template<class type>void list<type>::val_insert (Type const &x) by value {if (First->data = = 0) {Tail_ Insert (x); return;} nodelist<type> *p = first->next; nodelist<type> *s = Buynode (x); while (P->data < x && p->next! = NULL) {p = P->next;} if (p->data >= x) {p->prio->next = S;s->prio = P->prio;s->next = P;p->prio = s;first->data++;} Else{tail_insert (x);}} Find Template<class type>nodelist<type>* list<type>::find (Type const &x) {nodelist<type> *s = First->next;while (s! = NULL) {if (S->data = = x) {return s;} s = s->next;} return NULL;} Delete by value Template<class TyPe>void list<type>::val_delete (Type const &x) {if (First->data = = 0) {cout << "the List is Empty,cann OT delete! "<< Endl;return;} Nodelist<type> *s = Find (x), if (s = = NULL) {cout << "the number is not Exist,can not delete!" << Endl;ret Urn;} if (s->next! = NULL) {s->prio->next = S->next;s->next->prio = s->prio;first->data--;d elete s;} Else{tail_delete ();}} Request Length Template<class Type>type list<type>::length () {return first->data;} Invert Template<class type>void List<type>::reverse () {if (First->data = = 0) {return;} if (First->data = = 1) {show_list ();} nodelist<type> *t = first->next; nodelist<type> *p = t->next; nodelist<type> *q = Null;t->next = Null;p->prio = Null;last = T;first->data = 1;while (P! = NULL) {Head_inse RT (p->data); q = p;p = P->next;delete q;}} Empty Template<class type>void list<type>::clear () {if (First->data = = 0) {return;} while (First-> Next! = NULL) {tail_delete ();}} Destroy Template<class type>void list<type>::d Estroy () {clear ();d elete first;} Exit system Template<class type>void List<type>::quit_system (Type &x) {x = 0;}
Main function:
Implementing single-link list # include "DList.h" int main () {list<int> mylist;int input = 1;int Insert;while (input) {cout << "* * * * * * * in C + + "<< endl;cout <<" * [1] Show_list [2] Tail_insert * "<< endl;cout <<" * [3] Head_insert [4] s ORT * "<< endl;cout <<" * [5] head_delete [6] Tail_delete * "<< endl;cout <<" * [7] Val_insert [8] Find * "<< endl;cout <<" * [9] val_delete [ten] length * "<< endl;cout <<" * [All] Reverse Clear * "<< endl;cout <<" * [14] Destroy Quit_system * "<< endl;cout <<" ******************************************************************** * "<< endl;cout << "Choose:"; cin >> Input;switch (input) {case 1:mylist.show_list (); Break;case 2:cout << " Enter the number want to insert ( -1 end): "And while (CIN >> INSERT, insert! =-1) {Mylist.tail_insert (insert);} Break;case 3:cout << "Please enter the number want to insert ( -1 end):" And while (CIN >> INSERT, insert! =-1) {Myl Ist.head_insert (insert);} Break;case 4:mylist.sort (); break;case 5:mylist.head_delete (); break;case 6:mylist.tail_delete (); Break;case 7:cout << "Please enter the number want to insert:"; Cin >> Insert;mylist.val_insert (insert); Break;case 8:cout <&L T "Please enter the number want to find:"; Cin >> insert;if (mylist.find (insert) = = NULL) {cout << "the number is Not exist! "<< Endl;} Else{cout << "The number at the" << mylist.find (insert) << Endl;} Break;case 9:cout << "Please enter the number want to delete:"; Cin >> Insert;mylist.val_delete (insert); Case 10:cout << "the LengtH is "<<" "<< mylist.length () << endl;break;case 11:mylist.reverse (); break;case 12:mylist.clear (); BR Eak;case 13:mylist.destroy (); break;case 14:mylist.quit_system (input); break;default:break;}} return 0;}
Empty:
Head Delete:
Head Insert:
Reverse:
Sort:
Tail Delete:
Tail Plug:
Delete by value:
Insert by Value:
"Data structure" doubly linked list (C + +)