One-way chain linear table for C + + template classes

Source: Internet
Author: User


First on the code! Carefully edit it later!

Header file

<span style= "font-size:18px;" > #pragma oncetemplate<typename eletype>class chainlist{public:struct node{eletype _data; node* _next; Node () {_next = nullptr;} Node (Eletype data) {_data = data; _next = nullptr;}}; Chainlist (); ~chainlist (); bool Getelem (eletype& e, int index=1) const;//get the index element in the list and assign the _data of the element to E;bool Insertelem (const eletype E, int index=1);//Insert a node at the index of the list, the data for the node is Ebool Deleteelem (eletype& e, int index = 1); /delete a node at the index of the list, delete the data of the node assigned to Ebool inserthead (const eletype& e);//Insert data in the head bool Inserttail (const eletype& E );//Insert data bool Clear () at the end,//Empty listvoid showlist () const;//Show data for all elements of list private:bool empty () const;//determine if list is empty// In the list to find the node of the index position, assign the address of the node to n, where it is necessary to pass a reference to the pointer, in order to ensure that n can be modified, or only to ensure that the *n can be modified, that is, n points can be modified bool Find (int index,node*& N) const;bool checkindex (int index) const;//Check whether the list is empty, whether index is legal node* head;//head node node* tail;//tail node int Length;</span ><pre name= "code" class= "CPP"; 
Implementation file. cpp

<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "CPP" ><span style= "font-size:18px;" > #include "ChainList.h" #include <iostream>using namespace Std;template<typename eletype>bool Chainlist<eletype>::checkindex (int index) const{if (Empty ()) {cout << "find:the List is empty!\n"; return false;} if (index<1 | | index>length) {cout << "the index is invalid!\n"; return false;} return true;} Template<typename eletype>bool chainlist<eletype>::find (int index,node*& n) const//index [1,Length]; {if (CheckIndex (index)) {int i = 1; Node * temp = head;while (i < index) {temp = temp->_next;++i;} n = Temp;return true;} return false;} Template<typename eletype>void chainlist<eletype>::showlist () const{node * TEMP=NULLPTR; if (Empty ()) { cout << "The list is empty!\n";} for (int i=1;i<=length;++i) {Find (i, temp); cout << temp->_data << "";}  cout << Endl; }template<typename Eletype>boOl Chainlist<eletype>::clear () {if (Empty ()) {return true;} Else{while (Length) {Eletype m;deleteelem (m);} return true;}} Template<typename Eletype>bool chainlist<eletype>::D Eleteelem (eletype& e,int index=1) {if ( CheckIndex (Index)) {node* temp = nullptr; node* pre_temp = nullptr;if (Find (Index, temp)) {if (index = = 1) {Find (index + 1, Head);} Else{if (index = = Length) {Find (index-1, Tail);} Else{find (index-1, pre_temp);p re_temp->_next = Temp->_next;}} E = Temp->_data;delete Temp;--length;return true;} return false;} return false;} Template<typename eletype>bool Chainlist<eletype>::insertelem (const eletype E,int index) {Node *  Insertnode=new Node (e); if (Empty ()) {if (Index < 1) return false; Head = Tail = Insertnode; Insertnode->_next = nullptr; ++length; return true; } if (index==1) {insertnode->_next = Head; Head = Insertnode; ++length; return true; } if (index==length+1) {tail->_next = Insertnode; insertnode->_next = nullptr; Tail =Insertnode; ++length; return true; }node *temp=nullptr;  if (Find (index-1,temp)) {Insertnode->_next = temp->_next; temp->_next = Insertnode; ++length; return true; } return false;} Template<typename eletype>bool Chainlist<eletype>::getelem (eletype& e,int index) const{if ( CheckIndex (Index)) {node* temp = nullptr;if (Find (index, temp)) E = Temp->_data;return true;} return false;} Template<typename eletype>chainlist<eletype>::~chainlist () {Clear ();} Template<typename eletype>chainlist<eletype>::chainlist (): Length (0), Head (nullptr), Tail (nullptr) {} Template<typename eletype>bool Chainlist<eletype>::empty () Const{return (Length==0);} Template<typename eletype>bool chainlist<eletype>::inserttail (const eletype& e) {return InsertElem (E , Length + 1);} Template<typename eletype>bool chainlist<eletype>::inserthead (const eletype& e) {return InsertElem (E );} </span>

};


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

One-way chain linear table for C + + template classes

Related Article

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.