C ++ data structures and algorithms (4) Circular linked list of linear tables

Source: Internet
Author: User

I wrote a two-way linked list in the previous article. Today I reviewed the circular linked list and wrote a circular linked list with a header node. The specific implementation is as follows:

Node file (listnode. h ):

 template 
   
    class  
    circularlist; template  
    
     class  
     listnode { 
     Public  
    : friend  
     class  circularlist 
     
     ; listnode (): m_pnext (null) {} listnode ( 
      const  T data, listnode 
     
       * pnext = 
       null): m_data (data ), m_pnext (pnext) {} ~ 
       listnode () {m_pnext  = 
       NULL ;}  
       private  
      : T m_data; listnode  
      
        * 
        m_pnext ;};  
      
     
    
   
  

Circular linked list file (circularlist. h ):

# Include "  Listnode. h  "  Template <Typename T> Class  Circularlist {  Public  : Circularlist (): Head (  New Listnode <t> () {Head -> M_pnext = Head ;} ~Circularlist () {emptylist (); Delete head ;}  Public  :  Void Emptylist (); //  Clear linked list      Int Length (); //  Calculate the length of a linked list      Bool Insert (T item, Int N = 0 ); //  Insert element Listnode <t> * Find (Int N ); //  Find the nth Element T Delete ( Int N ); //  Delete nth Element T getdata ( Int N ); //  Obtain the nth Element      Void Print (); //  Output linked list  Private  : Listnode <T> *Head ;}; Template <Typename T> Void Circularlist <t> : Emptylist (){  //  Clear linked list Listnode <t> * P = head ,* Pdel;  While (P-> m_pnext! = Head) {pdel = P-> M_pnext; P -> M_pnext = pdel-> M_pnext; Delete pdel;} Template <Typename T> Int Circularlist <t>: Length (){  //  Calculate the length of a linked list      Int Count = 0  ; Listnode <T> * P = Head;  While (P-> m_pnext! = Head) {count ++ ; P = P-> M_pnext ;}  Return  Count;} Template <Typename T>Bool Circularlist <t >:: insert (T item, Int N = 0  ){  //  Insert element      If (N < 0 | N> Length ())  Return   False ; //  N outbound Listnode <t> * P = Head; listnode <T> * pnewnode =New Listnode <t> (Item );  If (Pnewnode = Null)  Return   False  ;  For ( Int I = 0 ; I <n; I ++ ) {P = P-> M_pnext;  If (P = Head) Return   False ; //  N outbound  } Pnewnode -> M_pnext = p-> M_pnext; P -> M_pnext = Pnewnode;  Return   True  ;} Template <Typename T> Listnode <T> * circularlist <t>: Find ( Int  N ){  // Find the nth Element      If (N < 0 | N> Length ())  Return   False  ; Listnode <T> * P = Head;  While (P! = Head) {P = P-> M_pnext ;}  If (P = Head)  Return NULL;  Return  P;} Template <Typename T> T circularlist <T>: delete ( Int  N ){  //  Delete nth Element      If (N < 0 | N> Length ())  Return  NULL; listnode <T> * P = head ,* Pdel; For ( Int I = 0 ; I <n; I ++ ) {P = P-> M_pnext;} pdel = P-> M_pnext; P -> M_pnext = pdel-> M_pnext; t data = Pdel-> M_data; Delete pdel;  Return  Data;} Template <Typename T> T circularlist <T>: getdata ( Int N ){  //  Obtain the nth Element      If (N < 0 | N> Length ())  Return  NULL; listnode <T> * P = Head;  For ( Int I = 0 ; I <n; I ++ ) {P = P-> M_pnext ;} Return  P;} Template <Typename T> Void Circularlist <t> : Print () {listnode <T> * P = Head; cout < "  Head  "  ;  While (P-> m_pnext! = Head) {P = P-> M_pnext; cout < " -->  " <P-> M_data;} cout < "  -->  " < Endl ;} 

Test file (circularlist. h ):

 //  Circularlist. cpp: defines console applications.Program.  //  # Include  "  Stdafx. h "  # Include  "  Circularlist. h  "  # Include <Iostream> Using   Namespace  STD;  Int _ Tmain ( Int Argc, _ tchar * Argv []) {circularlist < Int > Circularlist;  For ( Int I = 0 ; I < 15 ; I ++ ) Circularlist. insert (I * 3  , I); circularlist. Print (); cout < "  The length of list is:  " <Circularlist. Length () < Endl;  Int Del = circularlist. Delete ( 3 ); Cout <Del < "  Is deleted  " < Endl; circularlist. Print (); cout < "  The length of list is:  " <Circularlist. Length () < Endl; cin.  Get  ();  Return   0  ;} 

Test:

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.