You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							61 lines
						
					
					
						
							691 B
						
					
					
				
			
		
		
		
			
			
			
		
		
	
	
							61 lines
						
					
					
						
							691 B
						
					
					
				| package util | |
| 
 | |
| import "sync" | |
| 
 | |
| type node struct { | |
| 	data interface{} | |
| 	next *node | |
| } | |
| 
 | |
| type Queue struct { | |
| 	head  *node | |
| 	tail  *node | |
| 	count int | |
| 	sync.RWMutex | |
| } | |
| 
 | |
| func NewQueue() *Queue { | |
| 	q := &Queue{} | |
| 	return q | |
| } | |
| 
 | |
| func (q *Queue) Len() int { | |
| 	q.RLock() | |
| 	defer q.RUnlock() | |
| 	return q.count | |
| } | |
| 
 | |
| func (q *Queue) Enqueue(item interface{}) { | |
| 	q.Lock() | |
| 	defer q.Unlock() | |
| 
 | |
| 	n := &node{data: item} | |
| 
 | |
| 	if q.tail == nil { | |
| 		q.tail = n | |
| 		q.head = n | |
| 	} else { | |
| 		q.tail.next = n | |
| 		q.tail = n | |
| 	} | |
| 	q.count++ | |
| } | |
| 
 | |
| func (q *Queue) Dequeue() interface{} { | |
| 	q.Lock() | |
| 	defer q.Unlock() | |
| 
 | |
| 	if q.head == nil { | |
| 		return nil | |
| 	} | |
| 
 | |
| 	n := q.head | |
| 	q.head = n.next | |
| 
 | |
| 	if q.head == nil { | |
| 		q.tail = nil | |
| 	} | |
| 	q.count-- | |
| 
 | |
| 	return n.data | |
| }
 |