1、链节点

public class Node<T> {
public T data;
public Node next;
}

2、实现代码

public class Stack<T> {
private static Node bottom; //栈底指针
private static Node top; //栈顶指针
private static Integer size; //栈当前大小 /**
* 初始化
*/
public void initStack() {
bottom = top = new Node();
top.next = bottom;
size = 0;
} /**
* 是否空
*
* @return
*/
public static boolean isEmpty() {
if (top.next == bottom) {
return true;
}
return false;
} /**
* 入栈
*
* @param element
*/
public void pushStack(T element) {
Node temp = new Node();
temp.data = element;
if (top.next == bottom)//第一次入栈操作
{
temp.next = bottom;
top.next = temp;
} else {
temp.next = top.next;
top.next = temp;
}
size++;
} /**
* 出栈
*/
public void popStack() { if (isEmpty()) {
System.out.println("栈中没有元素!");
} else {
System.out.println("出栈操作:" + top.next.data + " ");
top.next = top.next.next;
}
size--;
} /**
* 元素个数
*
* @return :个数值
*/
public int sizeStack() {
return size;
} /**
* 查看顶部值
*/
public static void getTop() {
System.out.println("顶部值:" + top.next.data);
} /**
*
* 打印放入的元素
*/
public static void printStack() {
Node temp = top;
if (isEmpty()) {
System.out.println("栈中没有元素!");
} else {
for (int i = 0; i < size; i++) {
System.out.print(temp.next.data + " ");
temp = temp.next;
}
}
System.out.println(); } public static void main(String[] args) {
Stack<Integer> integerStack = new Stack<>();
integerStack.initStack();
integerStack.pushStack(1);
printStack();
integerStack.pushStack(2);
printStack();
integerStack.pushStack(3);
printStack();
integerStack.pushStack(4);
printStack(); integerStack.popStack();
printStack();
integerStack.pushStack(4);
printStack();
integerStack.popStack();
printStack(); System.out.println("大小:" + integerStack.sizeStack()); getTop();
}

3、结果展示

1
2 1
3 2 1
4 3 2 1
出栈操作:4
3 2 1
4 3 2 1
出栈操作:4
3 2 1
大小:3
顶部值:3

最新文章

  1. 摘要: Linux下which、whereis、locate、find命令的区别
  2. react经典进阶demo
  3. 杭电OJ——1198 Farm Irrigation (并查集)
  4. 读propert文件
  5. ajax请求或者页面需要缓存,代码如下
  6. .NET中利用反射来实现自动映射两个对象中的数据成员
  7. 用Java写的简单五子棋游戏(原创五子连珠算法)
  8. python读取excel时,数字自动转化为float
  9. C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解
  10. 痞子衡嵌入式:串口调试工具Jays-PyCOM诞生记(3)- 串口功能实现(pySerial)
  11. poi 导入Excle
  12. C#:如何使方法过时,如何否决方法
  13. Win7 下安装ubuntu14.04双系统
  14. java流程控制语句总结
  15. Parco_Love_gcd
  16. nisght heap increase
  17. python 全栈开发,Day111(客户管理之 编辑权限(二),Django表单集合Formset,ORM之limit_choices_to,构造家族结构)
  18. # 2017-2018-2 20155231《网络对抗技术》实验九: Web安全基础实践
  19. CORS support for ASP.NET Web API (转载)
  20. 日期插件rolldate.js的使用

热门文章

  1. 重学计算机组成原理(七)- 程序无法同时在Linux和Windows下运行?
  2. 阿里巴巴JAVA开发规范学习笔记
  3. 轻松pick移动开发第一篇,flex布局
  4. Shell--&gt;变量的数值计算
  5. .net打杂工程师的面试感想和总结
  6. keras+ ctpn 原理流程图
  7. Keras实例教程(2)
  8. Android Studio和 adb 的一些常用技巧
  9. Liunx学习总结(四)--文件的权限管理
  10. linux 如何初始化密码(解决mysql root用户登录不了的问题)