Design of linked list classes in C ++

Source: Internet
Author: User
Mylist. h //  Header file  Struct  Node {  Int  Idata_item;  Struct Node * pnode ;} //  Node Definition  Class  Mylist {  Private : //  Description of member variables        Struct Node *_ At_front;  Struct Node * _ at_end; //  This variable is defined to link the linked list.        Int  _ Size;  Public  :  Struct Node * get_front (){ Return  _ At_front ;}  Struct Node * get_end (){ Return  _ At_end ;}  Int Size ( Void ){ Return  _ Size ;}  Void Insert_front ( Int  )  Void Insert_end ( Int  )  Bool Insert ( Struct Node *, Int  )  Int Remove (Int  )  Void Remove_front ( Void  )  Void Remove_end ( Void  )  Struct Node * Find ( Int  )  Void Display ( Void  )  Void Reverse (Void  )  Bool Equality (mylist & ) Mylist & Concat (mylist & ) Mylist (): _ at_front (  0 ), _ At_end ( 0 ), _ SIZE ( 0 ) //  Constructor ~ The implementation of mylist ()} linked list is as follows: mylist. cpp  //  Name of linked list implementation file  // Insert implementationCode  Bool Mylist: insert ( Struct Node * pnode, Int  A) {assert (pnode ! = 0  );  Struct Node * temp = New   Struct  Node;  If (! Temp) {temp -> Idata_item = A; temp -> Pnext = pnode-> Pnext; pnode -> Pnext = Temp ;}  Return   True  ;  Else  Cerr < "  Non memory allocate  " < Endl;  Return   False  ;}  // Display implementation code  Void Mylist: Display ( Void  ){  If (_ Size = 0  ) Cout < "  Mylist is empty  " < Endl;  Else  {  Struct Node * iter = _ At_front; For ( Int I = 1 ; I <= _ size; I ++ ) {Cout <ITER-> idata_item < "   "  ; ITER = ITER-> Pnext ;}}}  //  Reverse implementation code  Void Mylist: reverse ( Void  ){  Struct Node *Temp; temp = _ At_front; _ at_front = _ At_end; _ at_end = Temp;  While  (}  //  Remove implementation code  Int Mylist: Remove ( Int  A ){  Struct Node * iter1 = _ At_front;  Struct Node * iter2 =0  ;  For ( Int I = 1 ; I <= _ size; I ++ ){  If (Iter1-> idata_item! = A) iter2 = Iter1; iter1 = Iter1-> Pnext;  Else  Iter2 = Iter1-> Pnext; Delete iter1; _ size -- ;  Return   1  ;}  Return   0  ;}  //  Insert_end implementation code  Void Mylist: insert_end ( Int  A ){  Struct Node * temp = New   Struct Node; temp -> Idata_item = A;  If (! _ At_end) {_ at_front = _ At_end = Temp; _ size ++ ;}  Else  {_ At_end -> Pnext = Temp; _ at_end = Temp; _ size ++ ;}}  //  Insert_front implementation code Void Mylist: insert_front ( Int  A ){  Struct Node * temp = New   Struct  Node; temp -> Idata_item = A;  If (! _ At_front) {_ at_front = _ At_end = Temp; _ size ++ ;}  Else  {Temp -> Pnext =_ At_front; _ at_front = Temp; _ size ++ ;}} 

 

Linked lists are the knowledge of data structures. Now we use C ++ classes for encapsulation.

The linked list class is analyzed as follows. The member variable (private) struct node * _ at_front; struct node * _ at_end; int   _ Size; nodes in the linked list, so the defined nodes are as follows: struct node {int idata_item;   Struct node * pnext;} operations supported by the linked list: Insert a node to the specified node, remove a node, and find   Find a node; Reverse flip a linked list; Size   Get the number of nodes in the linked list; output of the display linked list; determination of equal ity linked list; Concat   The two linked lists are linked together. The above are related operations of the linked list class, And the constructor and destructor are added. The declaration of the linked list is as follows:

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.