Write a java method areEqual that returns true if the two st
Write a java method areEqual that returns true if the two stacks specified in the parameters have the same elements in the same order, and false otherwise. Two elements are the same if they refer to the same object. The method may remove elements from the stacks, but it must return the elements to the stacks in the same order to restore the stacks to their original state. The only additional data structure that it can use as auxiliary storage is a single stack
Solution
// definition of Student class
class Student{
int roll; // roll is a instace variable for every stack.
Student(int roll){
this.roll=roll;
}
boolean equals(Student other){ // comparing two objects.
if(this.roll == other.roll){
return true;
}
return false;
}
}
// MyStack class definition
public class MyStack { // basic structure of stack
private int maxSize;
private Student[] stackArray;
private int top;
public MyStack(int s) { // creating stack
maxSize = s;
stackArray = new Student[maxSize];
top = -1;
}
public void push(Student j) { // pushing elements
stackArray[++top] = j;
}
public Student pop() { // poping
return stackArray[top--];
}
public Student peek() {
return stackArray[top];
}
public boolean isEmpty() { // checking method is stack is empty
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack stackOne = new MyStack(10); // creating stack one
// creating objects for pushing
Student one = new Student(11);
Student two = new Student(12);
Student three = new Student(13);
Student four = new Student(14);
Student five = new Student(15);
// pushing elements into stack one.
stackOne.push(one);
stackOne.push(two);
stackOne.push(three);
stackOne.push(four);
stackOne.push(five);
// creating second second
MyStack stackTwo = new MyStack(10);
// pushing same elements
stackTwo.push(one);
stackTwo.push(two);
stackTwo.push(three);
stackTwo.push(four);
stackTwo.push(five);
//calling compareStacks function
boolean flag = compareStacks(stackOne,stackTwo);
// printing result
if(flag){
System.out.println(\"Both Stacks are same\");
}
else{
System.out.println(\"Both Stacks are different\");
}
// changing last element of second stack
stackTwo.pop();
Student six = new Student(20);
stackTwo.push(six);
// comparing again
flag = compareStacks(stackOne,stackTwo);
// printing result
if(flag){
System.out.println(\"Both Stacks are same\");
}
else{
System.out.println(\"Both Stacks are different\");
}
}
// definition of comparingStacks
public static boolean compareStacks(MyStack firstStack, MyStack secondStack){
// if one of the stack is empty return false
if(firstStack.isEmpty() != secondStack.isEmpty()){
return false;
}
// if both are empty return true;
if(firstStack.isEmpty() && secondStack.isEmpty()){
return true;
}
// pop up the elements from two stacks
Student element_firstStack = firstStack.pop();
Student element_secondStack = secondStack.pop();
// compare them
if(!element_firstStack.equals(element_secondStack)){
firstStack.push(element_firstStack);
secondStack.push(element_secondStack);
return false; // if not equal push them back and return false
}
// if equal compare remaining elements in stacks with recursive call.
boolean result = compareStacks(firstStack,secondStack);
firstStack.push(element_firstStack); // push them back
secondStack.push(element_secondStack);
return result;// return result
}
}
// end of program

