アルゴリズムとデータ構造 2019-11-17 ⏱ 約 2 分

アルゴリズムとデータ構造 - スタック

スタック(LIFO)データ構造の仕組みを解説。push・pop・peek操作と計算量、Goのスライスを使った実装を紹介します。

Read in: en
アルゴリズムとデータ構造 - スタック

概要

アルゴリズム図鑑を参考に、アルゴリズムとデータ構造を学ぶ。

実装はgithub - bmf-san/road-to-algorithm-masterにも置いてある。

スタック

計算時間

配列や連結リストなど実装形式による。

実装

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]
}

参考

Tags: スタック
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ サポート

このブログを応援していただける方は、以下からサポートをお願いします。いただいたサポートはブログ運営・技術研鑽に活用します。


関連記事