Skip to content

Instantly share code, notes, and snippets.

@gustavocd
Created November 7, 2023 21:41
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 gustavocd/80b1c216095293c3c3857e8059aa6ef7 to your computer and use it in GitHub Desktop.
Save gustavocd/80b1c216095293c3c3857e8059aa6ef7 to your computer and use it in GitHub Desktop.
Deque in Go
package main
import (
"container/list"
"fmt"
"strconv"
)
func main() {
d := NewDeque()
d.PushFront(1)
d.PushBack(2)
d.PushBack(3)
fmt.Println(d.Print())
}
type Deque struct {
items *list.List
}
// NewDeque is a constructor that will declare and return the Deque type object
func NewDeque() *Deque {
return &Deque{list.New()}
}
// PushFront will push an element at the front of the dequeue
func (d *Deque) PushFront(val int) {
d.items.PushFront(val)
}
// PushBack will push an element at the back of the dequeue
func (d *Deque) PushBack(val int) {
d.items.PushBack(val)
}
// PopFront will pop an element from the front of the dequeue
func (d *Deque) PopFront() int {
return d.items.Remove(d.items.Front()).(int)
}
// PopBack will pop an element from the back of the dequeue
func (d *Deque) PopBack() int {
return d.items.Remove(d.items.Back()).(int)
}
// Front will return the element from the front of the dequeue
func (d *Deque) Front() int {
return d.items.Front().Value.(int)
}
// Back will return the element from the back of the dequeue
func (d *Deque) Back() int {
return d.items.Back().Value.(int)
}
// Empty will check if the dequeue is empty or not
func (d *Deque) Empty() bool {
return d.items.Len() == 0
}
// Len will return the length of the dequeue
func (d *Deque) Len() int {
return d.items.Len()
}
func (d *Deque) Print() string {
temp := d.items.Front()
s := "["
for temp != nil {
temp2, _ := temp.Value.(int)
s += strconv.Itoa(temp2)
temp = temp.Next()
if temp != nil {
s += " , "
}
}
s += "]"
return s
}
// Playground: https://play.golang.com/p/gqNX-58Ta86
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment