Advertisement
porteno

Untitled

Dec 5th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1.  
  2. public class Practice_Questions {
  3.  
  4. public static void main(String[] args) {
  5. Stack<Integer> stk1 = buildStack(1);
  6. Stack<Integer> stk2 = buildStack(2);
  7.  
  8. printStack(stackOfSums(stk1, stk2));
  9. }
  10. /*
  11. * q2
  12. */
  13. public static Stack<Integer> digitApperance(Stack<Integer> s1){
  14.  
  15. }
  16. /*
  17. * q1
  18. */
  19. public static Stack<Integer> stackOfSums(Stack<Integer> s1, Stack<Integer> s2){
  20.  
  21. Stack<Integer> sumStack = new Stack<Integer>();
  22.  
  23. return sumStack;
  24. }
  25. /*
  26. * builds stack for question usage
  27. * input - required stack by order of the questions
  28. */
  29. public static Stack<Integer> buildStack(int stack_num){
  30. Stack<Integer> stk = new Stack<Integer>();
  31. switch(stack_num) {
  32. case 1:
  33.  
  34. stk.push(1);
  35. stk.push(4);
  36. stk.push(4);
  37. return stk;
  38.  
  39. case 2:
  40. stk.push(2);
  41. stk.push(4);
  42. stk.push(3);
  43. return stk;
  44. default:
  45. break;
  46.  
  47. }
  48. return stk;
  49. }
  50. public static int stackSize(Stack<Integer> s) {
  51. int count = 0;
  52. Stack<Integer> tmp = new Stack<Integer>();
  53. while(!s.isEmpty()) {
  54. tmp.push(s.pop());
  55. count++;
  56. }
  57. while(!tmp.isEmpty()) {
  58. s.push(tmp.pop());
  59. }
  60. return count;
  61. }
  62. public static void printStack(Stack<Integer> s) {
  63. Stack<Integer> tmp = new Stack<Integer>();
  64. while(!s.isEmpty()) {
  65. System.out.println("|_"+s.top()+"_|");
  66.  
  67. tmp.push(s.pop());
  68. }
  69. while(!tmp.isEmpty()) {
  70.  
  71. s.push(tmp.pop());
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement