Skip to content

Instantly share code, notes, and snippets.

@TheAlchemistKE
Created June 6, 2023 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAlchemistKE/41e0e72d680764537863284c02379c84 to your computer and use it in GitHub Desktop.
Save TheAlchemistKE/41e0e72d680764537863284c02379c84 to your computer and use it in GitHub Desktop.
package main
type Deque struct {
items []interface{}
}
func NewDeque() *Deque {
return &Deque{
items: make([]interface{}, 0),
}
}
func (d *Deque) AddFront(item interface{}) {
d.items = append([]interface{}{item}, d.items...)
}
func (d *Deque) AddRear(item interface{}) {
d.items = append(d.items, item)
}
func (d *Deque) RemoveFront() interface{} {
if !d.IsEmpty() {
item := d.items[0]
d.items = d.items[1:]
return item
}
return nil
}
func (d *Deque) RemoveRear() interface{} {
if !d.IsEmpty() {
item := d.items[len(d.items)-1]
d.items = d.items[:len(d.items)-1]
return item
}
return nil
}
func (d *Deque) IsEmpty() bool {
return len(d.items) == 0
}
func (d *Deque) Size() int {
return len(d.items)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment