day9 | 栈与队列 part-1 (Go) | 232 用栈实现队列、225 用队列实现栈

warning: 这篇文章距离上次修改已过205天,其中的内容可能已经有所变动。

在Go语言中,可以使用内置的数据结构[]interface{}来实现栈和队列。以下是用Go实现的用栈实现队列和用队列实现栈的代码示例。

用栈实现队列:




type MyQueue struct {
    stackIn  []interface{}
    stackOut []interface{}
}
 
func Constructor() MyQueue {
    return MyQueue{
        stackIn:  make([]interface{}, 0),
        stackOut: make([]interface{}, 0),
    }
}
 
func (q *MyQueue) Push(x interface{}) {
    q.stackIn = append(q.stackIn, x)
}
 
func (q *MyQueue) Pop() interface{} {
    if len(q.stackOut) == 0 {
        for len(q.stackIn) > 0 {
            // 将stackIn中的元素转移到stackOut
            q.stackOut = append(q.stackOut, q.stackIn[len(q.stackIn)-1])
            q.stackIn = q.stackIn[:len(q.stackIn)-1]
        }
    }
    if len(q.stackOut) == 0 {
        return -1 // 队列为空
    }
    // 将stackOut的最后一个元素移到stackIn,并返回
    x := q.stackOut[len(q.stackOut)-1]
    q.stackOut = q.stackOut[:len(q.stackOut)-1]
    return x
}
 
func (q *MyQueue) Peek() interface{} {
    if len(q.stackOut) == 0 {
        for len(q.stackIn) > 0 {
            q.stackOut = append(q.stackOut, q.stackIn[len(q.stackIn)-1])
            q.stackIn = q.stackIn[:len(q.stackIn)-1]
        }
    }
    if len(q.stackOut) == 0 {
        return -1 // 队列为空
    }
    return q.stackOut[len(q.stackOut)-1]
}
 
func (q *MyQueue) Empty() bool {
    return len(q.stackIn) == 0 && len(q.stackOut) == 0
}

用队列实现栈:




type MyStack struct {
    queueIn  []interface{}
    queueOut []interface{}
}
 
func Constructor() MyStack {
    return MyStack{
        queueIn:  make([]interface{}, 0),
        queueOut: make([]interface{}, 0),
    }
}
 
func (s *MyStack) Push(x interface{}) {
    s.queueIn = append(s.queueIn, x)
}
 
func (s *MyStack) Pop() interface{} {
    if len(s.queueOut) == 0 {
        for len(s.queueIn) > 1 {
            // 将queueIn中的元素转移到queueOut,除了最后一个
            s.queueOut = append(s.queueOut, s.queueIn[len(s.queueIn)-1])
            s.queueIn = s.queueIn[:len(s.queueIn)-1]
        }
        // 最后一个元素从queueIn移动到queueOut,并返回
        x := s.queueIn[0]
        s.queueOut = append(s.queueOut, s.queueIn[0])
        s.queueIn = s.queueIn[:0]
        return x
    }
    // 如果queueOut不为空,直接从queueOut弹出元素
    x := s.queueOut[len(s.queueOut)-1]
    s.queueOut = s.queueOut[:len(s.queueOut)-1]
    return x
}
 
func (s *MyStack) Top() interface{} {
    if len(s.queueOut) == 0 {
        for len(s.queueIn) > 1 {
            s.queueOut = append(s.queueOut, s.queueIn[
最后修改于:2024年08月07日 11:01

评论已关闭

推荐阅读

DDPG 模型解析,附Pytorch完整代码
2024年11月24日
DQN 模型解析,附Pytorch完整代码
2024年11月24日
AIGC实战——Transformer模型
2024年12月01日
Socket TCP 和 UDP 编程基础(Python)
2024年11月30日
python , tcp , udp
如何使用 ChatGPT 进行学术润色?你需要这些指令
2024年12月01日
AI
最新 Python 调用 OpenAi 详细教程实现问答、图像合成、图像理解、语音合成、语音识别(详细教程)
2024年11月24日
ChatGPT 和 DALL·E 2 配合生成故事绘本
2024年12月01日
omegaconf,一个超强的 Python 库!
2024年11月24日
【视觉AIGC识别】误差特征、人脸伪造检测、其他类型假图检测
2024年12月01日
[超级详细]如何在深度学习训练模型过程中使用 GPU 加速
2024年11月29日
Python 物理引擎pymunk最完整教程
2024年11月27日
MediaPipe 人体姿态与手指关键点检测教程
2024年11月27日
深入了解 Taipy:Python 打造 Web 应用的全面教程
2024年11月26日
基于Transformer的时间序列预测模型
2024年11月25日
Python在金融大数据分析中的AI应用(股价分析、量化交易)实战
2024年11月25日
AIGC Gradio系列学习教程之Components
2024年12月01日
Python3 `asyncio` — 异步 I/O,事件循环和并发工具
2024年11月30日
llama-factory SFT系列教程:大模型在自定义数据集 LoRA 训练与部署
2024年12月01日
Python 多线程和多进程用法
2024年11月24日
Python socket详解,全网最全教程
2024年11月27日