The difference between a linked list node in C + + with a template class and a common class

Source: Internet
Author: User

Linked list nodes in C + + are typically of the same type. So we can do that with templates.

#include <iostream>using namespacestd;template<typename type>//define a declaration that a template class must haveclassLinknode//The class that represents the linked list node { Public: Linknode (): M_pnext (nullptr) {}//constructor, implemented in the function body equivalent to the default in front of the inline keyword decorationLinknode (Type item, linknode* next=nullptr): M_data (item), M_pnext (next) {}//constructors with default parameters~linknode () {M_pnext =nullptr;} Type GetData () {returnm_data; }//Note that the return value type must be written in type, to be clearfriend Ostream&operator<< (ostream& out, linknode& node)//The friend function overload operator, which can be implemented within a class, can also be implemented outside the class,    {         out<<Node.m_data; return  out; }    //friend ostream& operator<< <Type> (ostream& out, linknode& node)//A friend function is implemented outside the class, and when used for operator overloading, the type must be defined, that is, the <Type> cannot be less, otherwise the error    Private: Type m_data; Linknode* M_PNEXT;//all Linkenode can be omitted after <type>, because this defines the Linkenode as the template class in front of the first class, and the default is the same as the above type};//template<typename type>//ostream& operator<< (ostream& out, linknode<type>& node)//implement a friend function outside of the class, note that when declaring in a class, you need to explicitly type operator<< <Type> (), otherwise the error//{//Out << node.m_data;//return out;//}intMain () {Linknode<int>C; Linknode<int> A (3,&c); cout<<"a.m_data="<<a <<Endl; System ("Pause"); return 0;}


2. If you do not use the class template, it can be very simple, such as we use the int type:

#include <iostream>using namespacestd;structlinknode{ Public: Linknode (): M_pnext (nullptr), M_data (0){}//The constructor is initialized in the form of a member list, the efficiency is higher than the initialization in the constructor, the m_data can be initialized, the system defaults to 0, but for the sake of insurance it can be displayed as 0.Linknode (intnum, linknode* next = nullptr): m_data (num), M_pnext (next) {}//constructors with default parameters~linknode () {M_pnext =nullptr;} intGetData () {returnm_data;} Friend Ostream&operator<< (ostream& out, linknode& node);//friend function, overloaded operator, written in the class and outside the class is no different, no template so troublesome, do not need explicit type    /*friend ostream& operator<< (ostream& out, linknode& node) {return out << Node.m_data    ; }*/Private:    intm_data;//data field, explicitly intlinknode* M_pnext;//pointer Field};ostream&operator<< (ostream& out, linknode&node) {    return  out<<Node.m_data;}intMain () {Linknode C; //Linknode B ();//error, there is no corresponding constructor, either write B (3), or B (3,&c), or omit ()Linknode A (3,&c); cout<<"a.m_data="<< a <<Endl; System ("Pause"); return 0;}

The difference between a linked list node in C + + with a template class and a common class

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.