最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 數(shù)據(jù)結(jié)構(gòu)之棧與隊列

 更新時間:2017年07月27日 15:00:59   投稿:lqh  
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)之棧與隊列的相關(guān)資料,這里對java中的棧和隊列都做出實現(xiàn)實例來幫助大家理解學習數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下

java 數(shù)據(jù)結(jié)構(gòu)之棧與隊列

一:對列

隊列是一種先進先出的數(shù)據(jù)結(jié)構(gòu)

實現(xiàn)代碼:

package Queue; 
 
/* 
 * 使用java構(gòu)建隊列,并模擬實現(xiàn)隊列的入隊和出對方法 
 */ 
 
public class Queue {   //隊列類 
 
  private int maxSize; //定義隊列的長度 
  private int[] arrQueue;   //隊列 
  private int rear;   //定義隊列的尾指針 
  private int front;  //定義隊列的頭指針 
  private int empty; //元素的個數(shù) 
   
  public Queue(int s)  //初始化構(gòu)造函數(shù) 
  { 
    maxSize = s; 
    arrQueue = new int[s]; 
    rear = -1; 
    front=0; 
    empty = 0; 
  } 
   
  //實現(xiàn)插入方法 
  public void insert(int m) 
  { 
    if(rear == maxSize-1)  //處理循環(huán) 
      rear = -1;    
    arrQueue[++rear] = m;  //對尾指針加一,把值放在隊列結(jié)尾 
    empty++;   //隊列元素個數(shù)加1 
    System.out.println("隊列入隊元素 為:" + m); 
  } 
   
  //實現(xiàn)出棧的方法,即取得隊列的頭元素 
  public int remove() 
  { 
    int temp = arrQueue[front++]; //將棧頂元素賦值給temp,棧頂指針加1 
    if(front == maxSize) //處理循環(huán) 
      front = 0; 
    empty--; //元素個數(shù)-1 
    return temp; 
  } 
   
  //判斷隊列是否為空 
  public boolean isEmpty() 
  { 
    return (empty==0); 
  } 
   
  //判斷對列是否為滿 
  public boolean isFull() 
  { 
    return (empty == maxSize); 
  } 
   
  //返回隊列長度 
  public int qLong() 
  { 
    return empty; 
  } 
   
  public static void main(String[] args) { 
    Queue q = new Queue(5); //初始化隊列為5個元素 
     
    q.insert(1); 
    q.insert(2); 
    q.insert(3); 
    q.insert(4); 
    q.insert(5); 
     
    int t1 = q.remove(); 
    System.out.println("隊列元素出隊:" + t1); 
    int t2 = q.remove(); 
    System.out.println("隊列元素出隊:" + t2); 
     
    System.out.println("隊列是否為空:" + q.isEmpty()); 
    System.out.println("隊列是否為滿:" + q.isFull()); 
    System.out.println("隊列的長度:" + q.qLong()); 
  } 
   
} 

二:棧

棧是一種先進后出的數(shù)據(jù)結(jié)構(gòu)

1:使用數(shù)組模擬棧

package Statck; 
/* 
 * 使用java構(gòu)建棧,并模擬實現(xiàn)棧的入棧和出棧方法 
 * 使用數(shù)組實現(xiàn) 
 */ 
 
public class Statck1 { 
 
  private int maxSize;   //棧的最多元素數(shù) 
  private int top;  //棧頂指針 
  private int len;   //棧的深度 
  private int[] arrStack; // 模擬棧 
   
  //棧的初始化 
  public Statck1(int s){ 
    maxSize = s; 
    len =0; 
    top= -1; 
    arrStack = new int[s]; 
  } 
   
  //獲取棧的長度 
  public int getLen(){ 
    return len; 
  } 
   
  //獲取當前棧還能插入多少個f元素 
  public int getLeaveLen(){ 
    return (maxSize-len); 
  } 
  //判斷棧是否滿 
  public boolean isFull(){ 
    return (len==maxSize); 
  } 
   
