博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构堆栈 内存堆栈_数据结构:堆栈
阅读量:2502 次
发布时间:2019-05-11

本文共 2948 字,大约阅读时间需要 9 分钟。

数据结构堆栈 内存堆栈

A stack is an ordered collection of items following the Last-In-First-Out (LIFO) principle. We add and remove items from the same part of the pile, called the top. We cannot remove items from the base. Just like a pile of books.

堆栈是遵循后进先出(LIFO)原则的项目的有序集合。 我们从堆的同一部分(称为顶部)添加和删除项目。 我们无法从基础中删除项目。 就像一堆书。

Stacks have tons of uses, from creating a history of pages visited to commands entered and to store actions that can be undone.

从创建访问页面的历史记录到输入的命令以及存储可以撤消的操作,堆栈都有许多用途。

实作 (Implementation)

Internally the stack will be represented with a slice type, and I’ll expose the

在内部,堆栈将以slice类型表示,我将公开

  • Push()

    Push()

  • Pull()

    Pull()

  • New()

    New()

methods.

方法。

New() serves as the constructor, which initializes the internal slice when we start using it.

New()用作构造函数,当我们开始使用它时会初始化内部切片。

I’ll create an ItemStack generic type, concurrency safe, that can generate stacks containing any type by using genny, to create a type-specific stack implementation, encapsulating the actual value-specific data structure containing the data.

我将创建一个ItemStack泛型类型,并发安全的,可以生成包含任何类型的使用堆栈genny ,创建一个特定类型的堆栈实现,封装包含数据的实际值特定的数据结构。

// Package stack creates a ItemStack data structure for the Item typepackage stackimport (    "sync"    "github.com/cheekybits/genny/generic")// Item the type of the stacktype Item generic.Type// ItemStack the stack of Itemstype ItemStack struct {    items []Item    lock  sync.RWMutex}// New creates a new ItemStackfunc (s *ItemStack) New() *ItemStack {    s.items = []Item{}    return s}// Push adds an Item to the top of the stackfunc (s *ItemStack) Push(t Item) {    s.lock.Lock()    s.items = append(s.items, t)    s.lock.Unlock()}// Pop removes an Item from the top of the stackfunc (s *ItemStack) Pop() *Item {    s.lock.Lock()    item := s.items[len(s.items)-1]    s.items = s.items[0 : len(s.items)-1]    s.lock.Unlock()    return &item}

测验 (Tests)

The tests describe the usage of the above implementation.

这些测试描述了上述实现的用法。

package stackimport (    "testing")var s ItemStackfunc initStack() *ItemStack {    if s.items == nil {        s = ItemStack{}        s.New()    }    return &s}func TestPush(t *testing.T) {    s := initStack()    s.Push(1)    s.Push(2)    s.Push(3)    if size := len(s.items); size != 3 {        t.Errorf("wrong count, expected 3 and got %d", size)    }}func TestPop(t *testing.T) {    s.Pop()    if size := len(s.items); size != 2 {        t.Errorf("wrong count, expected 2 and got %d", size)    }    s.Pop()    s.Pop()    if size := len(s.items); size != 0 {        t.Errorf("wrong count, expected 0 and got %d", size)    }}

创建一个具体的堆栈数据结构 (Creating a concrete stack data structure)

You can use this generic implemenation to generate type-specific stacks, using

您可以使用以下通用实现来生成特定于类型的堆栈,方法是:

//generate a `IntStack` stack of `int` valuesgenny -in stack.go -out stack-int.go gen "Item=int"//generate a `StringStack` stack of `string` valuesgenny -in stack.go -out stack-string.go gen "Item=string"

翻译自:

数据结构堆栈 内存堆栈

转载地址:http://xaqgb.baihongyu.com/

你可能感兴趣的文章
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
图的算法专题——最短路径
查看>>
SQL批量删除与批量插入
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
C语言之一般树
查看>>
懂了很多大道理,却依旧过不好一生
查看>>
手工数据结构系列-C语言模拟队列 hdu1276
查看>>
【PyQt5 学习记录】008:改变窗口样式之二
查看>>
android EditText长按屏蔽ActionMode context菜单但保留选择工具功能
查看>>
BUAA 111 圆有点挤
查看>>
c++ 继承产生的名字冲突问题 (1)
查看>>
SQL中on条件与where条件的区别
查看>>