Created
May 24, 2013 12:12
-
-
Save x2764tech/5643078 to your computer and use it in GitHub Desktop.
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 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