Advertisement
realever15

stack題組

May 28th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. =======================================
  2. package stack;
  3.  
  4. public class stack {
  5.  
  6.     private int top = 0; //堆疊頂端指標
  7.     private int[] stackArray;//堆疊用的陣列
  8.    
  9.     public stack(){
  10.         this(10);
  11.     }
  12.  
  13.     public stack(int size) {
  14.         stackArray = new int[size];
  15.     }
  16.    
  17.     public void push(int num){
  18.         //將值放入堆疊頂端
  19.         top++;
  20.         for(int i=top-1;i>0;i--)
  21.         {
  22.             stackArray[i]=stackArray[i-1];
  23.         }
  24.         stackArray[0]=num;
  25.         //堆疊頂端指標加ㄧ
  26.     }
  27.    
  28.     public int pop(){
  29.         //回傳堆疊頂端的值並將堆疊頂端指標減ㄧ
  30.         int temp=stackArray[0];
  31.         top--;
  32.         for(int i=1;i<stackArray.length;i++)
  33.         {
  34.             stackArray[0]=stackArray[i];
  35.         }
  36.         return temp;
  37.     }
  38.    
  39.     public void popAll(){
  40.         //彈出堆疊內所有資料(可以順便把值印出來,寫main()的時候方便使用)
  41.         top=0;
  42.         for(int i=0;i<stackArray.length;i++)
  43.         {   if(stackArray[i]==0)
  44.             continue;
  45.             System.out.print(stackArray[i]+" ");
  46.             stackArray[i]=0;
  47.         }
  48.         System.out.println();
  49.     }
  50.    
  51.     public int getTop() {
  52.         return this.top;
  53.     }
  54.    
  55.     public int getSize()
  56.     {
  57.         return stackArray.length;
  58.     }
  59.  
  60. }
  61. =======================================
  62. package stack;
  63.  
  64. public class Object {
  65.  
  66.     private int top = 0; //堆疊頂端指標
  67.     private String[] stackArray;//堆疊用的陣列
  68.    
  69.     public Object(){
  70.         this(10);
  71.     }
  72.  
  73.     public Object(int size) {
  74.         stackArray = new String[size];
  75.     }
  76.    
  77.     public void push(String num){
  78.         //將值放入堆疊頂端
  79.         top++;
  80.         for(int i=top-1;i>0;i--)
  81.         {
  82.             stackArray[i]=stackArray[i-1];
  83.         }
  84.         stackArray[0]=num;
  85.         //堆疊頂端指標加ㄧ
  86.     }
  87.    
  88.     public String pop(){
  89.         //回傳堆疊頂端的值並將堆疊頂端指標減ㄧ
  90.         String temp=stackArray[0];
  91.         top--;
  92.         for(int i=1;i<stackArray.length;i++)
  93.         {
  94.             stackArray[0]=stackArray[i];
  95.         }
  96.         return temp;
  97.     }
  98.    
  99.     public void popAll(){
  100.         //彈出堆疊內所有資料(可以順便把值印出來,寫main()的時候方便使用)
  101.         top=0;
  102.         for(int i=0;i<stackArray.length;i++)
  103.         {   if(stackArray[i]==null)
  104.             continue;
  105.             System.out.print(stackArray[i]+" ");
  106.             stackArray[i]=null;
  107.         }
  108.         System.out.println();
  109.     }
  110.    
  111.     public int getTop() {
  112.         return this.top;
  113.     }
  114.    
  115.     public int getSize() {
  116.         return stackArray.length;
  117.     }
  118. }
  119. =======================================
  120. package stack;
  121. import java.util.Scanner;
  122.  
  123. public class stackmain {
  124.  
  125.     public static void main(String[] args) {
  126.         Scanner scn = new Scanner(System.in);
  127.         stack stacka = new stack();
  128.         Object object = new Object();
  129.         //Test1
  130.         System.out.println("======Test1======");
  131.         System.out.printf("請輸入欲放入堆疊的值(輸入-99表示結束):");
  132.         int input1;
  133.         while(true)
  134.         {
  135.             input1 = scn.nextInt();
  136.             if(input1==-99)
  137.                 break;
  138.             stacka.push(input1);
  139.         }
  140.        
  141.         System.out.print("輸出的堆疊值依序為:");
  142.         stacka.popAll();
  143.        
  144.         //Test2
  145.                 System.out.println("======Test2======");
  146.                 System.out.printf("請輸入欲放入堆疊的值(輸入-99表示結束):");
  147.                 String input2;
  148.                 while(true)
  149.                 {
  150.                     input2 = scn.next();
  151.                     if(input2.equals("-99"))
  152.                         break;
  153.                     object.push(input2);
  154.                 }
  155.                
  156.                 System.out.print("輸出的堆疊值依序為:");
  157.                 object.popAll();
  158.  
  159.     }
  160.  
  161. }
  162. =======================================
  163. package stack;
  164. import java.util.Scanner;
  165. public class safeStack extends stack {
  166.    
  167.     public safeStack(){
  168.         super();
  169.     }
  170.     public safeStack(int size){
  171.         super(size);
  172.     }
  173.    
  174.     public void push(int num){
  175.         if(getTop()==getSize())
  176.         {
  177.             System.out.println("堆疊已滿,無法存入"+num);
  178.         }
  179.         else
  180.             super.push(num);
  181.     }
  182.  
  183.     public static void main(String[] args)
  184.     {
  185.         Scanner scn = new Scanner(System.in);
  186.         System.out.print("請輸入欲放入堆疊的值(輸入-99表示結束):");
  187.         int input1;
  188.         stack stackb = new safeStack(5);
  189.         while(true)
  190.         {
  191.             input1 = scn.nextInt();
  192.             if(input1==-99)
  193.                 break;
  194.             stackb.push(input1);
  195.         }
  196.        
  197.         System.out.print("輸出的堆疊值依序為:");
  198.         stackb.popAll();
  199.  
  200.     }
  201.  
  202. }
  203. =======================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement