Last active
November 20, 2016 17:42
-
-
Save mauri870/c92f70e20b1d663619e651122a3d1027 to your computer and use it in GitHub Desktop.
Go program representing a queue data structure. Test it https://play.golang.org/p/QXC3OkMMT3
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" | |
) | |
func main() { | |
// Create a new Queue | |
queue := NewQueue() | |
// Push two items | |
queue.Push("First") | |
queue.Push("Second") | |
fmt.Println(queue.Items) | |
// Remove an item (the first inserted) | |
queue.Pop() | |
// Check if the queue is empty and his values | |
fmt.Println(queue.IsEmpty(), queue.Items) | |
} | |
func NewQueue() *Queue { | |
return &Queue{} | |
} | |
type Queue struct { | |
Items []interface{} | |
} | |
func (s *Queue) Push(item interface{}) { | |
s.Items = append(s.Items, item) | |
} | |
func (s *Queue) Pop() interface{} { | |
defer func() { | |
s.Items = s.Items[1:] | |
}() | |
return s.Items[0] | |
} | |
func (s *Queue) IsEmpty() bool { | |
if len(s.Items) == 0 { | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment