数据结构之栈

java版数据结构之链栈。

举凡链栈,一般就是一种特殊的双链表而已。

这个栈,完全是自己手写实现,从链栈的节点开始,链栈的节点类型,定义一个具名内部类,直接看完整的代码:

  1 /**
  2  * 定义了一个自己的栈,底层使用自己定义的节点来实现
  3  * @author Administrator
  4  *
  5  */
  6 class Stack <T>{
  7     private Node base;    //栈底
  8     private Node top;    //栈顶
  9     private int length;
 10     private int size;    //定义栈的大小
 11     
 12     public Stack() {// 构造并初始化一个栈
 13         this.length = 0;
 14     }
 15     
 16     {// 构造代码块用于 初始化一个空栈
 17         this.base = new Node();
 18         this.top = this.base;    //这种状态说明就是一个空栈
 19         this.size = 100;        //栈的大小默认为100
 20     }
 21     /**
 22      * 查看栈是不是为空栈
 23      * @return if empty,then return true
 24      */
 25     public boolean isEmpty()
 26     {
 27         if(this.base==this.top && this.length==0)
 28             return true;
 29         else
 30             return false;
 31     }
 32     
 33     /**
 34      * 获取栈的长度
 35      * @return
 36      */
 37     public int getLength()
 38     {
 39         return this.length;
 40     }
 41 
 42     /**
 43      * 入栈操作
 44      * @param e    要压入栈的元素
 45      * @return    入栈成功返回true
 46      */
 47     public boolean push(T e) {
 48         if(this.length < this.size)
 49         {
 50             synchronized (Stack.class) {// 保证线程的安全
 51                 Node node = new Node(e);
 52                 node.setPre(this.top);
 53                 this.top.setNext(node);
 54                 this.top = node;
 55                 this.length++;
 56                 return true;
 57             }
 58         }
 59         else
 60         {
 61             throw new RuntimeException(new Exception("stack is full"));
 62         }
 63     }
 64     
 65     public T pop() {
 66         if(this.isEmpty())
 67         {
 68             throw new RuntimeException(new Exception("stack is empty")); 
 69         }
 70         else{    //也可以注意 保护 安全 这里 没有加锁
 71             T e = this.top.getData();
 72             this.top = this.top.getPre();
 73             this.top.setNext(null);
 74             this.length --;
 75             return e;
 76         }
 77 
 78     }
 79     
 80     class Node{    //具名内部类来定义链栈的节点
 81         private T data;    //为了操作的方便这里就使用 public 属性
 82         private Node next;    //指向下一个节点
 83         private Node pre;    //指向前一个节点
 84         
 85         public Node() {
 86         }
 87         
 88         public Node(T data)
 89         {
 90             this.data = data;
 91             this.next = null;
 92             this.pre = null;
 93         }
 94 
 95         public T getData() {
 96             return data;
 97         }
 98 
 99         public void setData(T data) {
100             this.data = data;
101         }
102 
103         public Node getNext() {
104             return next;
105         }
106 
107         public void setNext(Node next) {
108             this.next = next;
109         }
110         
111         public Node getPre() {
112             return pre;
113         }
114 
115         public void setPre(Node pre) {
116             this.pre = pre;
117         }
118     }
119 }

测试了一下,还是能用的。

原文地址:https://www.cnblogs.com/OliverZhang/p/6581676.html