最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java 实现链栈存储的方法
时间:2022-06-29 01:24:49 编辑:袖梨 来源:一聚教程网
如下所示:
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());
}
}
}
相关文章
- 致命公司铲子可攻击怪物类型汇总 12-05
- 星球重启20级头盔制作方法详解 12-05
- 星球重启分解废品任务完成方法 12-05
- 星球重启偶像任务攻略 偶像任务过法 12-05
- 星球重启9个酸液腺体获取攻略 12-05
- 星球重启采集工具制作方法 采集工具怎么做 12-05