使用golang的slice来模拟栈

  • slice(切片):底层数据结构是数组
  • stack(栈):一种先进后出的数据结构
普通版的模拟写入和读取的栈
package main

import "fmt"

//栈的特点是先进后出
//使用一个切片的全局变量来模拟栈
var stack []int

//向栈中添加数据
func push(value int) {
    stack = append(stack, value)
}

//从栈中获取数据
func pop() (int, bool) {
    ok := false
    value := 0
    if len(stack) > 0 {
        value = stack[len(stack)-1]
        stack = stack[:len(stack)-1]
        ok = true
        return value, ok
    } else {
        return value, ok
    }
}

func main() {
    //向栈中添加数据
    for i := 0; i < 10; i++ {
        push(i)
        fmt.Println(stack)
    }
    //从栈中获取数据
    for {
        v, ok := pop()
        if ok {
            fmt.Println(v)
        } else {
            break
        }
    }
}
使用goroutine来异步读取栈中数据或往栈中写入数据
package main

import (
    "fmt"
)

//栈的特点是先进后出
//使用一个切片的全局变量来模拟栈
var stack1 []int

//此通道用于通知主协程已经完成操作了
//但是此操作有可能不会输出全部数据
//因为添加数据和获取数据是异步的
//当获取数据的速度快于写入数据
//便不会输出全部数据
var e chan int = make(chan int)

//向栈中添加数据
func push1(value int) {
    stack1 = append(stack1, value)
    fmt.Println(stack1)
}

//从栈中获取数据
func pop1() {
    for {
        if len(stack1) > 0 {
            value := stack1[len(stack1)-1]
            stack1 = stack1[:len(stack1)-1]
            fmt.Println(value)
        } else {
            e <- 0
        }
    }
}

func main() {
    for i := 0; i < 10; i++ {
        go push1(i)
    }

    go pop1()

    <-e
}

输出:

[1]
[1 6 5 9 3 2 7 0 4]
[1 6 5 9 3 2 7 0 4 8]
[1 6 5]
[1 6]
[1 6 5 9 3 2 7 0]
[1 6 5 9]
[1 6 5 9 3 2]
[1 6 5 9 3 2 7]
8
4
0
7
2
3
9
5
6
1
[1 6 5 9 3]

使用goroutine异步读取或者写入的时一定要注意通道的写法,很容易造成死锁

原文地址:https://www.cnblogs.com/TimLiuDream/p/9902496.html

时间: 2024-07-29 16:49:16

使用golang的slice来模拟栈的相关文章

java 16 - 5 LinkedList模拟栈数据结构的集合

请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. 1 package cn_LinkedList; 2 3 import java.util.LinkedList; 4 5 6 7 8 public class MyStack { 9 10 //定义一个LinkedList类的成员变量 11 private LinkedList list = null; 12 13 /** 14 * 构造方法 15

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

用数组模拟栈的结构

package datastruct; import java.util.Arrays; /** * 用数组模拟栈的结构:后进先出(LIFO) 线性表结构 * @author stone * 2014-07-29 06:34:49 */ public class SimulateStack<E> { public static void main(String[] args) { SimulateStack<String> ss = new SimulateStack<Str

采用LinkedList来模拟栈数据结构的集合--先进后出

三.用LinkedList来模拟栈数据结构的集合 /* * 自定义一个数据结构为LinkedList的集合类*/public class MyCollection_LinkedList { public LinkedList linkedList;            public MyCollection_LinkedList() {             //在构造方法里初始化             linkedList= new LinkedList();             }

java、C语言实现数组模拟栈

java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayStack(int size) { this.data = new int[size]; this.size = size; this.top = -1; } public boolean isEmpty() { if (this.top == -1) { return true; } return

迷宫问题,手动模拟栈

(1)迷宫问题 ①问题描述 这是心理学中的一个经典问题.心理学家把一只老鼠从一个无顶盖的大盒子的入口处放入,让老鼠自行找到出口出来.迷宫中设置很多障碍阻止老鼠前行,迷宫唯一的出口处放有一块奶酪,吸引老鼠找到出口. 简而言之,迷宫问题是解决从布置了许多障碍的通道中寻找出路的问题.本题设置的迷宫如图1所示. 图1 迷宫示意图 迷宫四周设为墙:无填充处,为可通处.设每个点有四个可通方向,分别为东.南.西.北.左上角为入口.右下角为出口.迷宫有一个入口,一个出口.设计程序求解迷宫的一条通路. ②基本要求

golang的slice作为函数参数传值的坑

直接贴代码 func sliceModify(slice []int) { // slice[0] = 88 slice = append(slice, 6) } func main() { slice := []int{1, 2, 3, 4, 5} sliceModify(slice) fmt.Println(slice) } 返回的没变,坑爹的,这个设计太那啥了,可以正确跑出效果的版本如下: func sliceModify(slice *[]int) { *slice = append(*

栈模拟队列 队列模拟栈

代码如下: PS:做了一些测试,目前没问题.有问题请指正... 栈模拟队列 队列模拟栈

ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(Tshing Hua OJ - Train)

本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In this station, A is the entrance for each train and B is the exit. S is the transfer end.