棧java實現,java實現

來源:互聯網
上載者:User

棧java實現,java實現

  這幾天,過得挺充實的,每天都在不停的上課,早上很早就起來去跑步,晚上到圖書館看書。一邊緊張的學習,一邊在默默的備戰軟考。最近還接手了一個公司官網的建設。這是我在川信最後的一個完整學期了,每件事我都要認真去做。就算會有失落,會有失敗,會有不甘,也不輕言放棄。

  剛剛下去跑了幾圈,吹著夜晚的冷風,在操場狂奔。汗水濕透了全身,滴在鏡片上,我快看不清了遠方。想起很多事來,來川信的兩年,參加了兩次運動會,這是第三次,且每次都是長跑,我享受著這種超越自己的感覺。長跑遠沒有結束,我一直在這條路上。

  對於時間的安排,我的重點還是軟考下午的5道大題,演算法資料結構,設計模式,資料流圖,資料庫設計,uml例圖,項目設計等。經過這幾天努力,用java實現了常用的幾種資料結構,畫了很多的圖,其中真是奧妙無窮。下一步,是演算法設計,然後設計模式等。因為高中學了2年的電腦基礎和網路知識,所以上午的一知半解。準備通過反覆做題和百度練習。將下午的複習完畢之後,將進入全面複習階段。通過真題訓練,反覆看書,做題,看代碼,寫代碼。希望通過這次的複習,能夠吸收這些知識轉換為自己的思想,而不是死記硬背,應付考試。

  下面是棧的順序儲存結構和鏈式儲存實現:

  

 1 package stack; 2  3 public interface IStack<E> { 4     //1.判斷空棧 5     public boolean isEmpty(); 6      7     //2.判斷棧滿 8     public boolean isMax(); 9     10     //3.入棧11     public boolean push(E e);12     13     //4.出棧14     public E pop();15     16     //5.返回棧頂17     public E peek();18     19     //6.返回元素在棧中的位置20     public int getIndex(E e);21     22     //7.返回棧的實際長度23     public int size();24     25     //8.返回棧容量26     public int getStackSize();27     28     //9.列印棧29     public void display();30     31     32 33 }

//順序棧

  1 package stack;  2   3 //我的棧資料結構  4 public class MyStack<E> implements IStack<E>{  5     private Object[] data = null; //資料域  6     private int top = -1;           //棧頂指標初始化為-1  7     private int maxSize = 0;      //棧最大容量  8       9     //預設設定棧容量為10 10     MyStack(){ 11         this(10); 12     } 13      14     public MyStack(int initialSize){ 15         if(initialSize >= 0){ 16             this.data = new Object[initialSize]; //初始化數組 17             this.maxSize = initialSize; //設定棧最大容量 18             this.top = -1; 19         } 20     } 21      22     public boolean isEmpty() { 23         return top == -1 ? true : false; //根據棧頂值判斷,如果棧頂指標沒有更新,則為空白棧 24     } 25  26     public boolean isMax() { 27         return top >= maxSize-1 ? true : false; //根據棧頂值判斷,如果棧頂指標大於最大容量,則為滿棧 28     } 29  30     public boolean push(E e) { 31         if(isMax()){ 32             System.err.println("對不起,棧已滿,無法入棧"); 33             return false; 34         } 35         top ++; //更新棧頂下標 36         data[top] = e; //將元素添加到表中 37 //        System.out.println("添加" + e + "成功"); 38         return true; 39     } 40  41     @SuppressWarnings("unchecked") 42     public E pop() { 43         if(isEmpty()){ 44             System.err.println("對不起,目前是空棧,沒有元素可以出棧"); 45             return null; 46         } 47         E e = (E) data[top]; //返回當前的棧頂元素 48         top--; //更新棧頂 49         return e; 50     } 51  52     @SuppressWarnings("unchecked") 53     public E peek() { 54         if(isEmpty()){ 55             System.err.println("對不起,目前是空棧,無法返回棧頂元素"); 56             return null; 57         } 58         return (E) data[top]; //返回棧頂元素 59     } 60  61     public int getIndex(E e) { 62         //根據棧頂和棧底(-1)遍曆棧 63         while(top != -1){ 64             //peek()返回當前棧頂 65             if(peek().equals(e)){ 66                 return top; 67             } 68             top --; 69         } 70          71         return -1; 72     } 73  74     public int size() { 75         return this.top+1; //棧頂值+1,為棧元素的實際個數 76     } 77  78     public int getStackSize() { 79         return this.maxSize; //返回棧實際長度 80     } 81  82     public void display() { 83         //根據棧頂和棧底(-1)遍曆 84         while(top != -1){ 85             System.out.println(top); 86             top --; 87         } 88     } 89      90     public static void main(String[] args) { 91         MyStack<Integer> stack = new MyStack<Integer>(); 92         //入棧 93         for (int i = 0; i < 10; i++) { 94             stack.push(i);  95         } 96         //出棧 97 //        stack.pop(); 98         //返回棧頂 99 //        System.out.println(stack.peek());100         //求長101 //        System.out.println(stack.size());102         103     }104     105 }


//鏈棧

 1 package stack; 2  3 //鏈棧是棧的鏈式儲存結構,由多個節點群組成。在鏈棧中的棧頂為前端節點,節點由資料域和指標域組成 4 //對鏈棧的操作都是間接的通過棧頂(前端節點)完成。 5 //順序棧是一種特殊的順序表 6 //鏈棧是一種特殊鏈表 7 public class LinkedStack{ 8     //定義節點類 9     private class Node{10         public Object data = null; //資料域11         public Node next = null;   //指標域12         13         //建構函式初始化14         @SuppressWarnings("unused")15         public Node(){}16         17         public Node(Object data, Node next){18             this.data = data;19             this.next = next;20         }21 22         @Override23         public String toString() {24             return "Node [data=" + data + ", next=" + next + "]";25         }26     }/*Node*/27     28     private Node top = null; //定義棧頂29     private int size = 0;    //定義棧節點數量30     31     //判斷棧空32     public boolean isEmpty(){33         return size == 0 ? true : false;34     }35         36     //壓棧37     public boolean push(Object obj){38         //更新前端節點,讓新節點指向原來的前端節點39         System.out.println("壓棧成功:" + obj + "指向->" + top);40         top = new Node(obj, top); //壓棧是將節點插入到棧頂之前。也就是更新前端節點。改變指標指向41         size ++; //棧長度++42         return true;43     }44     45     //出棧46     public Object pop(){47         if(isEmpty()){48             System.out.println("對不起,目前是空棧,不能出棧");49             return null;50         }51         Node temp = top;     //前端節點引用52         top = top.next;        //更新前端節點53         temp.next = null;     //釋放引用,刪除指標指向54         size-- ;             //棧節點數量-155         return temp.data;   //出棧56     }57     58     //返回棧頂元素,但不彈出棧59     public Object peek(){60         return this.top.data; //直接返回棧頂元素61     }62     63     //遍曆棧並列印64     public void display(){65         //從棧頂節點開始到棧底節點null遍曆66         while(top != null){67             System.out.println(top.data);68             top = top.next;69         }70     }71     72     //返回元素在棧中的位置73     public int getIndex(Object obj){74         int i = 0;75         while(top != null){76             if(peek().equals(obj)){77                 return i; 78             }79             top = top.next;80             i++;81         }82         return -1;83     }84     85     //返回棧的長度86     public int getSize(){87         return this.size;88     }89     90     public static void main(String[] args) {91         LinkedStack stack = new LinkedStack();92         for (int i = 0; i < 10; i++) {93             stack.push(i);94         }95 //        stack.display();96         System.out.println(stack.getIndex(9));97     }98     99 }

明天更新隊列和樹,然後是圖的深度優先,廣度優先,各種演算法。

 

 

 

 

  

 

 

  

  

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.