使用數組實現棧和迴圈隊列(JAVA語言)

來源:互聯網
上載者:User

這篇文章將講述兩個問題。一是如何使用數組實現一個棧,棧的大小可以伸縮。二是使用數組實現一個隊列,為了充分的利用數組的空間,這裡實現一個迴圈,構成迴圈隊列。

(1)基於數組實現棧

package com.datastructure.stack;import java.util.*;public class MyStack<T> {private Object[] array=null;private int capality;private final int defaultsize=10;private int size=0;private int top=-1;public MyStack(){this.capality=this.defaultsize;this.array=new Object[this.capality];this.size=0;}public MyStack(int capality){this.capality=capality;this.array=new Object[capality];this.size=0;}public boolean isEmpty(){return size==0;}public int size(){return this.size;}public void push(T obj){if(this.size<this.capality){this.top++;this.array[top]=obj;this.size++;}else{enableCapality();push(obj);}}private void enableCapality(){this.capality=this.capality*2;Object[] newarray=new Object[this.capality];System.arraycopy(array, 0, newarray, 0, this.size);this.array=newarray;}public T pop(){if(isEmpty()){try {throw new Exception("棧為空白!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}T temp=(T)array[this.top];array[this.top]=null;this.top--;this.size--;return temp;}public static void main(String[] args) {// TODO Auto-generated method stubMyStack<Integer> stack=new MyStack<Integer>();stack.push(1);stack.push(5);stack.push(2);stack.push(4);stack.push(3);while(!stack.isEmpty()){System.out.println(stack.pop());}}}

運行結果如下所示:

34251

(2)基於數組實現迴圈隊列

package com.datastructure.stack;public class MyQueue {private Object[] array=null;private int size;private int front;private int back;private boolean beginflag=true;private boolean endflag=false;public MyQueue(int size){array=new Object[size];this.size=size;front=0;back=0;}public MyQueue(){this(10);}public void enqueue(Object obj){if(isFull()){try {throw new Exception("隊列已滿!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}array[back]=obj;back=(back+1)%size;}public Object dequeue(){if(isEmpty())try {throw new Exception("隊列為空白!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}Object temp=array[front];array[front]=null;front=(front+1)%size;return temp;}public boolean isEmpty(){if(front==back){if(!endflag){endflag=true;return false;}return true;}elsereturn false;}private boolean isFull(){if((back+1)%size==front){if(beginflag){beginflag=false;return false;}return true;}else{return false;}}public static void main(String[] args) {// TODO Auto-generated method stubMyQueue queue=new MyQueue(5);queue.enqueue(1);queue.enqueue(2);queue.enqueue(3);queue.enqueue(4);queue.enqueue(5);System.out.println(queue.dequeue());System.out.println(queue.dequeue());System.out.println(queue.dequeue());queue.enqueue(5);System.out.println(queue.dequeue());System.out.println(queue.dequeue());System.out.println(queue.dequeue());queue.enqueue(6);System.out.println(queue.dequeue());}}

運行結果如下所示:

1234556

 

相關文章

聯繫我們

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