这期内容当中小编将会给大家带来有关Java中栈的线性结构是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。一:栈栈是限制插入和删除只能在一个位置上进行的表,此位置就是表的末端,叫作栈顶。栈的基本操作分为
这期内容当中小编将会给大家带来有关Java中栈的线性结构是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
栈是限制插入和删除只能在一个位置上进行的表,此位置就是表的末端,叫作栈顶。
栈的基本操作分为push(入栈) 和 pop(出栈),前者相当于插入元素到表的末端(栈顶),后者相当于删除栈顶的元素。
public class LinearStack { private int size = 5; int top = -1; private int[] stack; public LinearStack() { stack = new int[size]; } public boolean isFull() { boolean result = false; if(top == size - 1) { result = true; } return result; } public void push(int value) { if(isFull()) stack = expansionStack(); top++; stack[top] = value; } public int pop() { if(top == -1) throw new RuntimeException("栈空!出栈失败"); int result = stack[top] ; top--; return result; } public int[] 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]; } public String toString() { String str = "["; for (int i = 0; i <= top; i++) { if(i == top) str = str + stack[i] + "]"; else str = 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; private char[] stack; public LinearStackChar() { stack = new char[size]; } public boolean isFull() { boolean result = false; if(top == size - 1) { result = true; } return result; } public void push(char value) { if(isFull()) stack = expansionStack(); top++; stack[top] = value; } public char pop() { 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]; } public String toString() { String str = "["; for (int i = 0; i <= top; i++) { if(i == top) str = str + stack[i] + "]"; else str = str + stack[i] + ","; } return str; }}
public class LinearStackCharTest { public static void main(String[] args) { 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中栈的线性结构是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网精选频道。
--结束END--
本文标题: Java中栈的线性结构是什么
本文链接: https://lsjlt.com/news/299749.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0