Java 如果两个堆栈元素相同,则访问堆栈顶部元素以进行递归比较的方法

Java 如果两个堆栈元素相同,则访问堆栈顶部元素以进行递归比较的方法,java,arrays,data-structures,stack,compare,Java,Arrays,Data Structures,Stack,Compare,如何访问每个堆栈的顶部元素以便比较它们?我使用了(iTop、i2Top、nTop、n2Top),因为我一直在进行测试,以使其工作,但我似乎无法获得正确的输出。 我做错了什么 我以为这会奏效,但也不行。基本上我想引用stack的top元素,并将其与stack2的top元素进行比较。然后,如果它们相等,则弹出它们并对堆栈的其余部分执行相同的操作。最后,如果堆栈是空的,因为所有元素都相同并被弹出,那么堆栈确实是相同的。并将堆栈1和堆栈2推回。 多谢各位 public Boolean sameStack

如何访问每个堆栈的顶部元素以便比较它们?我使用了(iTop、i2Top、nTop、n2Top),因为我一直在进行测试,以使其工作,但我似乎无法获得正确的输出。 我做错了什么

我以为这会奏效,但也不行。基本上我想引用stack的top元素,并将其与stack2的top元素进行比较。然后,如果它们相等,则弹出它们并对堆栈的其余部分执行相同的操作。最后,如果堆栈是空的,因为所有元素都相同并被弹出,那么堆栈确实是相同的。并将堆栈1和堆栈2推回。 多谢各位

