C ++ Implementation of blockchain queue

Source: Internet
Author: User

# Include
 
  
Using namespace std; // node class template
  
   
Struct QNode {T data; QNode * next;}; // queue class template
   
    
Struct LinkList {QNode
    
     
* Front; QNode
     
      
* Rear; size_t size ;}; // construct an empty queue template
      
        Void InitQueue (LinkList
       
         & Que) {que. front = (QNode
        
          *) Malloc (sizeof (QNode
         
           ); If (! Que. front) exit (0); que. size = 0; que. rear = que. front; que. rear-> next = 0;} // destroy the queue template
          
            Void DestroyQueue (LinkList
           
             & Que) {QNode
            
              * P = que. front-> next; free (que. front); while (p! = 0) {que. front = p-> next; free (p); p = que. front ;}// clear the queue template
             
               Void ClearQueue (LinkList
              
                & Que) {QNode
               
                 * P = que. front-> next; while (p! = 0) {que. rear = p-> next; free (p); p = que. rear;} que. rear = que. front; que. front-> next = 0; que. size = 0;} // return the queue length template
                
                  Int QueueLength (LinkList
                 
                   & Que) {return que. size;} // return the first element template of the queue.
                  
                    T GetHead (LinkList
                   
                     & Que) {return que. front-> next-> data;} // element queue template
                    
                      Void EnQueue (LinkList
                     
                       & Que, T) {QNode
                      
                        * P = (QNode
                       
                         *) Malloc (sizeof (QNode
                        
                          ); If (! P) exit (0); p-> data = t; p-> next = 0; que. rear-> next = p; que. rear = p; que. size ++;} // element team-out template
                         
                           Bool DeQueue (LinkList
                          
                            & Que, T & t) {if (que. size = 0) return false; QNode
                           
                             * P = que. front-> next; que. front-> next = p-> next; que. size --; t = p-> data; free (p); return true;} // call the visit function template from the first to the last element traversal.
                            
                              Void VisitQueue (LinkList
                             
                               & Que, void (* visit) (T & t) {QNode
                              
                                * P = que. front-> next; while (p! = 0) {(* visit) (p-> data); p = p-> next ;}// test the function template.
                               
                                 Void Print (T & t) {cout <
                                
                                  Void AddOne (T & t) {t ++;} int main () {// create an empty queue LinkList
                                 
                                   Queue; // initialization queue InitQueue (queue); // element queue EnQueue (queue, 1); EnQueue (queue, 2); EnQueue (queue, 3); EnQueue (queue, 4); // return the length of the queue cout <"Queue Length:" <
                                  
                                    ); // Return the first element of the queue cout <"first element of the queue:" <
                                   
                                     ); // Output all the elements VisitQueue (queue, Print
                                    
                                      ); // Clear the queue ClearQueue (queue); // destroy the queue DestroyQueue (queue );}
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

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.