Stack Implementation in Golang

Stack Implementation in Golang

Stack is a fundamental data structure which is extensively used in algorithm design. A Stack can be simply defined as Last In First Out (LIFO) data structure. Here in this article we will be discussing how to implement stack in golang which will be holding any data type as value.

package main

import (
	"fmt"
)

type item struct {
	value interface{} //value as interface type to hold any data type
	next  *item
}

type Stack struct {
	top  *item
	size int
}

func (stack *Stack) Len() int {
	return stack.size
}

func (stack *Stack) Push(value interface{}) {
	stack.top = &item{
		value: value,
		next:  stack.top,
	}
	stack.size++
}

func (stack *Stack) Pop() (value interface{}) {
	if stack.Len() > 0 {
		value = stack.top.value
		stack.top = stack.top.next
		stack.size--
		return
	}

	return nil
}

func main() {
	stack := new(Stack)
	// Push different data type to the stack
	stack.Push(1)
	stack.Push("Welcome")
	stack.Push(4.0)

	// Pop until stack is empty
	for stack.Len() > 0 {
		fmt.Println(stack.Pop())
	}
}

Output

4
Welcome
1

Leave a Reply

Your email address will not be published. Required fields are marked *