« Back to Index

Stack and Queue Implementation

View original Gist on GitHub

Tags: #go

manual stack and queue.go

package main

import (
	"fmt"
)

type Stack []int

func (s Stack) Empty() bool {
	return len(s) == 0
}

func (s *Stack) Push(v int) {
	*s = append(*s, v)
}

func (s *Stack) Pop() int {
	v := (*s)[len(*s)-1]
	*s = (*s)[:len(*s)-1]
	return v
  
}

type Queue []int

func (q Queue) Empty() bool {
	return len(q) == 0
}

func (q *Queue) Enqueue(v int) {
	// the wrapping parentheses are not necessary
	//
	(*q) = append((*q), v)
}

func (q *Queue) Dequeue() int {
	v := (*q)[0]
	
	// the parentheses is needed for the indexing but not the assignment
	// otherwise you'd get an error stating: cannot slice q (type *Queue)
	// because you can't slice a pointer to something, 
	// you need the actual 'thing' dereferenced
	//
	*q = (*q)[1:len(*q)]
	return v
}

func main() {
	s := Stack{}
	s.Push(1)
	fmt.Printf("push %+v (%T)\n", s, s)
	s.Push(2)
	fmt.Printf("push %+v (%T)\n", s, s)
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	fmt.Println(s.Empty())
	// Output:
	// 2
	// 1
	// true

	q := Queue{}
	q.Enqueue(1)
	q.Enqueue(2)
	fmt.Println(q.Dequeue())
	fmt.Println(q.Dequeue())
	fmt.Println(q.Empty())
	// Output:
	// 1
	// 2
	// true
}