שיעורי בית במחסנית + פתרון תרגיל 9

שיעורי בית: עמוד 130 שאלות 10, 11

עמוד 133 שאלות 14,15,16,17,18

 

פתרון תרגיל 9 מעמוד 130

public boolean equals(Stack<T> st){

     Stack <T> tmp1 = new Stack<T>();

     boolean flag = true;

     while (!this.isEmpty() && !st.isEmpty() && flag){

           if (!this.top().equals(st.top())){

                flag = false;

                break;

           }

           else{

                tmp1.push(st.pop());

                this.pop();

           }

     }

     if (!this.isEmpty() || !st.isEmpty()){

           flag = false;  

     }

     while (!tmp1.isEmpty()){

           this.push(tmp1.top());

           st.push(tmp1.pop());

     }

     return flag;

}