热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于数组实现Java自定义Stack栈类及应用

栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出(Last-in-first-out,LIFO)的原则。java本身是有自带Stack类包,为了达到学习目的已经

栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出( Last-in-first-out,LIFO)的原则。java本身是有自带Stack类包,为了达到学习目的已经更好深入了解stack栈,自己动手自建java stack类是个很好的学习开始:

自建Java Stack 类

Stack 类:

 1 package com.stack;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 5 
 6 /**
 7  * Stack Class
 8  * @author ganyee
 9  *
10  */
11 public class Stack {
12     //Define capacity constant:CAPACITY
13     private static final int CAPACITY = 1024;
14     //Define capacity
15     private static int capacity;
16     //Define the top position of stack 
17     //top = -1 meaning that the stack empty
18     private static int top = -1;
19     //Basic Object class array
20     Object[] array;
21     //Initialize the capacity of stack
22     public Stack() {
23         this.capacity = CAPACITY;
24         array = new Object[capacity];
25     }
26 
27     //Get the size of stack
28     public int getSize(){
29         if(isEmpty()){
30             return 0;
31         }else{
32             return top + 1;
33         }
34     }
35 
36     //Get whether stack is empty
37     public boolean isEmpty(){
38         return (top <0);
39     }
40 
41     //Get the top element of stack
42     public Object top() throws ExceptionStackEmpty{
43 
44         if(isEmpty()){
45             throw new ExceptionStackEmpty("Stack is empty");
46         }
47         return array[top];
48 
49     }
50 
51     //Push element to stack
52     public void push(Object element) throws ExceptionStackFull{
53            if(getSize()== CAPACITY){
54                throw new ExceptionStackFull("Stack is full");
55            }
56            array[++ top] = element;
57     }
58 
59     //Pop element from stack
60     public Object pop() throws ExceptionStackEmpty{
61         if(isEmpty()){
62             throw new ExceptionStackEmpty("Stack is empty");
63         }
64         return array[top --];
65     }
66 
67     //Get the all elements of stack
68     public String getAllElements() throws ExceptionStackEmpty{
69         String[] arr = new String[top + 1];
70         if(!isEmpty()){
71             for(int i = 0;i ){
72                 arr[i] = (String)array[i];
73             }
74         }
75         return Arrays.toString(arr);
76     }
77 }

自定义ExceptionStackEmpty异常类

 1 package com.stack;
 2 
 3 public class ExceptionStackEmpty extends Exception {
 4 
 5     //Constructor
 6     public ExceptionStackEmpty(){
 7 
 8     }
 9 
10     //Define myself exception construct with parameters
11     public ExceptionStackEmpty(String string){
12         super(string);
13     }
14 }

自定义ExceptionStackFull异常类

 1 package com.stack;
 2 
 3 public class ExceptionStackFull extends Exception {
 4 
 5     //Constructor
 6         public ExceptionStackFull(){
 7 
 8         }
 9 
10         //Define myself exception construct with parameters
11         public ExceptionStackFull(String string){
12             super(string);
13         }
14 }

测试类:

 1 package com.stack;
 2 
 3 public class StackTest {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Stack stack= new Stack();
 8         System.out.println(stack.getSize());
 9         System.out.println(stack.isEmpty());
10         try {
11             stack.push(8);
12             stack.push(3);
13             stack.push(4);
14             stack.push(7);
15             stack.push(1);
16             stack.push(8);
17             stack.push(3);
18             stack.push(4);
19             stack.push(7);
20             stack.push(1);
21             System.out.println(stack.getSize());
22             System.out.println(stack.top());
23             System.out.println(stack.getAllElements());
24 
25             System.out.println(stack.pop());
26             System.out.println(stack.pop());
27             System.out.println(stack.pop());
28             System.out.println(stack.pop());
29             System.out.println(stack.pop());
30             System.out.println(stack.pop());
31             System.out.println(stack.pop());
32             System.out.println(stack.pop());
33             System.out.println(stack.pop());
34             System.out.println(stack.pop());
35 
36         } catch (ExceptionStackFull e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }catch (ExceptionStackEmpty e) {
40             // TODO Auto-generated catch block
41             e.printStackTrace();
42         }
43     }
44 
45 }

测试结果:

0
true
10
1
[8, 3, 4, 7, 1, 8, 3, 4, 7, 1]
1
7
4
3
8
1
7
4
3
8

栈的应用:符号匹配

下面,我们将借助一个栈结构 S,通过对算术表达式自左向右的一遍扫描,检查其中的括号是否匹配。 
假设算术表达式为 X = “x0x1x2…xn-1”,其中 xi 可以是括号、常数、变量名或者算术运算符。我们依次检查 X 中的各个符号,非括号的符号都可以忽略。若遇到左括号,则将其压入栈 S 中;若遇到右括号,则将栈顶符号弹出并与该右括号对比。如果发现某对括号不匹配,或者遇到右括号时栈为空,或者整个表达式扫描过后栈非空,都可以断定括号不匹配。 
在按照以上规则扫描完所有字符后,若栈为空,则说明括号是匹配的。如果按照前面对栈的实现,每一 push()和 pop()操作都只需常数时间,因此对于长度为 n 的算术表达式,上述算法需要运行 O(n)的时间。 
该算法的伪代码描述如 算法二.1 所示:

