1 package struct;
2
3 //接口
4 interface ILinkStack{
5 //栈中元素个数(栈大小)
6 int size();
7 //取栈顶元素
8 Object top();
9 //判断栈是否为空
10 boolean isEmpty();
11 //入栈
12 Object pop();
13 //出栈
14 Object push(Object value);
15 //清空栈
16 void clear();
17 }
18
19 //工厂类
20 class Factory2{
21 private Factory2(){}
22 public static ILinkStack getILinkStackInstance(){
23 return new LinkStackImpl();
24 }
25 }
26
27 class LinkStackImpl implements ILinkStack {
28 //栈顶元素
29 Node top;
30 //链表长度记录入栈元素
31 private int count;
32 class Node{
33 Node prev;
34 Node next;
35 Object data;
36 public Node(Object data) {
37 this.data = data;
38 }
39 }
40
41 public int size() {
42 return count;
43 }
44
45 public Object top() {
46 return top.data;
47 }
48 //判栈空
49 public boolean isEmpty() {
50 return (size()==0);
51 }
52 //入栈
53 public Object push(Object value) {
54 Node node = new Node(value);
55 if(top == null){
56 top = node;
57 }else{
58 top.next = node;
59 node.prev = top;
60 top = top.next;
61 }
62 count++;
63 return top;
64 }
65 public void print(){
66 System.out.println("从栈顶到栈底打印栈中元素:");
67 myPrint(top);
68 return;
69 }
70 //栈顶->栈底打印链表
71 private void myPrint(Node top){
72 for(Node node = top;node!=null;node=node.prev){
73 System.out.print(node.data+" ");
74 }
75 }
76 //出栈
77 public Object pop() {
78 Node node = top;
79 if(top == null){
80 System.out.println("空栈无要出栈元素");
81 return -1;
82 }else{
83 top = top.prev;
84 node.prev = null;
85 node.data = null;
86 }
87 count--;
88 return top();
89 }
90 //清空栈
91 public void clear(){
92 Node node1 = top;
93 for(Node node = top;node!=null;){
94 node = node.prev;
95 node1.data = null;
96 node1.prev = null;
97 count--;
98 }
99 }
100 }
101 public class LinkStack {
102 public static void main(String[] args) {
103 ILinkStack linkStack = Factory2.getILinkStackInstance();
104 //向下转型
105 LinkStackImpl linkStack1 = (LinkStackImpl)linkStack;
106 System.out.println("============测试isEmpty函数(空栈)=================");
107 System.out.println(linkStack.isEmpty());
108 System.out.println("============测试push和print函数=================");
109 linkStack.push(5);
110 linkStack.push(9);
111 linkStack.push(10);
112 linkStack.push(1);
113 linkStack.push(8);
114 linkStack.push(12);
115 linkStack.push(6);
116 linkStack.push(3);
117 linkStack1.print();
118 System.out.println();
119 System.out.println("============入栈后测试top函数=================");
120 System.out.println(linkStack.top());
121 System.out.println("============入栈后测试size函数=================");
122 System.out.println(linkStack.size());
123 System.out.println("============测试pop和print函数=================");
124 linkStack.pop();
125 linkStack.pop();
126 linkStack.pop();
127 linkStack1.print();
128 System.out.println();
129 System.out.println("============出站后测试top函数=================");
130 System.out.println(linkStack.top());
131 System.out.println("============出栈后测试size函数=================");
132 System.out.println(linkStack.size());
133 System.out.println("============测试clear后size函数=================");
134 linkStack.clear();
135 System.out.println(linkStack.size());
136 }
137 }

最新文章

  1. Sql Cursor example
  2. 架构验证过程发现非数据类型错误 validation found non-data type errors
  3. lucene中FSDirectory、RAMDirectory的用法
  4. Android发展演变与开发环境搭建
  5. redis 2.4异常
  6. ISO13485给企业带来的益处
  7. 界面动态加载时报NullPointException
  8. sorts
  9. POJ 1014 Dividing 多重背包
  10. 03--理解HelloWorld结构
  11. OCP读书笔记(16) - 管理资源
  12. Chapter 2 Open Book——18
  13. shell之路【第二篇】运算与文件调用
  14. thinkphp中select()和find()的区别
  15. Django笔记--模型
  16. [二十六]JavaIO之再回首恍然(如梦? 大悟?)
  17. python locust 性能测试:嵌套
  18. Java高级特性 第13节 解析XML文档(1) - DOM和XPath技术
  19. python网页爬虫开发之六-Selenium使用
  20. 转载 【.NET基础】--委托、事件、线程(3)

热门文章

  1. Loto实践干货(8)loto示波器在LED台灯调光问题维修中的应用案例
  2. Arraylist,LinkedList和Vector的异同
  3. topk算法
  4. yum设置取消代理
  5. 【Git 系列】一个超好用的命令你会用吗?
  6. selet 语句详解
  7. [bzoj2257]瓶子和燃料
  8. C/C++ Qt ToolBar 菜单组件应用
  9. 由于vue的for循环id并不严谨,提高id严谨性
  10. Swagger2简单使用教程