本篇文章给大家分享的是有关怎么在java项目中实现一个链栈存储,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。如下所示:package com.learn.alGorithm.l
本篇文章给大家分享的是有关怎么在java项目中实现一个链栈存储,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
如下所示:
package com.learn.alGorithm.linkStack;public class LinkStack<T> { private LinkStack<T>.node<T> top = new Node<T>(); private int size=0; public boolean push(T t){ if ( isEmpty() ) { top.next = new Node<T>(t); } else { Node<T> newNode = new Node<T>(t, top.next); top.next = newNode; } size ++ ; return true; } public T pop(){ if ( isEmpty() ) { return null; } else { LinkStack<T>.Node<T> node = top.next; top.next = node.next; size --; return node.getT(); } } public T getTop(){ if ( isEmpty() ) { return null; } else { return top.next.getT(); } } public boolean isEmpty(){ return size() == 0; } public int size(){ return size; } class Node<T>{ private T t = null; private Node<T> next = null; public Node(){ } public Node(T t){ this.t = t; } public Node(T t,Node<T> next){ this.t = t; this.next =next; } public T getT() { return t; } public void setT(T t) { this.t = t; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; } }}
--结束END--
本文标题: 怎么在java项目中实现一个链栈存储
本文链接: https://www.lsjlt.com/news/224223.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0