数据结构——栈的使用
来源:互联网 发布:linux 查看文件内容 编辑:程序博客网 时间:2024/06/06 19:19
栈是常见的数据结构,stack可以说是内存中具有重要的用途,比如说在JVM中就有本地方法栈,在方法的调用中会产生调用栈,在主栈里面调用方法/函数(C语言也是如此),临时变量或者局部变量都会在stack结构中,stack是一种临时的内存模型,调用栈在方法退出时系统会得到回收。在微机系统中常会遇到中断的情况,从中断请求到保护断点以及中断程序响应执行与返回整个过程都与栈结构密切相关。
栈简单地说就是一种的先进后出的结构。一般情况下有入栈(push)与出栈(pop)基本操作。
下面创建一个数据栈,栈存放的数组。通常除了push、pop操作之外,还常有peek(查看)、判断是否栈容量来决定栈是否full或者Empty状态。
class StackDesign{ private int length=0; private int top=-1; int[] a=null; public StackDesign(int length) { this.length=length; a=new int[this.length]; } public void push(int data) { a[++top]=data; } public int pop() { int value=0; value=a[top--]; return value; } public boolean isEmpty() { return top==-1; } public boolean isFull() { return top==this.length-1; }}
以上对类StackDesign的设计基于数据先进后出的思想。在push之前需要判断stack是否isFull,在pop数据之前需要判断stack是否isEmpty。这些都是保证stack安全稳定工作必要的判断。
以下是测试代码:
StackDesign stackApp=new StackDesign(10); stackApp.push(6); stackApp.push(7); stackApp.push(8); stackApp.push(9); while(!stackApp.isEmpty()) { System.out.println(stackApp.pop()); }
测试结果是:
9876
通过以上的结果来看,类StackDesign符合先进后出的基本特征。stack使用比较广泛,比如说字符串反转,分隔符匹配等,总之凡是符合先进后出的条件均可以考虑stack结构来实现。
下面给出使用stack结构实现字符串反转功能。
class StringReverse{ char[] buffer=null; String s=null; public String getS() { return s; } public void setS(String s) { this.s = s; } public String getWord() { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("please input String :"); try { s=br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { br.close(); isr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } } public void reverse() { buffer=new char[this.getS().length()]; System.out.println(buffer.length); for(int i=0;i<buffer.length;i++) { buffer[i]=this.getS().charAt(i); } } public void reverseShow() { String sbuffer=""; for(int i=buffer.length-1;i>=0;i--) { sbuffer+=buffer[i]+""; } System.out.println(sbuffer); }}
字符创由用户输入所得。下面是测试代码:
StringReverse a=new StringReverse();String ddd=a.getWord();a.reverse();a.reverseShow();
以下是测试结果:
please input String :XYZ3ZYX
以上采用先进后出的思想只需要通过charAt()方法split各个字符后进行push即可。
在常见的IDE种,经常会自动提示匹配符的情况。例如{([] ) }这种情况下系统不会提示错误,但是系统检测到用户输入的文本中匹配符是这种情况系统就会提示:
{[( ]} 因为少了一个‘)’,编译器具备这种功能可以通过stack实现。具体过程为:当检测到左匹配符时顺序push压栈,当检测到右匹配符时对栈中的左匹配符进行匹配,若匹配成功则pop左匹配符。没找到左匹配符或者无匹配情况则系统输出提示信息。以下是实现代码:
class StackX{ private int length=0; int top; char[] a=null; StringGet sg=null; public StackX() { a=new char[100]; } public void push(char ch) { a[++top]=ch; } public char pop() { char value=0; value=a[top--]; return value; } public boolean isEmpty() { return top==-1; } public boolean isFull() { return top==100; }}
以上是创建一个基本的stack结构。
class StringGet{ private int length=0; String s; InputStreamReader isr=null; BufferedReader br=null; public StringGet() { isr=new InputStreamReader(System.in); br=new BufferedReader(isr); } public String str() { try { System.out.println("请输入字符串:"); s=br.readLine(); } catch (IOException e) { e.printStackTrace(); }finally { try { br.close(); isr.close(); } catch (IOException e) { e.printStackTrace(); } } return s; }}
以上创建类StringGet并获取用户输入的字符串(包括匹配符)。
class Check{ char[] charBuffer=null; int length; StackX stack=null; StringGet stringGet=null; public Check() { //stack=new StackX(); stringGet=new StringGet(); length=stringGet.str().length(); charBuffer=new char[length]; for(int i=0;i<length;i++) { char ch=stringGet.s.charAt(i); charBuffer[i]=ch; //System.out.println(charBuffer[i]); } } public void checkMatching() { stack=new StackX(); boolean flag=false; for(int i=0;i<charBuffer.length;i++) { switch(charBuffer[i]) { case '{':stack.push(charBuffer[i]); break; case '(':stack.push(charBuffer[i]); break; case '[':stack.push(charBuffer[i]); break; case ']': for(int j=0;j<charBuffer.length;j++) { if(stack.a[j]=='[') { System.out.println(stack.pop()); flag=true; } } if(!flag) { System.out.println("you should check code: short of '['"); } break; case ')': for(int j=0;j<charBuffer.length;j++) { if(stack.a[j]=='(') { System.out.println(stack.pop()); flag=true; } } if(!flag) { System.out.println("you should check code: short of '('"); } break; case '}': for(int j=0;j<charBuffer.length;j++) { if(stack.a[j]=='{') { System.out.println(stack.pop()); flag=true; } } if(!flag) { System.out.println("you should check code: short of '{'"); } break; default :break; } } }}
以上是check类,主要是完成匹配过程。
Check check=new Check(); check.checkMatching();
以上是测试语句。测试结果如下:
请输入字符串:xyz[][请输入字符串:xyz]you should check code: short of '['
可以看出以上能够实现基本的匹配符检测功能。对于更复杂的匹配符检测与匹配则需要完善check类,才能检测出多个匹配符嵌套的匹配。
0 0
- 数据结构——栈的使用
- 数据结构——栈的使用
- 数据结构——队列的使用
- 数据结构——队列的使用
- 数据结构——链表的使用
- 数据结构之栈的使用
- 数据结构—栈的应用
- 数据结构的应用——使用栈实现任意迷宫的求解
- 数据结构——dll的创建和使用
- 数据结构——队列的使用(二)
- 竞赛中使用的数据结构——二叉树
- 数据结构的应用——使用栈实现字符串括号匹配检查
- 数据结构的应用——使用栈和递归实现Hanoi问题求解
- 关于数据结构中栈的若干使用
- 使用LinkedList模拟栈数据结构的集合
- 数据结构—栈的顺序的实现
- 数据结构---->堆栈的使用
- 15.数据结构的使用。
- Shell 流程控制
- 多态——动态绑定
- 16.4.3
- Spring Boot入门及集成MyBatis开发Web项目
- BP算法改进
- 数据结构——栈的使用
- 工具类:自定义 pickerView
- RecyclerView的item点击事件
- IE11降级为IE8的方法
- 16.5.1
- GC调优在Spark应用中的实践
- Queue和Deque
- FAT12文件系统
- c#移位运算符("<<"及">>")详细说明