Created
September 14, 2013 20:11
-
-
Save trajber/6565230 to your computer and use it in GitHub Desktop.
A simple implementation of a queue in Golang.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Queue struct { | |
elements []interface{} | |
} | |
func NewQueue() *Queue { | |
q := new(Queue) | |
q.elements = make([]interface{}, 0) | |
return q | |
} | |
func (q *Queue) Enqueue(e interface{}) *Queue { | |
q.elements = append(q.elements, e) | |
return q | |
} | |
func (q *Queue) Dequeue() (e interface{}) { | |
e, q.elements = q.elements[0], q.elements[1:len(q.elements)] | |
return | |
} | |
func (q *Queue) Size() int { | |
return len(q.elements) | |
} | |
func main() { | |
q := NewQueue() | |
q.Enqueue(1).Enqueue(2).Enqueue(3).Enqueue("Hello") | |
for q.Size() > 0 { | |
fmt.Println(q.Dequeue()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment