zoukankan      html  css  js  c++  java
  • 关于JAVA数据结构中的栈操作

     
     1 package com.stack;
     2 
     3 
     4 //自定义异常类
     5 public class StackEmptyException extends Exception {
     6 
     7     private static final long serialVersionUID = 1L;
     8     public StackEmptyException(){
     9         
    10     }
    11     public StackEmptyException(String message){
    12         super(message);
    13     }
    14 
    15 }
    自定义异常类
     1 package com.stack;
     2 
     3 public interface Stack {
     4     //返回桟的大小
     5     public int size();
     6     //判断桟是否为空
     7     public boolean isEmpty();
     8     //数据元素入桟
     9     public void push(Object o);
    10     //栈顶元素出栈
    11     public Object pop() throws StackEmptyException;
    12     //取出栈顶元素
    13     public Object peek() throws StackEmptyException;
    14     //打印输出
    15     public String print();
    16 }
    栈的接口实现
     1 package com.stack;
     2 
     3 
     4 
     5 public class StackArray implements Stack{
     6 
     7     private final int LEN = 8;
     8     private Object [] elements;
     9     private int top;
    10     
    11     public StackArray(){
    12         top = -1;
    13         elements = new Object[LEN];
    14     }
    15     
    16     @Override
    17     public int size() {
    18         return (top +1);
    19     }
    20 
    21     @Override
    22     public boolean isEmpty() {
    23         return top<0;
    24     }
    25 
    26     @Override
    27     public void push(Object o) {
    28         expendSpace();        
    29         elements[++top] = o;        
    30     }
    31 
    32     public void expendSpace(){
    33         if(size()>=LEN){
    34             Object [] a = new Object[elements.length * 2];
    35             for(int i=0;i<elements.length;i++){
    36                 a[i] = elements[i];
    37             }
    38             elements = a;
    39         }
    40     }
    41     @Override
    42     public Object pop() throws StackEmptyException {
    43         if(size()<1){
    44             throw new StackEmptyException("ERROR!堆栈为空!");
    45         }
    46         Object obj = elements[top];
    47         elements[top--]=null;
    48         return obj;
    49     }
    50 
    51     @Override
    52     public Object peek() throws StackEmptyException {
    53         if(size()<1){
    54             throw new StackEmptyException("ERROR!桟为空!");
    55         }
    56         return elements[top];
    57     }
    58 
    59     @Override
    60     public String print() {
    61         String str = "(";
    62         for(int i= 0;i<top+1;i++){
    63             str += elements[i];
    64             str +=",";
    65         }
    66         str +=")";
    67         return str;
    68         
    69     }
    70 }
    栈的顺序存储实现
     1 package com.stack;
     2 
     3 public class Test {
     4 
     5     /**
     6      * @param args
     7      * @throws StackEmptyException 
     8      */
     9     public static void main(String[] args) throws StackEmptyException {
    10         StackArray sa = new StackArray();
    11                 //You Code here!!        
    12     }
    13 
    14 }
    测试

     

  • 相关阅读:
    OP_REQUIRES failed at conv_ops.cc:386 : Resource exhausted: OOM when allocating tensor with shape..
    Python中*args和**kwargs的区别
    命令行运行Python脚本时传入参数的三种方式
    关于 initWithNibName 和 loadNibNamed 的区别和联系-iPhone成长之路
    NSBundle介绍
    UIView总结
    iPhone How-to:如何调整UIView的Z-Order
    有关View的几个基础知识点-IOS开发
    NSNumber与NSInteger的区别
    iOS第三方开源库的吐槽和备忘
  • 原文地址:https://www.cnblogs.com/struCoder/p/3405749.html
Copyright © 2011-2022 走看看