Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Adt.Stack;
- public class Q {
- public static void main(String[] args) {
- Stack s = new Stack<Integer>();
- Stack <TwoItems> c = new Stack<TwoItems>();
- s.push(9);
- s.push(4);
- s.push(7);
- s.push(5);
- s.push(5);
- s.push(32);
- s.push(6);
- s.push(1);
- System.out.println(lastAndRemove(s));
- }
- public static int size(Stack<Integer> s) {
- Stack<Integer> buff = new Stack<Integer>();
- int c = 0;
- while(!s.isEmpty()) {
- c ++;
- buff.push(s.pop());
- }
- while(!buff.isEmpty()) s.push(buff.pop());
- return c;
- }
- public static int lastAndRemove(Stack<Integer>st)
- {
- int last=0;
- // In my test here was only ( int last; ) instead of ( int last=0; )so I lost 2 points here. The other part of method is working correctly
- Stack<Integer>tmp=new Stack<Integer>();
- while(!st.isEmpty())
- {
- last=st.pop();
- if(!st.isEmpty())
- tmp.push(last);
- }
- while(!tmp.isEmpty())
- st.push(tmp.pop());
- return last;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement