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 ();} 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;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 = Null;last = T;first->data = 1;while (P! = NULL) {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;for (int i = 0; i < first->data-1; ++i) {s = s->next;} S->next = Null;last = S;delete s->next;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; nodelist<type> *s = Buynode (x); while (P->next->data < x && p->next->next! = NULL) {p = p->n ext;} 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! = NULL) {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! = NULL) {p->next = s->next;first->data--;d elete s;} else {tail_delete ();}} Seeking 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;last = T;first->data = 1;while (P! = NULL) {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! = 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 "List.h" int main () in C + + {list<int> mylist;int input = 1;int Insert;while (input) {cout << * * * * * * "<< endl;cout <<" * [1] Show_list [2] Tail_insert * "<< endl;cout <<" * [3] head_insert [4] So RT * "<< 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 (); Bre Ak;case 13:mylist.destroy (); break;case 14:mylist.quit_system (input); break;default:break;}} return 0;}
Clear:
Find:
Head Delete:
Head Insert:
To find the length:
Exit System:
Reverse:
Sort:
Tail Delete:
Tail Plug:
Delete by value:
Insert by Value:
"Data structure" implementation of single-linked list (C + +)