链式栈——Java实现
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 }