「模拟栈java」模拟栈算法是什么意思
今天给各位分享模拟栈java的知识,其中也会对模拟栈算法是什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java用数组模拟栈, 重写了tostring方法,却没有调用,输出的是地址,为什么?
- 2、JAVA语言 用数组来模拟堆栈
- 3、java只用栈判断字符串是否回文
- 4、java如何用数组来模拟栈的先进后出
- 5、JAVA中栈的问题
- 6、java 中用双向链表模拟栈
java用数组模拟栈, 重写了tostring方法,却没有调用,输出的是地址,为什么?
你好!
你的tostring()方法名错了,toString()才对。
希望对你有帮助!
JAVA语言 用数组来模拟堆栈
很容易 利用 一个数top计数 数组a表示栈顶,比如起初是 空的 top=0;1 进栈 top++;
2 进栈 top++;
出栈 top--
栈顶的数据 就是a[top-1] 当然top==0栈为空
java只用栈判断字符串是否回文
import java.util.ArrayList;
import java.util.List;
public class StackTest {
/**
* @param args
*/
public static void main(String[] args) {
String str = "abcba";
System.out.println(str + "回文数:" + isPalindrome(str));
str = "abccba";
System.out.println(str + "回文数:" + isPalindrome(str));
str = "123321";
System.out.println(str + "回文数:" + isPalindrome(str));
str = "1238887321";
System.out.println(str + "回文数:" + isPalindrome(str));
}
/**
* p判断输入字符串是否为回文/p
* @param pValue String 输入待判定的字符串
* @return boolean 是否是回文
*/
public static boolean isPalindrome(String pValue){
// 堆栈一
ListCharacter stack = new ArrayListCharacter();
// 堆栈二
ListCharacter stack2 = new ArrayListCharacter();
// 字符串长度的一半
int haflen = pValue.length()/2;
for(int i=0;ihaflen;i++){
// 字符进栈
stack.add(pValue.charAt(i));
// 倒序进栈
stack2.add(pValue.charAt(pValue.length()-i-1));
}
// 标识符
boolean bFlag = true;
// 出栈并比较
for(int i=haflen-1;i=0;i--){
if(stack.remove(i) != stack2.remove(i)){
bFlag = false;
break;
}
}
// 返回比对结果
return bFlag;
}
}
你这是作业题吧,其实最简单直接用循环然后charAt()把第一个和最后一个取出来比较就可以了。要用栈的话,我就用List来模拟栈。我们知道栈只有两个基本的操作进栈和出栈,用ArrayList的add(),remove()来模拟进栈和出栈。上面的代码和你的思路是一样的,但是简化掉奇偶数的判定,因为是奇数的时候,最中间一个我们可以不用管它,是偶数的话就是前半部分和后半部分的比较。
java如何用数组来模拟栈的先进后出
import java.util.Arrays;
public class StackTest {
public static void main(String[] args) {
Stack stack = new Stack();
stack.put(1);
stack.put(2);
stack.put(3);
stack.put(4);
stack.show();
System.out.println(stack.push());
System.out.println(stack.push());
System.out.println(stack.push());
System.out.println(stack.push());
}
}
class Stack{
private int index;
private int length = 3;
private int[] stack;
public Stack() {
stack= new int[length];
index = 0;
}
public void put(int i){
if(index = length -1){
stack[index++] = i;
} else {
System.out.println("Stack 已满");
}
}
public int push(){
if (index 0){
return stack[--index];
}else {
System.out.print("Stack 已空");
return -1;
}
}
public void show() {
System.out.println(Arrays.toString(stack));
}
}
JAVA中栈的问题
栈,其实是一种数据结构!你可以用数组实现,也可以用LinkedList实现!
栈的结构是先进的后出!
就你的问题,如果你用数组解决的话,就判断数组是不是空,就能知道模拟栈里有没有东西了!
关于第二个问题!~做一个简单的程序测试一下就好了·~其实和C++没有什么区别!
java 中用双向链表模拟栈
这里只是实现了Stack的部分功能
public class Stack{
private Node top;
public Stack(){
this.top = null;
}
public void push(Node node){
if(node == null)
return;
if(this.top == null){
this.top = node;
node.setNext(null);
node.setPre(null);
}
else{
this.top.setNext(node);
node.setPre(this.top);
node.setNext(null);
this.top = node;
}
}
public Node pop(){
if(this.top == null)
return null;
Node curr = this.top;
Node pre = curr.getPre();
pre.setNext(null);
this.top = pre;
return curr;
}
public Node top(){
return this.top;
}
public boolean isEmpty(){
return this.top == null ? true : false;
}
public void empty(){
this.top = null;
}
public static void main(String[] args){
Stack stack = new Stack();
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
System.out.println(stack.isEmpty());
stack.push(n1);
System.out.println(stack.top().getValue());
stack.push(n2);
stack.push(n3);
System.out.println(stack.pop().getValue());
stack.empty();
}
}
class Node {
private int value;
private Node next;
private Node pre;
public Node(int value, Node next, Node pre){
this.value = value;
this.next = next;
this.pre = pre;
}
public Node(int value){
this.value = value;
this.next = null;
this.pre = null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
}
模拟栈java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于模拟栈算法是什么意思、模拟栈java的信息别忘了在本站进行查找喔。
发布于:2022-12-05,除非注明,否则均为
原创文章,转载请注明出处。