技术分享

 1 package com.stack;
 2 
 3 public class MatchClass {
 4 
 5     public static boolean Match(String str) throws ExceptionStackFull, ExceptionStackEmpty{
 6         Stack stack = new Stack();
 7         str = str.replaceAll(" ","");
 8         char s;
 9         for(int i = 0;i ){
10             if(str.charAt(i) == ‘(‘ || str.charAt(i) == ‘{‘ || str.charAt(i) == ‘[‘)
11                 stack.push(str.charAt(i));
12             else{
13                 if(stack.isEmpty())
14                     return false;
15                 else{
16                     s = str.charAt(i);
17                     switch(s){
18                     case ‘)‘:
19                         if((Character)stack.pop() != ‘(‘)
20                             return false;
21                         break;
22                     case ‘}‘:
23                         if((Character)stack.pop() != ‘{‘)
24                             return false;
25                         break;
26                     case ‘]‘:
27                         if((Character)stack.pop() != ‘[‘)
28                             return false;
29                         break;
30                     }
31                 }
32 
33             }
34         }
35         if(stack.isEmpty()){
36             return true;
37         }else{
38             return false;
39         }
40     }
41 }
 1 package com.stack;
 2 
 3 public class ParentMatch {
 4 
 5     public static void main(String[] args) {
 6 
 7         MatchClass match = new MatchClass();
 8         //String str = "()({})";  //Match
 9         //String str = "()({}) {([()[]])}";//Match
10         //String str = "([]{)";//Not match
11         //String str = ")([()] {}";//Not match
12         String str = "([())]{}";//Not match
13         try {
14             if(!match.Match(str)){
15                 System.out.println(str + ": Not Macth");
16             }else{
17                 System.out.println(str + ": Macth");
18             }
19         } catch (ExceptionStackFull e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         } catch (ExceptionStackEmpty e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         }
26     }
27 
28 }

测试结果:

()({}): Macth
()({}) {([()[]])}: Macth
([]{): Not Macth
)([()] {}: Not Macth
([())]{}: Not Macth

基于数组实现Java 自定义Stack栈类及应用


推荐阅读
  • 简单动态字符串redis里面很多地方都用到了字符串,我们知道redis是一个键值对存储的非关系型数据库,那么所有的key都是用字符串存储的,还有字符串类型,这些都是用字符串存储的 ... [详细]
  • 22.Container With Most Water(能装最多水的容器)
    thecontainercontainsthemos ... [详细]
  • FroggerTimeLimit:1000MSMemoryLimit:65536KTotalSubmissions:32257Accepted:10396DescriptionFr ... [详细]
  • spotify engineering culture part 1
    原文,因为原视频说的太快太长,又没有字幕,于是借助youtube,把原文听&打出来了。中文版日后有时间再翻译。oneofthebigsucceessfactorshereatSpo ... [详细]
  • Illustrator绘制逼真的愤怒的小鸟实例教程
    Illustrator教程: ... [详细]
  • 抓取百万知乎用户设计之实体设计
    一.实体的关系实体是根据返回的Json数据来设计的教育经历方面用户可以有很多教育经理,USER和education是一对多的关系,一个education对应一个education一 ... [详细]
  • iOS之富文本
    之前做项目时遇到一个问题:使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ... [详细]
  • Xib九宫格应用管理使用xib封装一个自定义view的步骤1新建一个继承UIView的自定义view,假设类名叫做(AppView)2新建一个AppView.xib文件来描述 ... [详细]
  • 【自制小工具】代码生成器
    【自制小工具】代码生成器陆陆续续接触过好几款代码生成工具,发现确实好用,但都会有那么点不完善的地方,所以索性就自己做一个吧。界面非常简单,反正是自己用的,简单点用起来也方便上图:左 ... [详细]
  • kepserver中文手册,kepserver使用教程,kepserver设置
    下面介绍一下KepServer模拟器的使用,以下示例使用服务器随附的Simulator驱动程序来演示创建、配置和运行项目的过程。Simulator驱动程序是基于内存的驱动程序,能为 ... [详细]
  • 论文阅读及复现 | Improved Semantic Representations From TreeStructured Long ShortTerm Memory Networks
    两种形式的LSTM变体Child-SumTree-LSTMsN-aryTree-LSTMshttps:paperswithcode.compaperimproved-semanti ... [详细]
  • 看这里,教你如何快速将pdf文件翻译成中文
    因为网上下载的PDF资料,往往掺杂着一些英文,所以中英文翻译是一件很平常的事,毕竟不是每个人的英文都那么好,轻轻松松的就能够看完一篇英文的文件,那么,我们就要寻找翻译工具来帮助我们 ... [详细]
  • 以SOA服务为导向的信息系统构建是通过有计划地构建信息系统时,一种简单而有柔性的方法,就是组件化与服务导向架构。过去的信息系统,是在使用者需要新功能时才开发的,也就是响应不同时 ... [详细]
  • Linux     系统安装
    Linux系统安装linux系统安装准备工作电脑、u盘、光盘、网络、硬盘主要使用光盘、网络虚拟化软件vmwarevi ... [详细]
  • 定义:定义两个数论函数\(f\)、\(g\)的Dirichlet卷积为:\[\left(f*g\right)\left(n\right)\sum_{d|n}f\left(d\rig ... [详细]
author-avatar
殇猿
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有