Original, reproduced please indicate the source.
To familiarize yourself with the list of class writes in the VS environment, C + +
ChainNode.h:
#pragmaOnce#include<memory>#ifndef Chainnode_h#defineChainnode_husingstd::make_shared;usingstd::shared_ptr;template<typename t>structchainnode{T element; shared_ptr<chainNode>Next; Chainnode (ConstT &theelement) {element=theelement; Next=nullptr; } chainnode (ConstT &theelement,ConstShared_ptr<chainnode<t>>thenext) {element=theelement; Next=Thenext; }};#endif // ! Chainnode_h
Chain.h:
#pragmaOnce#include"chainNode.h"#include<memory>#ifndef Chain_h#defineChain_husingstd::shared_ptr;usingstd::make_shared;template<typename t>classChain//Subscript starting from 1{ Public: Chain () {}; Chain (ConstT elementlist[],Const intLeng); voidInsertintPosConstT &theelement); voidEraseConstT &theelement); voidOutput ()Const;Private: shared_ptr<chainNode<T>>Firstnode; intchainsize;}; Template<typename t>chain<t>::chain (ConstT elementlist[],Const intLeng//Linked list length is at least 2{Firstnode= Make_shared<chainnode<t>> (elementlist[0]); Chainsize=1; Auto Pos (firstnode); for(inti =1; I < Leng; i++) {pos->next = make_shared<chainnode<t>>(Elementlist[i]); POS= pos->Next; Chainsize++; }}template<typename t>voidChain<t>::insert (intSituConstT &theelement) {Auto TargetNode= make_shared<chainnode<t>>(theelement); if(Situ = =1) {TargetNode->next =Firstnode; Firstnode=TargetNode; Chainsize++; return; } Situ-=2; Auto Fron (Firstnode); while(Situ && fron! =nullptr) {fron= fron->Next; Situ--; } Auto Tail (fron-next); Fron->next =TargetNode; TargetNode->next =tail; Chainsize++; return;} Template<typename t>voidChain<t>::erase (ConstT &theelement) { if(Firstnode->element = =theelement) {Firstnode= firstnode->Next; Chainsize--; return; } Auto Fron (Firstnode); Auto Pos (fron-next); while(pos!=nullptr) { if(Pos->element = =theelement) {fron->next = pos->Next; Break; } fron= fron->Next; POS= fron->Next; } chainsize--;} Template<typename t>voidChain<t>::output ()Const{Auto pos (firstnode); while(POS! =nullptr) {cout<< pos->element <<' '; POS= pos->Next; } cout<<Endl;}#endif // ! Chain_h
Chain.cpp:
//chain2.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>#include"Chain.h"usingstd::cout;usingStd::endl;intMain () {Doublelist[5] = {2,4,6,8,Ten }; Chain<Double> Chainlist (List,5);//InitializeChainlist.output (); Chainlist.insert (1,1); Chainlist.insert (3,3); Chainlist.output (); Chainlist.erase (3); Chainlist.output (); Chainlist.erase (1); Chainlist.output (); GetChar (); return 0;}
Operation Result:
C + + linked list, use c++11 as much as possible