Created
June 6, 2023 13:38
-
-
Save cod3smith/41e0e72d680764537863284c02379c84 to your computer and use it in GitHub Desktop.
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 | |
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