public Boolean sameStack(StackArray s2){
e1,t2;
//E iTop=本项目[顶部];
//E i2Top=s2.项目[顶部];
int nTop=this.top;//
int n2Top=s2.top;//即使元素不相等,nTop==n2Top也是真的
布尔t=真;
if(this.isEmpty()&&s2.isEmpty()){
返回t;
}//如果
if(this.top!=s2.top){
t=假;
返回t;
}   
if(this.items[top]==s2.items[s2.top]){
t1=this.pop();
t2=s2.pop();
t=sameStack(s2);
推(t1);
推(t2);
}
否则{
t=假;
}
返回t;
}
公共类堆栈数组{
私有int top=-1;
私有静态最终整数最大项=10;
私人电子物品【】;
@抑制警告(“未选中”)
公共堆栈数组(){
items=(E[])新对象[MAX_items];
System.out.println(“堆栈已创建!”);
}
公共空间推送(E){
如果(isFull()==true){
System.out.println(“堆栈已满!”);
}
否则{
顶部=顶部+1;
项目[顶部]=e;
}
}//推
公共E-pop(){
如果(isEmpty()==true){
System.out.println(“堆栈为空!”);
}
否则{
E=(E)项目[顶部];
项目[顶部]=空;
top=top-1;
返回e;
}
返回null;
}//流行音乐
公共布尔值isFull(){
if(top==items.length-1){
返回true;
}
返回false;
}//已满
公共布尔值为空(){
如果(顶部==-1){
返回true;
}
返回false;
}//空空如也
@凌驾
公共字符串toString()
{
System.out.println(“数组:”);
系统输出打印(“{”);
对于(int i=0;i
只需迭代
数组,并使用
equals()
比较元素(而不是
=

public boolean sameStack(StackArray s2){
if(this.top!=s2.top){
返回false;
}

对于(inti=0;i如果您特别需要使用递归来测试堆栈的相等性,那么一个更简单的方法就是将索引传递给该方法。这样您就不需要推送和弹出

public boolean equals(Stack<E> other) {
    return equalsFrom(other, 0);
}

private boolean equalsFrom(Stack<E> other, int from) {
    if (from >= this.top || from >= other.top)
        return from >= this.top && from >= other.top;
    else
        return this.items[from].equals(other.items[from])
            && equalsFrom(other, from + 1);
}
public boolean equals(堆栈其他){
返回equalsFrom(其他,0);
}
专用布尔值equalsFrom(堆栈其他,int-from){
if(from>=this.top | | from>=other.top)
返回from>=this.top&&from>=other.top;
其他的
返回此.items[from].equals(其他.items[from])
&&equalsFrom(其他,从+1开始);
}

希望该方法对您有意义。if语句的第一个分支可以理解为“如果我到达了任一堆栈的末尾,那么我必须到达两个堆栈的末尾才能使它们相等”。第二个分支可以理解为“如果我没有到达任何一个堆栈的末尾,那么当前元素必须相等,其余堆栈必须相等,才能使整个堆栈相等。”

海绵宝宝meme许多小时后…………我终于自己解决了

import java.util.NoSuchElementException;

public class StackArray<E> {

        private int top=-1;
        private static final int MAX_ITEMS = 10;
        private E items[];

        @SuppressWarnings("unchecked")
        public StackArray() {
            items = (E[]) new Object[MAX_ITEMS];
            System.out.println("Stack Created!");
        }

        public void push(E e) {
            if (isFull()==true) {
               System.out.println("Stack Full!");
            }
            else{
                top=top+1;
                items[top] = e;
            }
        }

        public E pop() {
             if (isEmpty()==true) {
                   System.out.println("Stack Empty!");
                }
             else{
            E e = (E) items[top];
            items[top] = null;
            top = top-1;
            return e;
             }
            return null;
        }

        public boolean isFull() {
             if (top == items.length-1) {
                 return true;
             }
             return false;
        }

        public boolean isEmpty(){
             if (top==-1) {
                 return true;
             }
             return false;
        }

        @Override
        public String toString()
        {
            System.out.println("Array:");
            System.out.print("{");
             for(int i = 0; i < items.length ;i++) {
                 System.out.print(items[i]+" ");
        }
             System.out.print("}");
            return "";
    }

        public int peek() {
            if (this.isEmpty()) throw new NoSuchElementException("Stack underflow");
            //System.out.println("items[top]"+this.items[top]);
            //System.out.println("e"+e);
            return (int) this.items[top];
        }


        public Boolean sameStack(StackArray<E> s2) {
            E t1, t2;
            boolean t = true;

            if(this.isEmpty() && s2.isEmpty()) {
                return t;
            }

            if ((int) this.peek() == s2.peek()) {
                t1 = this.pop();
                t2 = s2.pop();
                t = sameStack(s2);
                this.push(t1);
                s2.push(t2);
            }
            else {
                t = false;
                return t;
            }   
            return t;           
        }

        public static void main(String[] args) 
        {
            // Code reference for sameStack method
           StackArray<Integer> stack = new StackArray<Integer>();
           StackArray<Integer> stack2 = new StackArray<Integer>();

            stack.push(122);
            stack.push(222);
            stack.push(3313);
            stack.push(46);
            stack2.push(122);
            stack2.push(222);
            stack2.push(3313);
            stack2.push(46);
            System.out.println(stack);
            System.out.println(stack2);

            stack.peek();
            if (stack.sameStack(stack2) == true) {
                System.out.println("They are equal");
            }
            else {
                System.out.println("They are NOT equal");
            }

            System.out.println(stack);
            System.out.println(stack2);

            //Calling comparison method
           // stack.sameStack(stack2); 
        }
}
import java.util.NoSuchElementException;
公共课
public boolean sameStack(StackArray<E> s2) {
    if (this.top != s2.top) {
        return false;
    }
    for (int i = 0; i <= this.top; i++) {
        if (! this.items[i].equals(s2.items[i])) {
            return false;
        }
    }
    return true;
}
public boolean equals(Stack<E> other) {
    return equalsFrom(other, 0);
}

private boolean equalsFrom(Stack<E> other, int from) {
    if (from >= this.top || from >= other.top)
        return from >= this.top && from >= other.top;
    else
        return this.items[from].equals(other.items[from])
            && equalsFrom(other, from + 1);
}
import java.util.NoSuchElementException;

public class StackArray<E> {

        private int top=-1;
        private static final int MAX_ITEMS = 10;
        private E items[];

        @SuppressWarnings("unchecked")
        public StackArray() {
            items = (E[]) new Object[MAX_ITEMS];
            System.out.println("Stack Created!");
        }

        public void push(E e) {
            if (isFull()==true) {
               System.out.println("Stack Full!");
            }
            else{
                top=top+1;
                items[top] = e;
            }
        }

        public E pop() {
             if (isEmpty()==true) {
                   System.out.println("Stack Empty!");
                }
             else{
            E e = (E) items[top];
            items[top] = null;
            top = top-1;
            return e;
             }
            return null;
        }

        public boolean isFull() {
             if (top == items.length-1) {
                 return true;
             }
             return false;
        }

        public boolean isEmpty(){
             if (top==-1) {
                 return true;
             }
             return false;
        }

        @Override
        public String toString()
        {
            System.out.println("Array:");
            System.out.print("{");
             for(int i = 0; i < items.length ;i++) {
                 System.out.print(items[i]+" ");
        }
             System.out.print("}");
            return "";
    }

        public int peek() {
            if (this.isEmpty()) throw new NoSuchElementException("Stack underflow");
            //System.out.println("items[top]"+this.items[top]);
            //System.out.println("e"+e);
            return (int) this.items[top];
        }


        public Boolean sameStack(StackArray<E> s2) {
            E t1, t2;
            boolean t = true;

            if(this.isEmpty() && s2.isEmpty()) {
                return t;
            }

            if ((int) this.peek() == s2.peek()) {
                t1 = this.pop();
                t2 = s2.pop();
                t = sameStack(s2);
                this.push(t1);
                s2.push(t2);
            }
            else {
                t = false;
                return t;
            }   
            return t;           
        }

        public static void main(String[] args) 
        {
            // Code reference for sameStack method
           StackArray<Integer> stack = new StackArray<Integer>();
           StackArray<Integer> stack2 = new StackArray<Integer>();

            stack.push(122);
            stack.push(222);
            stack.push(3313);
            stack.push(46);
            stack2.push(122);
            stack2.push(222);
            stack2.push(3313);
            stack2.push(46);
            System.out.println(stack);
            System.out.println(stack2);

            stack.peek();
            if (stack.sameStack(stack2) == true) {
                System.out.println("They are equal");
            }
            else {
                System.out.println("They are NOT equal");
            }

            System.out.println(stack);
            System.out.println(stack2);

            //Calling comparison method
           // stack.sameStack(stack2); 
        }
}