  //判斷棧是否為空 
  public boolean isEmpty(){ 
    return (len ==0); 
  } 
   
  //元素入棧 
  public void inStack(int s) 
  { 
    arrStack[++top] = s; //棧頂指針加1,入棧 
    System.out.println("元素入棧:" + s); 
    len ++ ;//棧深度+1 
  } 
   
  //元素出棧 
  public int outStack() 
  { 
    int temp = arrStack[top--];//賦值之后減1 
    System.out.println("元素出棧:" + temp); 
    len--;  //棧深度-1 
    return temp; 
  } 
   
  public static void main(String[] args) { 
    Statck1 s = new Statck1(5); 
     
    s.inStack(1); 
    s.inStack(2); 
    s.inStack(3); 
    s.inStack(4); 
    s.inStack(5); 
     
    s.outStack(); 
    s.outStack(); 
    System.out.println("棧的長度:" + s.getLen()); 
    System.out.println("還能入棧元素個數(shù):" + s.getLeaveLen()); 
    System.out.println("棧的是否為空:" + s.isEmpty()); 
    System.out.println("棧的是否為滿:" + s.isFull()); 
  } 
} 

2:使用鏈表模擬棧

package Statck; 
 
import java.util.ArrayList; 
import java.util.EmptyStackException; 
import java.util.List; 
 
/* 
 * 使用java構(gòu)建棧,并模擬實現(xiàn)棧的入棧和出棧方法 
 * 使用鏈表實現(xiàn) 
 */ 
 
public class Statck2<E extends Object> {  
   
  private List<E> statck = new ArrayList<E>();  
   
  public Statck2(){ 
       //棧的初始化 
  } 
   
  //清空棧 
  public void clear(){ 
    statck.clear(); 
    System.out.println("清空棧.........."); 
  } 
  //判斷棧是否為空 
  public boolean isEmpty(){ 
    return statck.isEmpty(); 
  } 
  //獲取棧頂元素 
  public E getTop(){ 
    if(isEmpty()) 
      return null; 
    return statck.get(0); 
  } 
   
  //彈出棧操作 
  public E pop(){ 
    if (isEmpty())  
      throw new EmptyStackException();  
    System.out.println(statck.size() + "\t 出棧"); 
    return statck.remove(statck.size() - 1);  
  } 
   
  //壓入棧操作 
  public void push(E e){ 
    statck.add(e); 
    System.out.println(e + "\t 入棧"); 
  } 
   
  //獲取當前棧的深度 
  public int getStatckSize(){ 
    if(isEmpty()) 
      throw new EmptyStackException(); 
    return statck.size(); 
  } 
   
  public static void main(String[] args) { 
    Statck2 s = new Statck2(); 
    s.clear();      //清空棧 
    System.out.println("當前棧是否為空:" + s.isEmpty()); 
    s.push(1); 
    s.push(2); 
    s.push(3); 
     
    s.pop(); 
    System.out.println("當前棧的深度為:" + s.getStatckSize()); 
    System.out.println("當前棧頂元素為:" + s.getTop()); 
  } 
   
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持,如有疑問請留言或者到本站社區(qū)交流討論,大家共同進步!

相關(guān)文章

最新評論

北票市| 大厂| 临汾市| 开江县| 福贡县| 平顺县| 车致| 启东市| 济源市| 凤冈县| 黄平县| 洛浦县| 丹凤县| 环江| 正宁县| 石渠县| 松潘县| 射阳县| 报价| 永川市| 东宁县| 沙雅县| 犍为县| 鹰潭市| 屯门区| 衡东县| 博白县| 达拉特旗| 灵石县| 芦山县| 睢宁县| 清镇市| 伊金霍洛旗| 海南省| 桃江县| 新疆| 若羌县| 建德市| 綦江县| 称多县| 肇源县|