如下所示:
packagecom.learn.algorithm.linkStack; /** * 链栈实现 * @author Jiekun.Cui * @param*/ publicclassLinkStack { privateLinkStack .Node top =newNode (); privateintsize=0; /** * 进栈 * @param t * @return ; */ publicbooleanpush(T t){ if( isEmpty() ) { top.next =newNode (t); }else{ Node newNode =newNode (t, top.next); top.next = newNode; } size ++ ; returntrue; } /** * 出栈 * @param t * @return */ publicT pop(){ if( isEmpty() ) { returnnull; }else{ LinkStack .Node node = top.next; top.next = node.next; size --; returnnode.getT(); } } /** * 获取栈顶元素 * @return */ publicT getTop(){ if( isEmpty() ) { returnnull; }else{ returntop.next.getT(); } } /** * 判断栈是不是为空 * @return */ publicbooleanisEmpty(){ returnsize() ==0; } /** * 返回栈的大小 * @return */ publicintsize(){ returnsize; } /** * @author 链栈的节点类 * @param */ classNode { privateT t =null; privateNode next =null; publicNode(){ } publicNode(T t){ this.t = t; } publicNode(T t,Node next){ this.t = t; this.next =next; } publicT getT() { returnt; } publicvoidsetT(T t) { this.t = t; } publicNode getNext() { returnnext; } publicvoidsetNext(Node next) { this.next = next; } } }
package com.learn.algorithm.linkStack;
/**
* 链栈测试
* @author Jiekun.Cui
*/
public class Demo {
public static void main(String[] args) {
LinkStack ls = new LinkStack<>();
ls.push(1);
ls.push(2);
ls.pop();
ls.push(4);
ls.push(5);
ls.push(6);
while ( !ls.isEmpty() ) {
System.out.println(ls.pop());
}
}
}
原神祈愿模拟器最新版
原神祈愿模拟器手机版是一款完整汉化的趣味原神抽卡模拟小游戏,
宝宝森林美食完整版
宝宝森林美食游戏最新版是一款十分有趣的休闲益智游戏,帮助宝宝
g沙盒仇恨官方英文版(gorebox)
G沙盒仇恨英文原版是一款最近非常火热的沙盒模拟类游戏,在这里
迷你世界测试服最新版2024
迷你世界测试服2021最新版,即迷你世界的先遣服版本,用户能
闪耀暖暖最新版2024
闪耀暖暖手游这是非常好玩的换装手游,游戏内容丰富有趣,游戏环