Golang program to check if a stack is empty


In this Golang article, we are going to check if a stack is empty using iterative and recursive method. A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.

Algorithm

  • Step 1 − First, we need to import the fmt and strconv package.

  • Step 2 − Create a stack structure with an item slice to store the stack elements.

  • Step 3 − Now, define the functions, push(), pop() and peek() to perform the basic operations with the stack.

  • Step 4 − push() adds the item to the end of the stack while pop() removes the last item from the stack. And peek() returns the last item from the stack, without removing it.

  • Step 5 − Now, create a isEmpty() function that returns true if the stack is empty otherwise false.

  • Step 6 − It checks if the length of the items in the Stack struct is equal to 0. If the length is 0, then the stack is empty and the function returns true. Otherwise, if the length is greater than 0, the stack is not empty and the function returns false.

  • Step 7 − Start the main() function. Inside the main() function, create a stack struct and initialize a stack with some items.

  • Step 8 − Now, call the isEmpty() function and pass the stack to it as argument.

  • Step 9 − Initially, the stack is empty so it returns false. Then, after pushing some items again call isEmpty() function and now it will return true.

  • Step 10 − Further, after popping all the items, it returns true.

Example 1

In this example, we will define a isEmpty() function that is used to check if a stack is empty using iterative method.

package main

import (
   "fmt"
)

type Stack struct {
   items []int
}

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

func (s *Stack) pop() int {
   l := len(s.items) - 1
   item := s.items[l]
   s.items = s.items[:l]
   return item
}

func (s *Stack) peek() int {
   return s.items[len(s.items)-1]
}

func (s *Stack) isEmpty() bool {
   return len(s.items) == 0
}

func main() {
   s := &Stack{}

   fmt.Println("Is the stack empty?", s.isEmpty())

   s.push(4)
   s.push(5)
   s.push(3)

   fmt.Println("Is the stack empty?", s.isEmpty())

   s.pop()
   s.pop()
   s.pop()

   fmt.Println("Is the stack empty?", s.isEmpty())
}

Output

Is the stack empty? true
Is the stack empty? false
Is the stack empty? true 

Example 2

In this example, we will define a isEmpty() function that is used to check if a stack is empty using recursive method.

package main

import "fmt"

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

func (s *Stack) Pop() int {
   if len(s.items) == 0 {
      return -1
   }
   item := s.items[len(s.items)-1]
   s.items = s.items[:len(s.items)-1]
   return item
}

func (s *Stack) isEmpty() bool {
   if len(s.items) == 0 {
      return true
   } else {
      lastItem := s.Pop()
      isEmpty := s.isEmpty()
      s.Push(lastItem)
      return isEmpty
   }
}

func main() {
   s := Stack{}
   s.Pop()
   s.Pop()
   s.Pop()

   fmt.Println("Is the stack empty?", s.isEmpty())
}

Output

Is the stack empty? true

Conclusion

We have successfully compiled and executed a go language program to check if a stack is empty using iterative and recursive method along with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.

Updated on: 03-Apr-2023

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements