Skip to content

Instantly share code, notes, and snippets.

@x2764tech
Created May 24, 2013 12:12
Show Gist options
  • Save x2764tech/5643078 to your computer and use it in GitHub Desktop.
Save x2764tech/5643078 to your computer and use it in GitHub Desktop.
package queue
import (
"fmt"
"container/list"
"errors"
)
type Queue struct {
list *list.List
}
func (l *Queue) Dequeue() (value interface{}, err error) {
first := l.list.Front()
if(first == nil) {
err = errors.New("Queue is empty")
return
}
l.list.Remove(first);
value = first.Value;
return
}
func (l *Queue) Enqueue(value interface{}) {
l.list.PushBack(value)
}
func (l *Queue) Len() int {
return l.list.Len()
}
func New(init ...interface{}) (q *Queue) {
q = &Queue{list.New()}
for _, value := range init {
q.Enqueue(value)
}
return
}
func main() {
l := New("Hello, playground")
value, err := l.Dequeue()
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Value was %v\n", value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment