programming

How to Implement Stack in Golang

What is a stack?

Stack is one that is commonly used in programming or computer storage systems. For ‘programmers’ or those usually involved in the IT world, you must be familiar with this term. Stacks are used to make it easier to organize data. So what exactly is Stack? What are the advantages and disadvantages of using it? OK, Santekno will explain to you in more detail and Santekno will provide a very easy sample for implementing the stack.

The definition of Stack is quoted from TechTerm, Stack is a data structure used to store a collection of objects or variables. As the name suggests, we certainly understand and imagine that the data will look like a pile.

The Stack characteristic itself is LIFO (last in first out). What is LIPO? This means that the last data entered is the data that will come out first. As with stacks in general, for example a stack of books, the one at the top or the last one entered must be removed first to get to the book at the bottom of the stack.

For example, you have books on mathematics, physics, chemistry and biology. You put the physics books first, then the next books on chemistry, biology and mathematics. So, to get a chemistry book, you have to take out the biology and mathematics books first because these two books are at the top because they are the last ones to go in.

This analogy is like a stack that has LIPO properties. The last data entered must exit first. This concept is generally used for programming and organizing computer storage.

Advantages of Stack

  • helps manage data with the LIFO method
  • automatically clean objects
  • not easily broken
  • variable size cannot be changed
  • control memory independently

Lack of Stack

  • very limited stack memory
  • There is a possibility that the stack will overflow if there are too many objects
  • does not allow random access, as it must evict the top stack first to access the bottom stack

Methods in Stack Implementation

Santekno is currently modeling or implementing using the Go language (Golang) with easier samples. Here we give an example with 2 function methods that must be present in the Stack data structure. If you also want to know how to implement it using the C++ language, you can see Implement Stack using C++

First, we create a struct which is used to store data later.

type Stack struct {
	items []int
}

Push(), functions to enter data

func (s *Stack) Push(item int) {
	s.items = append(s.items, item)
}

Pop(), functions to retrieve data which will be retrieved from the most recently entered data

func (s *Stack) Pop() int {
	left := len(s.items)

	if left == 0 {
		return -1
	}

	item, items := s.items[left-1], s.items[0:left-1]
	s.items = items
	return item
}

Example Stack Implementation

package main

type Stack struct {
	items []int
}

func (s *Stack) Push(item int) {
	s.items = append(s.items, item)
}

func (s *Stack) Pop() int {
	left := len(s.items)

	if left == 0 {
		return -1
	}

	item, items := s.items[left-1], s.items[0:left-1]
	s.items = items
	return item
}

func main() {
	s := Stack{}

	s.Push(1)
	s.Push(2)
	s.Push(3)
	s.Push(4)

	println(s.Pop())
	println(s.Pop())
	println(s.Pop())
	println(s.Pop())
	println(s.Pop())
	println(s.Pop())
	println(s.Pop())
}

Finally, the result is below

4
3
2
1
-1
-1
-1
comments powered by Disqus