当前位置: 首页 > news >正文

链表专题(Golang)

中小厂手写题中最常见的是链表

LRU

用链表实现栈

package mainimport ("fmt"
)type Node struct {Value intNext  *Node
}type Stack struct {top *Node
}// Push adds a new element to the top of the stack.
func (s *Stack) Push(value int) {newNode := &Node{Value: value, Next: s.top}s.top = newNode
}// Pop removes the top element from the stack and returns its value.
// If the stack is empty, it returns -1 as an indicator.
func (s *Stack) Pop() int {if s.IsEmpty() {return -1 // Or any other sentinel value or error handling}value := s.top.Values.top = s.top.Nextreturn value
}// Peek returns the value of the top element without removing it.
func (s *Stack) Peek() int {if s.IsEmpty() {return -1 // Or any other sentinel value or error handling}return s.top.Value
}// IsEmpty checks if the stack is empty.
func (s *Stack) IsEmpty() bool {return s.top == nil
}// Test function to verify the correctness of the stack implementation.
func testStack() {stack := &Stack{}// Test pushing elementsstack.Push(10)stack.Push(20)stack.Push(30)// Test peeking the top elementif stack.Peek() != 30 {fmt.Println("Peek failed")}// Test popping elementsif stack.Pop() != 30 {fmt.Println("Pop failed for value 30")}if stack.Pop() != 20 {fmt.Println("Pop failed for value 20")}// Test popping from an empty stack should return -1if stack.Pop() != 10 {fmt.Println("Pop failed for value 10")}if stack.Pop() != -1 {fmt.Println("Pop from empty stack failed")}fmt.Println("All tests passed!")
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • SpringBoot集成MyBatis-Plus
  • 如何在多台电脑上同步 VSCode配置和插件
  • N-152基于java贪吃蛇游戏5
  • 【论文阅读】视觉分割新SOTA: Segment Anything(SAM)
  • 【ABAP】ole2 excel多sheet导入导出
  • MySQL 解决时区相关问题
  • Map(HashMap)
  • SpringBoot开发——整合Logbook进行HTTP API请求响应日志输出
  • 卫生间装修防滑石用瓷砖还是大理石好呢?
  • 算法知识点————背包问题
  • 时间序列预测学习方向总概括
  • Python基础语法(1)
  • 已知两圆的圆心半径,求交点坐标——CAD VBA 解决
  • 1-【JavaWeb】数据库基础
  • 生日贺卡录放音芯片,多段音频录音ic生产厂商,NVF04M-32minute
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • ES6 学习笔记(一)let,const和解构赋值
  • Flex布局到底解决了什么问题
  • JAVA 学习IO流
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • leetcode46 Permutation 排列组合
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • 程序员该如何有效的找工作?
  • 创建一个Struts2项目maven 方式
  • 官方解决所有 npm 全局安装权限问题
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 如何学习JavaEE,项目又该如何做?
  • 什么是Javascript函数节流?
  • 双管齐下,VMware的容器新战略
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • ​用户画像从0到100的构建思路
  • ‌JavaScript 数据类型转换
  • # wps必须要登录激活才能使用吗?
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (52)只出现一次的数字III
  • (LeetCode C++)盛最多水的容器
  • (STM32笔记)九、RCC时钟树与时钟 第一部分
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)原始图像数据和PDF中的图像数据
  • ./configure,make,make install的作用
  • .NET Framework .NET Core与 .NET 的区别
  • .NET中GET与SET的用法
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @angular/cli项目构建--http(2)
  • [ 2222 ]http://e.eqxiu.com/s/wJMf15Ku
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [\u4e00-\u9fa5] //匹配中文字符
  • [4.9福建四校联考]