package com.company;
public class Main {
//用链表模拟栈
public static void main(String[] args) {
// write your code here
LinkedStake ls = new LinkedStake(4);
ls.push("aa");
ls.push("aB");
ls.push("Ca");
ls.push("Da");
ls.toList();
System.out.println("++++++++++");
ls.pop();
System.out.println("______");
ls.toList();
System.out.println("__ssss____");
}
}
class Node {
public String val;
public Node next;
public Node(String str){
this.val = str;
}
}
class ArrayLinked{
public String val ;
public Node next;
//设置一个头节点
public Node head = new Node("");
public void add(String val){
if(head.next != null){
Node temp = head.next;
head.next = new Node(val);
head.next.next = temp;
}else{
head.next = new Node(val);
}
}
//getLast
public String pop(){
String val = "";
if(head.next != null){
val = head.next.val;
Node temp2 = head.next.next;
head.next = temp2;
}
return val;
}
public void toList(){
Node temp = head;
while(temp.next!= null){
System.out.printf(temp.next.val+"\t");
temp = temp.next;
}
}
}
class LinkedStake{
public ArrayLinked arrayLink = new ArrayLinked();
//设置栈的相关属性
public int top = -1;
public int maxSize;
//初始化
public LinkedStake(int size){
this.maxSize = size;
}
//栈满判断
public boolean isFull(){
return top ==maxSize-1;
}
//栈空判断
public boolean isEmpty(){
return top ==-1;
}
//添加push方法
public void push(String val){
//先判断栈满情况
if(isFull()){
System.out.println("栈满了");
//throw new RuntimeException("栈满了");
}else{
//在链表的头部第一个位置擦插入,因为第一个是最先访问到的
arrayLink.add(val);
top++;
}
}
//取出元素方法
public String pop() {
//先判断栈满情况
String value = "";
if (isEmpty()) {
System.out.println("栈空了");
} else {
//在链表的头部第一个位置擦插入,因为第一个是最先访问到的
top--;
value = arrayLink.pop();
}
return value;
}
public void toList(){
arrayLink.toList();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?