Data Structure-some basic operations c ++ code for linear tables

Source: Internet
Author: User

Data Structure-some basic operations c ++ code for linear tables

// Sequence storage structure template of a linear table
 
  
Class Linearlist {public: Linearlist (int MaxListSize = 10 );~ Linearlist () {delete [] element;} bool IsEmpty () const {return length = 0;} bool IsFull () const {return length = MaxSize;} int Length () const {return length;} bool Find (int k, T & item) const; int search (const T & item) const; void Delete (int k, T & item ); void Insert (int k, const T & item); private: int MaxSize, length, T * element;}; template
  
   
Linearlist
   
    
: Linearlist (int MaxListSize = 10) {MaxSize = MaxListSize; element = new T [MaxSize]; // apply for an array with a size of MaxSize length = 0; // there is no real table node at the beginning, so the table length is 0} // access: Assign the field value of the node whose subscript is k to item and return true, if not, false is returned. template
    
     
Bool Linearlist
     
      
: Find (int k, T & item) const {if (k <0 | length = 0 | k> length-1) {cout <"unreasonable position or empty list! "<Endl; return false;} else {item = element [k]; return true ;}// search: find the node where the field value is item in the table and return its subscript. If there is no item in the table,-1 is returned; template
      
        Int Linearlist
       
         : Search (const T & item) const {for (int k = 0; k <= length-1; k ++) {if (element [k] = item) return k;} return-1;} // Delete the node whose table is k and assign the value to itemtemplate.
        
          Void Linearlist
         
           : Delete (int k, T & item) {if (find (k, item) // if a node whose subscript is k is found, move all the nodes behind it to a forward position {for (int I = k; I
          
            Void Linearlist
           
             : Insert (int k, const T & item) {if (IsFull () // if The table is full, The new node {cout <"The list is full! "<Endl; exit (1);} else if (k <0 | k> length-1) {cout <" The node does not exist! "<Endl; exit (1) ;}else {for (I = length-1; I >= k + 1; I --) {element [I + 1] = element [I] ;}} element [k + 1] = item; length ++ ;}// SLNodetemplate
            
              Struct SLNode {T data; // data domain SLNode
             
               * Next; // pointer domain SLNode (SLNode * nextNode = NULL) {next = nextNode;} SLNode (const T & item, SLNode * nextNode = NULL) {data = item; next = nextNode ;}}; // SLNode definition reference blog Data Structure-stack basic operations c ++ code // chain storage structure template of a single-chain table
              
                Class SLList {public: SLList () {head = new SLNode
               
                 ()}; // Constructor, which constructs an empty table SLList (T & item) with only the whistle node; // constructor ~ SLList (); bool IsEmpty () const {return head-> next = NULL;}; int length () const; // bool Find (int k, T & item) const; int Search (const T & item) const; void Delete (int k, T & item); void Insert (int k, const T & item); private: SLNode
                
                  * Head; SLNode
                 
                   * Tail; SLNode
                  
                    * Currptr;}; // constructor of a single-chain table to generate an empty table template with only the whistle Node
                   
                     SLList
                    
                      : SLList () {head = tail = currptr = new SLNode
                     
                       (); Size = 0;} // The constructor of a single-chain table to generate a table template containing a whistle node and a table node.
                      
                        SLList
                       
                         : SLList (T & item) {tail = currptr = new SLNode
                        
                          (Item); // generate a table node head = new SLNode
                         
                           (Currptr); // generate the whistle node size = 1;} // The Destructor template of the single-chain table
                          
                            SLList
                           
                             ::~ SLList () {while (! IsEmpty () {currptr = head-> next; head-> next = currptr-> next; delete currptr;} delete head ;} // algorithm Insert // Insert a node template with the data field value of item after the current node
                            
                              Void SLList
                             
                               : Insert (const T & item) {currptr-> next = new SLNode
                              
                                (Item, currptr-> next); if (tail = currptr) tail = currptr-> next; size ++ ;} // insert a node template with the data field value of item at the end of the table
                               
                                 Void SLList
                                
                                  : InsertFromTail (const T & item) {tail-> next = new SLNode
                                 
                                   (Item, NULL); tail = tail-> next; size ++;} // insert a template after the whistle Node
                                  
                                    Void SLList
                                   
                                     : InsertFromHead (const T & item) {if (IsEmpty () {head-> next = new SLNode
                                    
                                      (Item, head-> next); tail = head-> next;} else {head-> next = new SLNode
                                     
                                       (Item, head-> next);} size ++;} // algorithm Delete // Delete the successor node of the current node and return its data value to the variable de_itemtemplate
                                      
                                        Bool SLList
                                       
                                         : Delete (T & de_item) {if (currptr = tail | IsEmpty () {cout <"No next node or empty list! "; Return false;} SLNode
                                        
                                          * Temp = currptr-> next; currptr-> next = temp-> next; size --; de_item = temp-> data; if (temp = tail) tail = currptr; delete temp; return true;} // delete the first real table node after the whistle node and return its data value to the variable de_itemtemplate
                                         
                                           Bool SLList
                                          
                                            : DeleteFromHead (T & de_item) {if (IsEmpty () {cout <"Empty list! "; Return false;} SLNode
                                           
                                             * Temp = head-> next; head-> next = temp-> next; size --; de_item = temp-> data; if (temp = tail) {tail = head;} delete temp; return true;} // delete the end node of the table and return its data value to the variable de_itemtemplate.
                                            
                                              Bool SLList
                                             
                                               : DeleteFromTail (T & de_item) {if (IsEmpty () {cout <"Empty list! "; Return false;} // point the current pointer to the front node SetEnd (); Prev (); de_item = tail-> data; currptr-> next = NULL; size --; delete tail; tail = currptr; return true ;}
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

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.