zoukankan      html  css  js  c++  java
  • 顺序栈(数组实现)

    上面代码实现了Stack的 isEmpty(),isFull(),clear(),push(),pop(),peek()方法。顺序栈,必须要同时检查下溢出(underflow)和上溢出(overflow)。

    public class Stack {
        private int[] stack;
        private static final int defaultSize = 100;
        private int size;
        private int topIndex;
        public Stack() {
            setUp(defaultSize); 
        }
        public Stack(int sz) {
            setUp(sz);
        }
        public void setUp(int sz) {
            size = sz; topIndex = 0; stack = new int[size];
        }
        public boolean isEmpty() {
            return topIndex == 0;
        }
        public boolean isFull() {
            return topIndex == size;
        }
        public void clear() {
            topIndex = 0;
        }
        public void push(int x) throws Exception {
            if(isFull()) throw new Exception("overflow");
            else stack[topIndex++] = x;
        }
        public int pop() throws Exception {
            if(isEmpty()) throw new Exception("underflow");
            else return stack[--topIndex];
        }
        public int peek() throws Exception {
            if(isEmpty()) throw new Exception("underflow");
            else return stack[topIndex-1];
        }
    }
  • 相关阅读:
    顧客満足度調査
    GeoStTool.dll过滤图层
    GeoStTool.ini相关技术要点
    GeoStTool.dll与RasterServer通信
    hdu 1007 Quoit Design
    hdu 4325 Flowers
    hdu 2516 取石子游戏
    hdu 1006 Tick and Tick
    CodeForces 101A Homework
    ZOJ Problem Set 1879
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4855848.html
Copyright © 2011-2022 走看看