package com.my; /** * 链式栈 * @author wanjn * */ public class LinkedStack { private Node head; private int size; //入栈,前插入构造链表,删除头结点实现栈;向后插入构造链表,删除头结点实现队列 public void push(Object value){ if (head ==null) { head = new Node(null, value); }else { head = new Node(head, value); } } //出栈 public Object pop(){ Object oldValue =null; if (head!=null) { oldValue = head.value; head = head.next; } return oldValue; } @Override public String toString() { Node temp = head; String result= ""; while (temp!=null) { result +=temp.value+" ,"; temp = temp.next; } return result; } private class Node{ Node next; Object value; Node(Node next,Object value){ this.next = next; this.value = value; } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步