集合_用List實現棧和隊列

   1、棧的實現

             1)棧的功能:進棧、出棧、返回棧口元素……

            

            2)詳解的過程看代碼吧:

      

[java] view plain copy
  1. import java.util.*;  
  2.   
  3. //1藉助LinkedList 類中的方法實現棧  
  4. public class MyStack {  
  5.     private LinkedList<Object> li=new LinkedList<Object>();  
  6.       
  7.     //1構造方法  
  8.     public MyStack(){  
  9.           
  10.     }  
  11.       
  12.     //2出棧  
  13.     public Object pop(){  
  14.         if(isEmpty()){  
  15.             throw new EmptyStackException();  
  16.         }  
  17.         return li.removeFirst();  
  18.     }  
  19.       
  20.     //3進棧  
  21.     public void push(Object obj){ //注意o不要0的區別,不要寫成0了  
  22.         li.addFirst(obj);  
  23.     }  
  24.       
  25.     //4清空  
  26.      public void clear() {  
  27.            li.clear();  
  28.         }  
  29.     //5判斷是否爲空  
  30.     public boolean isEmpty(){  
  31.         return li.isEmpty();  
  32.     }  
  33.       
  34.     //6 將對象轉換成字符串  
  35.     public String toString(){  
  36.         return li.toString();  
  37.     }  
  38.        
  39.      //7返回棧口元素  
  40.     public Object peek(){  
  41.         if(isEmpty()){  
  42.             throw new EmptyStackException();  
  43.         }  
  44.         return li.peekFirst();  
  45.           
  46.     }  
  47.       
  48.     public static void main(String[] args) {  
  49.         MyStack stack=new MyStack();  
  50.         //進棧  
  51.         stack.push("a");  
  52.         stack.push("b");  
  53.           
  54.         //出棧  
  55.         System.out.println(stack.pop());  
  56.           
  57.         //返回棧口元素  
  58.         System.out.println(stack.peek());  
  59.           
  60.   
  61.     }  
  62.   
  63. }  
    

  2、隊列的實現:

       1)隊列的功能:隊尾進,隊首出、....

       2)詳細的見代碼:

[java] view plain copy
  1. import java.util.*;  
  2.   
  3. //藉助LinkedList 類中的方法實現隊列  
  4. public class MyQueue {  
  5.     private LinkedList<Object> li = new LinkedList<Object>();  
  6.   
  7.     // 1構造方法  
  8.     public MyQueue() {  
  9.   
  10.     }  
  11.   
  12.     // 2出列  
  13.     public Object get() {  
  14.         if (isEmpty()) {  
  15.             throw new EmptyStackException();  
  16.         }  
  17.         return li.removeFirst();  
  18.     }  
  19.   
  20.     // 3進列  
  21.     public void put(Object obj) {  
  22.         li.addLast(obj);  
  23.     }  
  24.   
  25.     // 4清空  
  26.     public void clear() {  
  27.         li.clear();  
  28.     }  
  29.   
  30.     // 5 返回隊列首元素(不刪除)  
  31.     public Object getTop() {  
  32.         if (isEmpty()) {  
  33.             throw new EmptyStackException();  
  34.         }  
  35.         return li.peekFirst();  
  36.     }  
  37.   
  38.     // 6將對象轉換成字符串  
  39.     public String toString() {  
  40.         return li.toString();  
  41.     }  
  42.   
  43.     // 7判斷隊列是否爲空  
  44.     public boolean isEmpty() {  
  45.         return li.isEmpty();  
  46.     }  
  47.   
  48.     public static void main(String[] args) {  
  49.         MyQueue mq = new MyQueue();  
  50.         // 進列  
  51.         mq.put("a");  
  52.         mq.put("b");  
  53.         mq.put("c");  
  54.   
  55.         // 出列  
  56.         System.out.println(mq.get());  
  57.         System.out.println(mq.get());  
  58.   
  59.         // 返回對首元素  
  60.         System.out.println(mq.getTop());  
  61.   
  62.     }  
  63. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">      </span>  

轉載自:http://blog.csdn.net/zsw101259/article/details/7751011

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章