Algorithms and Data Structures - Stack

Learn how the Stack (LIFO) data structure works. Covers push, pop, and peek operations with time complexity analysis, and a complete Go implementation using slices.

Read in: ja
Algorithms and Data Structures - Stack

Overview

Referring to the Algorithm Encyclopedia, we learn about algorithms and data structures.

The implementation is also available on github - bmf-san/road-to-algorithm-master.

Stack

Computational Time

Depends on the implementation form such as arrays or linked lists.

Implementation

package main

// Stack is a stack.
type Stack struct {
	nodes []*Node
}

// Node is a item of a stack.
type Node struct {
	value string
}

// newStack create a Stack.
func newStack() *Stack {
	return &Stack{}
}

// push adds an node to the top of the stack.
func (s *Stack) push(n *Node) {
	s.nodes = append(s.nodes[:len(s.nodes)], n)
}

// pop removes an node from the top of the stack.
func (s *Stack) pop() {
	s.nodes = s.nodes[:len(s.nodes)-1]
}

References

Tags: Stack
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles