<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 97,  comments - 93,  trackbacks - 0

    com.ibm.nicky.stack--
                                  ++ExceptionStackEmpty
                                  ++Stack
                                  ++StackInterface  
                                  ++TestStack
    ********************************************************************************************

     1package com.ibm.nicky.stack;
     2
     3/**
     4 * @author QuQiang
     5 *
     6 *  Try to use pop(top) method when the Stack is null, ExceptionStackEmpty.
     7 */

     8public class ExceptionStackEmpty extends Exception{
     9     public ExceptionStackEmpty(String e){
    10      super(e);
    11     }

    12     
    13     private static final long serialVersionUID = 4983277878327382L;// Enable at once but have no impact on the sub-class
    14}

    15

     1package com.ibm.nicky.stack;
     2
     3/**
     4 * @author QuQiang
     5 *
     6 */

     7public class Stack<E> implements StackInterface<E> {
     8    
     9    private final static int CAPACITY = 1024// The default capacity of Stack  
    10    private int capacity = 0// The current capacity of Stack
    11    private Object[] element; // E array
    12    private int toppiont = -1// the current top piont 
    13
    14    public Stack(){
    15        this(CAPACITY);
    16    }

    17    public Stack(int cap){
    18        capacity = cap ;
    19        element = new Object[capacity];
    20    }

    21    /* (non-Javadoc)
    22     * @see com.ibm.nicky.stack.Stack#getSize()
    23     */

    24    public int getSize() {
    25        return (toppiont+1);
    26    }

    27
    28    /* (non-Javadoc)
    29     * @see com.ibm.nicky.stack.Stack#isEmpty()
    30     */

    31    public boolean isEmpty() {
    32        return (toppiont<0);
    33    }

    34
    35    /* (non-Javadoc)
    36     * @see com.ibm.nicky.stack.Stack#pop()
    37     */

    38    public E pop() throws ExceptionStackEmpty {
    39        E TopPiontValue;
    40        if(isEmpty()){
    41            throw new ExceptionStackEmpty("Stack is Empty");
    42        }

    43        TopPiontValue = (E) element[toppiont];
    44        element[toppiont--= null;
    45        return TopPiontValue;
    46    }

    47
    48    /* (non-Javadoc)
    49     * @see com.ibm.nicky.stack.Stack#push()
    50     */

    51    public void push(E object) {
    52        if(getSize() == capacity){
    53            creatNew();
    54        }

    55        element[++toppiont] = object;
    56    }

    57
    58    /* (non-Javadoc)
    59     * @see com.ibm.nicky.stack.Stack#top()
    60     */

    61    public E top() throws ExceptionStackEmpty {
    62        if(isEmpty()){
    63            throw new ExceptionStackEmpty("Stack is Empty");
    64        }

    65        return (E)element[toppiont];
    66    }

    67    
    68    private Object creatNew(){
    69        int size = 0;
    70        if(capacity == 0){
    71            size = 1;
    72        }
    else{
    73            size = CAPACITY;  // Not optimized method
    74        }

    75
    76        Object[] temp = new Object[element.length+size];
    77        System.arraycopy(element, 0, temp, 0, element.length);
    78        element = temp;
    79        System.out.println("hava invoke this createNew");
    80        System.out.print("the current size of the Stack is :"+ element.length);
    81        return element;
    82    }

    83
    84}

    85

     1package com.ibm.nicky.stack;
     2
     3/**
     4 * @author QuQiang
     5 * 
     6 * Stack ADT interface
     7 */

     8public interface StackInterface<E> {
     9    /**
    10     * @return boolean if the Stack is empty.
    11     */

    12    public boolean isEmpty();
    13
    14    /**
    15     * @return int, the Size of the Stack.
    16     */

    17    public int getSize();
    18
    19    /**
    20     * 
    21     * @return the Object of the Stack top but not delete it.
    22     * @throws ExceptionStackEmpty
    23     *             the Stack is empty
    24     */

    25    public E top() throws ExceptionStackEmpty;
    26
    27    /**
    28     * 
    29     * @return the Object of the Stack top but delete it.
    30     * @throws ExceptionStackEmpty
    31     *             the Stack is empty
    32     */

    33    public E pop() throws ExceptionStackEmpty;
    34
    35    /**
    36     * push a object to the Stack top.
    37     */

    38    public void push(E object);
    39}

    40

     1package com.ibm.nicky.stack;
     2
     3/**
     4 * @author QuQiang
     5 *
     6 */

     7public class TestStack {
     8
     9  public static void main(String[] args){
    10      Stack<Integer> testStack = new Stack<Integer>(2);
    11      
    12      testStack.push(21);
    13      testStack.push(12);
    14      testStack.push(-9);
    15      
    16      try {
    17        System.out.println("testStack.top()="+testStack.top());
    18          System.out.println("testStack.pop()="+testStack.pop());
    19          System.out.println("testStack.top()="+testStack.top());
    20    }
     catch (ExceptionStackEmpty e) {
    21        e.printStackTrace();
    22    }

    23  }

    24}
    posted on 2007-08-03 18:14 wqwqwqwqwq 閱讀(996) 評論(0)  編輯  收藏 所屬分類: Data Structure && Algorithm
    <2007年8月>
    2930311234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術(shù)網(wǎng)站

    java技術(shù)相關(guān)

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網(wǎng)網(wǎng)絡(luò)電話,中國最優(yōu)秀的網(wǎng)絡(luò)電話
    主站蜘蛛池模板: 亚洲色成人网站WWW永久四虎| 久久丫精品国产亚洲av不卡| 亚洲精品无码午夜福利中文字幕| 亚洲伊人色欲综合网| 亚洲国产成人久久精品影视| 亚洲乱码在线视频| 无码亚洲成a人在线观看| WWW国产成人免费观看视频| 久久久久久久岛国免费播放| 皇色在线视频免费网站| 免费又黄又硬又爽大片| 亚洲AV无码精品色午夜果冻不卡| 91亚洲国产成人久久精品网址| 亚洲av成人一区二区三区观看在线 | 亚洲AV无码第一区二区三区 | 男男gay做爽爽的视频免费| 中文字幕在线视频免费观看 | 精品成人免费自拍视频| 久久久久久久久免费看无码| 亚洲国产综合精品一区在线播放| 亚洲av日韩av高潮潮喷无码| 亚洲看片无码在线视频| 国产成人1024精品免费| 69av免费视频| 亚洲一区视频在线播放| 日韩亚洲Av人人夜夜澡人人爽| 亚洲七久久之综合七久久| 国产在线观看无码免费视频| 一个人免费观看视频www| 亚洲精品网站在线观看不卡无广告| 亚洲欧洲国产日韩精品| 国产成人亚洲精品无码AV大片| 久久99热精品免费观看动漫| 日本特黄特黄刺激大片免费| 久久精品国产精品亚洲蜜月| 亚洲AV无码国产剧情| 无码国产精品一区二区免费vr| 日韩免费a级在线观看| 亚洲午夜精品久久久久久人妖 | 亚洲国产成a人v在线观看| 成在人线av无码免费高潮水|