Header file:
#pragma once#include <iostream>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);p rivate:type data; nodelist<type>* next;};/ /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); return p;} private:nodelist<type>* first; nodelist<type>* last;};/ /Node class constructor Template<class Type>nodelist<type>::nodelist () {data = Type (); next = NULL;} Template<classType>nodelist<type>::nodelist (Type d, nodelist<type> *n = NULL) {data = D;next = N;} The constructor of the list class Template<class Type>list<type>::list () {first = last = Buynode (); last->next = First;} 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! = First) {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->next = First;last = p;first->data++;} Head plug Template<class type>void list<type>::head_insert (Type const &x) {nodelist<type> *p = BuyNode (x);p->next = First->next;first->next = P;last = p;for (int i = 0; i < first->data; ++i) {last = Last->ne XT;} 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 = First;last = T;first->data = 1;while (P! = first) {Val_insert (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;first->next = p->next;if (First->data = = 1) last = First;delete p;first- >data--;} Tail delete template<class type>void list<type>::tail_delete () {if (First->data = = 0) {cout << "the List is E Mpty,cannot delete! "<< Endl;return;} nodelist<type> *s = First; nodelist<type> *p = S->next;while (p->next! = first) {s = S->next;p = P->next;} if (P->next = = first) {S->next = First;last = S;delete p;first->data--;}} Insert Template< by valueClass Type>void List<type>::val_insert (Type const &x) {if (First->data = = 0) {tail_insert (x); return;} nodelist<type> *p = First; nodelist<type> *s = Buynode (x); while (P->next->data < x && P->next->next! = first) {p = p-> Next;} if (p->next->data>x) {s->next = P->next;p->next = 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! = first) {if (S->data = = x) {return s;} s = s->next;} return NULL;} Delete Template<class type>void List<type>::val_delete by value (Type const &x) {if (First->data = = 0) {cout << "The list is Empty,cannot delete!" << Endl;return;} Nodelist<type> *s = Find (x), if (s = = NULL) {cout << "the number is not Exist,can not delete!" << Endl;ret Urn;} nodelist<type> *p = First;while (p->next! = s) {p = P->next;} if (s->next! = fIRST) {p->next = s->next;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 = First;last = T;first->data = 1;while (P! = first) {Head_insert (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! = first) {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 "CList.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:
Find:
Head Delete:
Head Insert:
Exit System:
Reverse:
Sort:
Tail Delete:
Tail Plug:
Delete by value:
Insert by Value:
"Data structure" to implement circular linked list (C + +)