Advertisement
porteno

Untitled

Jan 22nd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import Adt.Stack;
  2.  
  3. public class Q {
  4.  
  5. public static void main(String[] args) {
  6. Stack s = new Stack<Integer>();
  7. Stack <TwoItems> c = new Stack<TwoItems>();
  8. s.push(9);
  9. s.push(4);
  10. s.push(7);
  11. s.push(5);
  12. s.push(5);
  13. s.push(32);
  14. s.push(6);
  15. s.push(1);
  16. System.out.println(lastAndRemove(s));
  17.  
  18. }
  19. public static int size(Stack<Integer> s) {
  20. Stack<Integer> buff = new Stack<Integer>();
  21. int c = 0;
  22. while(!s.isEmpty()) {
  23. c ++;
  24. buff.push(s.pop());
  25. }
  26. while(!buff.isEmpty()) s.push(buff.pop());
  27. return c;
  28. }
  29. public static int lastAndRemove(Stack<Integer>st)
  30. {
  31. int last=0;
  32. // 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
  33. Stack<Integer>tmp=new Stack<Integer>();
  34. while(!st.isEmpty())
  35. {
  36. last=st.pop();
  37. if(!st.isEmpty())
  38. tmp.push(last);
  39. }
  40. while(!tmp.isEmpty())
  41. st.push(tmp.pop());
  42. return last;
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement