首页 > 代码库 > 顺序栈的实现

顺序栈的实现

        今天接着谈谈栈这个基本的数据结构。栈是后进先出(LIFO)的数据结构,栈的基本模型如下图:

 

       

        下面用java编程语言对栈的实现进行详细描述:

        Stack.java      顺序栈的接口

 1 package com.yeyan.seqstack; 2 /** 3  * 顺序栈的接口 4  * @author yeyan 5  * @date   2014-12-8 6  * API: 7  * push(Object obj)              入栈 8  * pop()                         出栈 9  * getTop()                      获取栈顶元素10  * isEmpty()                     判断栈是否为空11  */12 public interface Stack {13     //入栈14     public void push(Object obj) throws Exception;15     //出栈16     public Object pop() throws Exception;17     //获取栈顶元素18     public Object getTop() throws Exception;19     //判断栈是否为空20     public boolean isEmpty() throws Exception;21 }

        SeqStack.java    顺序栈的实现类

 1 package com.yeyan.seqstack; 2 /** 3  * 顺序栈实现类 4  * @author yeyan 5  * @date   2014-12-08 6  * 7  */ 8 public class SeqStack implements Stack { 9     private Object[] stack;                     //对象数组10     private final int defaultSize = 10;         //默认长度11     private int top;                            //栈顶位置12     private int maxSize;                        //最大长度13 14     /**15      * 不带参的构造函数16      */17     public SeqStack(){18         this.maxSize = defaultSize;19         top = 0;20         stack = new Object[defaultSize];21     }22     /**23      * 带参构造函数24      */25     public SeqStack(int size){26         this.maxSize = size;27         top = 0;28         stack = new Object[size];29     }30     31     /**32      * 入栈的实现33      */34     @Override35     public void push(Object obj) throws Exception {36         //先判断栈是否已满37         if(top == maxSize) {38             throw new Exception("堆栈已满!");39         } else {40             stack[top] = obj;41             top ++;42         }43     }44 45     /**46      * 出栈的实现47      */48     @Override49     public Object pop() throws Exception {50         if(isEmpty()) {51             throw new Exception("栈为空!");52         }53         top --;54         return stack[top];55     }56 57     /**58      * 获取栈顶元素59      */60     @Override61     public Object getTop() throws Exception {62         if(isEmpty()) {63             throw new Exception("栈为空!");64         }65         return stack[top-1];66     }67 68     /**69      * 判断栈是否为空70      */71     @Override72     public boolean isEmpty() throws Exception {73         return top == 0;74     }75 76 }

        Test.java     顺序栈测试类,测试入栈、出栈操作

 1 package com.yeyan.seqstack; 2 /** 3  * 顺序栈测试类 4  * @author yeyan 5  * @date   2014-12-08 6  * 7  */ 8 public class Test { 9 10     public static void main(String[] args) {11         SeqStack seqStack = new SeqStack(20);12         //入栈测试(用10个元素模拟)13         System.out.println("请输入10个整数:");14         for(int i = 0; i < 10; i ++) {15             try {16                 seqStack.push(i);17             } catch (Exception e) {18                 e.printStackTrace();19             }20         }21         //出栈测试22         for(int i = 0; i < 10; i ++) {23             try {24                 Object obj = seqStack.pop();25                 System.out.println(Integer.parseInt(obj.toString()));26             } catch (Exception e) {27                 e.printStackTrace();28             }29         }30     }31 32 }

    

顺序栈的实现