Golang Stack Data Structure Implementation

In this post, we will learn how to implement Stack data structure in Golang.

A stack is an ordered list in which insertion and deletion are done at one end, called a top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list.

Push, pop, top, and get size are the typical operations that are allowed on stack data structures.

Stack Operations

  1. When an element is inserted in a stack, the concept is called a push.
  2. When an element is removed from the stack, the concept is called pop.
  3. Trying to pop out an empty stack is called underflow (treat as Exception).
  4. Trying to push an element in a full stack is called overflow (treat as Exception).

Golang Stack Data Structure Implementation

Let's create file named " stack.go" and add the following code to it:

package main

// importing fmt package
import (
	"fmt"
	"strconv"
)

//Element class
type Element struct {
	elementValue int
}

// String method on Element class
func (element *Element) String() string {
	//fmt.Println(element.elementValue)

	return strconv.Itoa(element.elementValue)
}

// NewStack returns a new stack.
func (stack *Stack) New() {
	stack.elements = make([]*Element, 0)
}

// Stack is a basic LIFO stack that resizes as needed.
type Stack struct {
	elements     []*Element
	elementCount int
}

// Push adds a node to the stack.
func (stack *Stack) Push(element *Element) {
	stack.elements = append(stack.elements[:stack.elementCount], element)
	stack.elementCount = stack.elementCount + 1
}

// Pop removes and returns a node from the stack in last to first order.
func (stack *Stack) Pop() *Element {
	if stack.elementCount == 0 {
		return nil
	}

	var length int = len(stack.elements)
	var element *Element = stack.elements[length-1]
	//stack.elementCount = stack.elementCount - 1
	if length > 1 {
		stack.elements = stack.elements[:length-1]

	} else {
		stack.elements = stack.elements[0:]

	}
	stack.elementCount = len(stack.elements)
	return element
}

// main method
func main() {
	var stack *Stack = &Stack{}
	stack.New()
	var element1 *Element = &Element{3}
	var element2 *Element = &Element{5}
	var element3 *Element = &Element{7}
	var element4 *Element = &Element{9}
	stack.Push(element1)
	stack.Push(element2)
	stack.Push(element3)
	stack.Push(element4)
	fmt.Println(stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop())
}

The main() method creates a stack, calls the New method, and pushes the elements after initializing them. The popped-out element value and the order is printed.

Run the following commands to execute the stack.go file:

G:\GoLang\examples>go run stack.go
9 7 5 3

Let's understand above golang program.

The New method on the Stack class creates a dynamic array of elements:


// NewStack returns a new stack.
func (stack *Stack) New() {
	stack.elements = make([]*Element, 0)
}

The Push method adds the node to the top of the stack class:


// Push adds a node to the stack.
func (stack *Stack) Push(element *Element) {
	stack.elements = append(stack.elements[:stack.elementCount], element)
	stack.elementCount = stack.elementCount + 1
}


The Pop method on the Stack implementation removes the last element from the element array and returns the element:


// Pop removes and returns a node from the stack in last to first order.
func (stack *Stack) Pop() *Element {
	if stack.elementCount == 0 {
		return nil
	}

	var length int = len(stack.elements)
	var element *Element = stack.elements[length-1]
	//stack.elementCount = stack.elementCount - 1
	if length > 1 {
		stack.elements = stack.elements[:length-1]

	} else {
		stack.elements = stack.elements[0:]

	}
	stack.elementCount = len(stack.elements)
	return element
}

Comments