java資料結構–stack

來源:互聯網
上載者:User

 

下面介紹三種實現stack的方法。儘管java中有stack類,但是已經被廢棄了。自己動手實現!!

1.  用鏈表實現

package com.jzm.stackQueueTree;public class  LinkedStack<T> {private  Node topNode;  //引用鏈表中的第一個節點public LinkedStack(){          clear();}//end default constructorprivate  class  Node{ private T data;   //棧的元素  private Node next; //指向下一個節點的串連   private  Node(T data){   this.data = data;   next = null;    }//end constructor  private  T getData(){    return  data;  }   private Node getNextNode(){   return next; }  }private  boolean isEmpty(){   return  topNode == null;   }  private  void clear(){  topNode = null; }  public  void  push(T  newEntry){  Node  newNode = new Node(newEntry);  newNode.next = topNode;      topNode =   newNode;}  public  T pop(){   T top = null;  if(!isEmpty()){     top =     topNode.getData();  topNode = topNode.getNextNode();    }   return  top; }    public void display(){  Node point = topNode;      System.out.println("從頂端元素開始:");   while(point != null){ System.out.println(point.getData());              point = point.next;  }   }  public static void main(String[] args) {    LinkedStack<Integer> linkedStack  = new LinkedStack<Integer>();    for (int i = 0; i < 100; i++){         linkedStack.push(i);                if (i %2 ==0) { linkedStack.pop();}}       linkedStack.display(); }}

2. 用數組實現

 

package com.jzm.stackQueueTree;public class ArrayStack<T> {    private T[]    a;                         //用於存放值的數組private int    topIndex;                   //棧的頂端索引private final  static  int  MAX = 500;      //預設棧大小    public ArrayStack(){     clear(); a = (T[]) new Object [MAX];  }        public ArrayStack(int size){          //自訂棧大小       clear();  a = (T[])new Object[size];}private boolean isFull(){return  (topIndex >= a.length-1);}public void clear(){              //清空棧 topIndex = -1; a = null;} public  void  push(T   newEntry){ if (!isFull()) {  a[++topIndex] = newEntry; }else {        System.out.println("棧滿了  topIndex="+topIndex); }}     public  T  pop(){   T  top = null;  if(topIndex >= 0){    top =  a[topIndex]; topIndex--;  }else{   System.out.println("棧為空白");  }    return  top;    }        public void display(){  System.out.println("ArrayStack從頂端元素開始:");   for(int i=topIndex; i>=0; i--){            System.out.println(a[i]);       }        }     public static void main(String[] args) { ArrayStack<Integer> arrayStack  = new ArrayStack<Integer>();  for (int i = 0; i < 100; i++){           arrayStack.push(i);                if (i %2  == 0){                   arrayStack.pop();  }           } //end for           arrayStack.display();      }  }

3. 用vector實現

package com.jzm.stackQueueTree;import java.util.Vector;public class VectorStack<T>{private Vector <T>  stack;public VectorStack (){stack = new Vector<T>();}    public VectorStack (int maxsize){stack = new Vector<T>(maxsize);}    public  void  push(T   newEntry){           stack.addElement(newEntry);}     public  T  pop(){     T  top = null;   if (!isEmpty()) {  top = stack.lastElement();  stack.removeElementAt(stack.size()-1); }    return  top;    }         private  boolean isEmpty(){           return stack.isEmpty();    }        public void display(){       System.out.println("vectorStack從頂端元素開始:");        for (int i = stack.size()-1; i>=0; i--) {           System.out.println(stack.elementAt(i));    }    }      public static void main(String[] args) { VectorStack<Integer> vectorStack = new VectorStack<Integer>(); for (int i = 0; i < 100; i++){                 vectorStack.push(i);                if (i %2  == 0){                    vectorStack.pop();  }           } //end for           vectorStack.display();      }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.