Last active
November 20, 2016 17:46
-
-
Save mauri870/572f34454d270cefe923a7d7f428ae85 to your computer and use it in GitHub Desktop.
Go program representing a deque data structure. Test it https://play.golang.org/p/i1BGlRsP19
This file contains hidden or 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 Deque | |
deque := NewDeque() | |
// Inject two items in back | |
deque.Inject("Second") | |
deque.Inject("Third") | |
// Push an item in front | |
deque.Push("First") | |
fmt.Println(deque.Items) | |
// Remove an item in front | |
deque.Pop() | |
// Remove an item in back | |
deque.Eject() | |
// Check if the deque is empty and its values | |
fmt.Println(deque.IsEmpty(), deque.Items) | |
} | |
func NewDeque() *Deque { | |
return &Deque{} | |
} | |
type Deque struct { | |
Items []interface{} | |
} | |
func (s *Deque) Push(item interface{}) { | |
temp := []interface{}{item} | |
s.Items = append(temp, s.Items...) | |
} | |
func (s *Deque) Inject(item interface{}) { | |
s.Items = append(s.Items, item) | |
} | |
func (s *Deque) Pop() interface{} { | |
defer func() { | |
s.Items = s.Items[1:] | |
}() | |
return s.Items[0] | |
} | |
func (s *Deque) Eject() interface{} { | |
i := len(s.Items) - 1 | |
defer func() { | |
s.Items = append(s.Items[:i], s.Items[i+1:]...) | |
}() | |
return s.Items[i] | |
} | |
func (s *Deque) 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