Data structure-the implementation of a chain-of-queue storage structure (Golang)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
The implementation of the chain storage structure of the queue
Queue-chained storage structure
Typelsqueuestruct{
Front *lsqnode  //head pointer    
Rear *lsqnode//   tail hands     
The linear  length of the count int//queue           
}

Queue Chain Storage Fabric Node
Typelsqnodestruct{
Elem elemtype//   stored data       
Next *lsqnode//pointer to the next node           
Lsqueue *lsqueue  //Affiliation queue    
}

Initialize Queue
Funcinitlsqueue ()*lsqueue{
Lsqueue: = new (lsqueue)     
Node: = new (Lsqnode)     
  node.elem  = nil   
  node.next  = nil   
  node.lsqueue  = lsqueue   
  lsqueue.front  = node   
  lsqueue.rear  = node   
  lsqueue.count  = 0   
  return  lsqueue    
}

inserting elements
Func(lsqueue*lsqueue)insertlsqueueelem (elemelemtype)  *lsqnode{
Node: = new (Lsqnode)     
  node.elem  = elem   
  node.next  = nil   
  node.lsqueue  = lsqueue   
  lsQueue.rear.next  = node   
  lsqueue.rear  = node   
  lsqueue.count++     
  return  node    
}

Delete Element
Func(lsqueue*lsqueue)deletelsqueueelem ()*lsqnode{
If Lsqueue.emptyqueue ( ) {     
 return             Nil
    }
Node: = lsQueue.front.next    
  lsQueue.front.next  = node.next   
  lsqueue.count--     

If node  = = lsqueue.rear       {
 lsqueue.front           =lsqueue.rear
    }
  return  node    
}

Determine if the queue is empty
Func(lsqueue*lsqueue)emptyqueue ()bool{
If Lsqueue.front  = =       Lsqueue.rear{
 return             True
    }
  return  false    
}

Empty queue
Func(lsqueue*lsqueue)clearlsqueue (){
If Lsqueue.emptyqueue ( ) {     
  return          
    }
  lsqueue.front  = lsqueue.rear   
  lsqueue.count  = 0   
}

Get Queue linear length
Func(lsqueue*lsqueue)lsqueuelength ()int{
If Lsqueue.emptyqueue ( ) {     
    return 0        
    }
  return  lsqueue.count    
}

Import(
   "testing"     
)

varlsqueue*lsqueue

Funcinit (){
Lsqueue =  initlsqueue ()    
}

Functestinsertlsqueueelem (t*testing. T){
_= Lsqueue.insertlsqueueelem ("one")     
  
}

Functestdeletelsqueueelem (t*testing. T){
_= Lsqueue.deletelsqueueelem ( )    
}

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.