Java数据结构之栈的线性结构详解

目录

  • 一:栈
  • 二:栈的实现
  • 三:栈的测试
  • 四:栈的应用(回文序列的判断)
  • 总结

一:栈
【Java数据结构之栈的线性结构详解】栈是限制插入和删除只能在一个位置上进行的表,此位置就是表的末端,叫作栈顶。
栈的基本操作分为push(入栈) 和 pop(出栈),前者相当于插入元素到表的末端(栈顶),后者相当于删除栈顶的元素。
Java数据结构之栈的线性结构详解
文章图片


二:栈的实现
public class LinearStack {/*** 栈的初始默认大小为10*/private int size = 5; /*** 指向栈顶的数组下标*/int top = -1; /*** 定义栈stack*/private int[] stack; public LinearStack() {stack = new int[size]; }/*** 判断栈满*/publicboolean isFull() {boolean result = false; if(top == size - 1) {result = true; }return result; }/*** 入栈操作push*/publicvoid push(int value) {/*** 如果栈满,拓展栈的容量*/if(isFull())stack = expansionStack(); top++; stack[top] = value; }/*** 出栈操作*/publicintpop() {if(top == -1)throw new RuntimeException("栈空!出栈失败"); int result = stack[top] ; top--; return result; }/*** 扩充容量*/publicint[] expansionStack() {size = size + 10; int[] stackTemp = new int[size]; for (int i = 0; i < stack.length; i++) {stackTemp[i] = stack[i]; }return stackTemp; }/*** 获取栈顶的元素*/public int getTop() {return stack[top]; }/*** 显示栈中的全部元素*/publicString toString() {String str = "["; for (int i = 0; i <= top; i++) {if(i == top)str = str + stack[i] + "]"; elsestr = str + stack[i] + ","; }return str; }}


三:栈的测试
public class LinearStackTest {public static void main(String[] args) {LinearStack linearStack = new LinearStack(); /*** 元素入栈*/linearStack.push(1); linearStack.push(2); linearStack.push(3); linearStack.push(4); linearStack.push(5); /*** 栈满,显示栈中所有元素*/System.out.println("0:arrayStack" + linearStack.toString()); /*** 再次入栈*/linearStack.push(6); /*** 再次显示占中的所有元素*/System.out.println("1:arrayStack:" + linearStack.toString()); /*** 获取栈顶元素*/System.out.println("获取栈顶元素:stack[top] = " + linearStack.getTop()+"top = " + linearStack.top); /*** 出栈*/System.out.println("出栈:stack[top] = " + linearStack.pop()+"top = " + linearStack.top); /*** 再次显示栈中的元素*/System.out.println("2:arrayStack:" + linearStack.toString()); }}


四:栈的应用(回文序列的判断)
public class LinearStackChar {private int size = 5; /*** 指向栈顶的数组下标*/int top = -1; /*** 定义栈stack*/private char[] stack; public LinearStackChar() {stack = new char[size]; }/*** 判断栈满*/publicboolean isFull() {boolean result = false; if(top == size - 1) {result = true; }return result; }/*** 入栈操作push*/public void push(char value) {/*** 如果栈满,拓展栈的容量*/if(isFull())stack = expansionStack(); top++; stack[top] = value; }/*** 出栈操作*/publiccharpop() {if(top == -1)throw new RuntimeException("栈空!出栈失败"); char result = stack[top] ; top--; return result; }/*** 扩充容量*/public char[] expansionStack() {size = size + 10; char[] stackTemp = new char[size]; for (int i = 0; i < stack.length; i++) {stackTemp[i] = stack[i]; }return stackTemp; }/*** 获取栈顶的元素*/public char getTop() {return stack[top]; }/*** 显示栈中的全部元素*/publicString toString() {String str = "["; for (int i = 0; i <= top; i++) {if(i == top)str = str + stack[i] + "]"; elsestr = str + stack[i] + ","; }return str; }}

public class LinearStackCharTest {public static void main(String[] args) {/*** 判断一个字符串abcba是不是回文序列?* 思路:将字符串切割成为单个字符,存放在字符栈中;*然后出栈,判断出栈后字符数组组成的字符串是否和原字符串相等;*相等--回文序列*不相等--不是回文序列*/String str = "abcba"; LinearStackChar linearStackChar = new LinearStackChar(); //讲字符串切割,存放在栈中for (int i = 0; i < str.length(); i++) {linearStackChar.push(str.charAt(i)); }//存放完成,显示栈中的元素System.out.println("stack = " + linearStackChar.toString()); //出栈String result = ""; int length = linearStackChar.top; System.out.println("top = " + length); for (int i = 0; i <= length; i++) {result= result + String.valueOf(linearStackChar.pop()); }//出栈组成的字符串System.out.println("result = " + result); //判断是否相等System.out.println("result = abcba?" + (result.equals("abcba") ? true : false)); }}


总结 到此这篇关于Java数据结构之栈的线性结构的文章就介绍到这了,更多相关Java栈的线性结构内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读