Single-chain table class definition

Source: Internet
Author: User

//////////////////////////////////////// //////////////////////////////////////// /<Br/> // two classes are usually used, that is, the linked list node class and linked list class, collaboratively representing a single-chain table. <Br/> // a linked list contains zero or multiple nodes, therefore, a list object contains zero or more objects of the type <br/> // linknode. This relationship is called an aggregation relationship or a whole-local relationship in the object-oriented method. <Br/> // to simplify the problem, set the data element in each node of a single-chain table to an integer data. There are four methods to design the data structure <br/>. <Br/> //////////////////////////////////// //////////////////////////////////////// //// </P> <p> ///////////////////////////// //// // 1 composite class /////////////////////////// ///// <br/> // use the method used to declare a friend class in the linknode class, allow members of the linknode and List classes to access private data members of the <br/> // linknode class. Note that the relationship of the friend Meta class is not symmetric. In the linknode class definition, <br/> // declares that list is its membership class. All members of the List class can directly use private members of linknode, no <br/> // operation is required through the interface. Otherwise, the linknode Member cannot directly use the private member of the list. <Br/> //////////////////////////////////// //////////////////////////////////////// /// <br/> class list; // Forward Declaration of the List class <br/> class linknode // node class definition <br/>{< br/> friend class list; // declare the list class as a friend Meta class <br/> PRIVATE: <br/> int data; // data element Field <br/> linknode * link; // chain pointer field <br/>}; <br/> class list // list class definition <br/>{< br/> public: <br/> // public operation of the linked list <br/> ...... <br/> PRIVATE: <br/> linknode * First; // linked list header pointer <br/> }; </P> <p> //////////////// /// // 2 Nested classes /////////// /// // <br/> // internally defined in the List class definition. Because the linknode class is defined in the private part of the List class, <br/> // This ensures that objects and functions outside the list class cannot directly access objects of the linknode class. However, the data of the linknode class <br/> // The Member is placed in its public part, so that members of the linknode class and the list class can directly access them. <Br/> //////////////////////////////////// //////////////////////////////////////// ////// <br/> class list <br/>{< br/> public: <br/> // Linked List Operation <br/> .... <br/> .... <br/> .... <br/> PRIVATE: <br/> class linknode // nested class <br/>{< br/> Public: <br/> int data; // The data field of the node <br/> linknode * link; // the pointer field of the node <br/>}; <br/> linknode * first; // The header pointer of a single-chain table <br/> }; </P> <p> ///////////////////////////////// /// 3 base class and derived class ///////////////////////// ///// // <Br/> // This structure is special. The linknode class is declared as the base class, and the list class is declared as the derived class, establish an inheritance relationship so that the List class <br/> // inherits the data members and some member functions of the linknode class. This indicates a "use" relationship. <Br/> //////////////////////////////////// //////////////////////////////////////// /// // <br/> class linknode // linked list node class <br/>{< br/> protected: <br/> int data; <br/> linknode * link; <br/>}; <br/> class list: public class linknode <br/>{< br/> // linked list class, inheriting the data and operations of the linked list node class <br/> PRIVATE: <br/> linknode * first; // linked list header pointer <br/> Public: <br/> // Linked List Operation <br/> .... <br/> .... <br/> .... <br/> }; </P> <p> /////////////////////////////// // Define the linknode class with struct // <br/> // use this method to define the linknode class, the linknode class is not encapsulated, but the description is simplified. Because first is encapsulated in the List class <br/> //, all linknode instances belonging to the list become private members of the List instance, therefore, <br/> // ensure that it is not directly accessed by the outside world. <Br/> //////////////////////////////////// //////////////////////////////////////// ///// <br/> struct linknode // linked list node Structure <br/> {<br/> int data; <br/> linknode * link; <br/>}; <br/> class list <br/> {<br/> // linked list class, directly use the data and operations of the linked list node struct <br/> PRIVATE: <br/> linknode * First; // linked list header pointer <br/> public: <br/> // Linked List Operation <br/> .... <br/> .... <br/> .... <br/> };

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.