C + + data structure single linked list (template class)

Source: Internet
Author: User

Using template class to realize single linked list and its function

Actions to be implemented:

[1] push_back [2] Push_front
[3] show_list [0] Quit_system
[4] pop_back [5] Pop_front
[6] insert_val [7] Delete_val
[8] Find [9]length
[Ten] Clear [11]destroy
[Reserv] [13]sort

Header File Source code:

#ifndef list_h_included #define list_h_included #include <iostream> using namespace std;

Template<class type> class List; Template<class Type> class ListNode {friend Class list<type>; Public:listnode ():d ATA (Type ()), Next (NULL) { ListNode (Type D, listnode<type> *n=null):d ata (d), Next (n) {} ~listnode () {} public:void SetData (type D) {
	data = D;}
	Type GetData () const {return data;} private:type data;
Listnode<type> *next;

};
	Template<class type> class List {public:list () {i = last = Buynode ();
	} ~list () {list<type>::d Estroy ();
	} public:void push_back (const Type &x);
	void Push_front (const Type &x);
	void Show_list () const;
	void Pop_back ();
	void Pop_front ();
	void Insert_val (const Type &x);
	void Delete_val (const Type &x);
	BOOL Find (const Type &x);
	Type length ();
	void Clear ();                        void Destroy ();                         Destroy the order table void Reserv (); reverse voidSort ();
		protected:listnode<type>* Buynode (Type x = Type ()) {listnode<type> *p = new listnode<type> (x);
	return p;
	} private:listnode<type> *first;
Listnode<type> *last;

}; 
    Template<class type> void List<type&gt::p ush_back (const Type &x) {listnode<type> *s = Buynode (x);
    Last->next = s;
    last = s;
first->data++; Template<class type> void List<type>::p ush_front (const Type &x) {listnode<type> *s = Buynode
    (x);
    S->next=first->next;
    first->next=s;
    if (i = last) last = s;
first->data++;
    } template<class type> void List<type>::show_list () const {listnode<type> *p = first->next;
        while (P!= NULL) {cout<<p->data<< "";
    p = p->next;
} cout<<endl;
    Template<class type> void List<type&gt::p op_back () {if (First->data = 0) return; ListnodE<type> *p = A;
    while (P->next!= last) p = p->next;
    listnode<type> *q = p->next;
    P->next = NULL;
    last = p;
    Delete q;
first->data--;
    Template<class type> void List<type>::p Op_front () {listnode<type> *p = first->next;
    First->next = p->next;
    p = NULL;
    Delete p;
    if (First->data = = 1) last = i;
first->data--; } template<class type> void List<type>::insert_val (const Type &x) {listnode<type> *s = Buynod
    E (x);
    listnode<type> *p = first->next;
    if (P->data > S->data) {push_front (s->data);
    else if (Last->data < S->data) {push_back (s->data);
                } else {while ((P->data < x) && (p->next->data<x)) {
            p = p->next;
            } S->next=p->next; P->next = s;
            first->data++; } template<class type> void list<type>::d elete_val (const Type &x) {listnode<type> *p = i
    ->next;
    listnode<type> *s = Buynode (x);
    if (x = = P->data) {Pop_front (); } else {while (p->data!= x) && (p->next->data!= x)) {p = p->n
        Ext
        } P->next = p->next->next;
        listnode<type> *q = p->next;
        Delete q;
    first->data--; }} template<class type> bool List<type>::find (const Type &x) {listnode<type> *p = First->n
    Ext
        while (P!=null && p->data!=x) p = p->next;
    return p; } template<class type> Type list<type>::length () {cout<< "length =" <<first->data<<e
    Ndl
Return first->data; } template<class type> void List<type>::clear () {while first->data>0) Pop_front ();
    Template<class type> void List<type>::d Estroy ()//Destroy the sequence table {clear ();
    Delete-I;
i = last = NULL; Template<class type> void List<type>::reserv ()/reverse, invert the entire table space {listnode<type> *p = First->next
    ;
    listnode<type> *curr = p->next;

    listnode<type> *tmp = NULL;
    First->next->next = NULL;
        while (Curr)//to directly follow next {TMP = curr->next; to the current pointer
        Curr->next = p;
        p = Curr;
    Curr = tmp;
} first->next = P;
    } template<class type> void List<type>::sort () {listnode<type> *s = first->next;
        while (S->next) {listnode<type> *p = s; while (P->next) {if (S->data > P->next->data) {Typ
                    e tmp = s->data;
                    S->data = p->next->data;
         P->next->data = tmp;       } p = p->next;
    } s = s->next;
 }} #endif//list_h_included

Main function:

<pre name= "code" class= "CPP" > #include "List.h" int main () {list<int> mylist;
	int select = 1;
	int Item;
	int POS;
		while (select) {cout<< "**************************************" <<endl;
		cout<< "* [1] push_back [2] push_front *" <<endl;
		cout<< "* [3] show_list [0] quit_system*" <<endl;
		cout<< "* [4] pop_back [5] Pop_front *" <<endl;
		cout<< "* [6] insert_val [7] delete_val *" <<endl;
		cout<< "* [8] Find [9]length *" <<endl;
		cout<< "* [a] clear [11]destroy *" <<endl;
		cout<< "* [A] reserv [13]sort *" <<endl;
		cout<< "**************************************" <<endl;
		cout<< "Please choose:>";
		cin>>select;
			Switch (SELECT) {Case 1:cout<< "Enter the value you want to insert (-1 end):>";
			while (Cin>>item, item!=-1) {mylist.push_back (Item);
		} break; Case 2:cout<< "Please enter a value to insert (-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 you want to insert:>";
            cin>>item;
            Mylist.insert_val (Item);
		Break
			Case 7:cout<< "Please enter the value to delete:>";
			cin>>item;
			Mylist.delete_val (Item);
		Break
			Case 8:cout<< "Please enter the value to find:>";
			cin>>item;
			Mylist.find (Item);
        Break
            Case 9:mylist.length ();
        Break
            Case 10:mylist.clear ();
        Break
            Case 11:mylist.destroy ();
        Break
            Case 12:mylist.reserv ();
        Break
            Case 13:mylist.sort ();
		Break
        Default:break;


 }
    }
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.