Advertisement
LoganBlackisle

StackDemo

Jun 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package prep_27_stack;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class StackDemo {
  6.  
  7. public static void main(String[] args) {
  8. StackI s = new NodeStack();
  9. // StackI s = new ArrayStack(5);
  10. // StackI s = new ArrayListStack();
  11. s.push("Tom");
  12. s.push("Diana");
  13. s.push("Harry");
  14. s.push("Thomas");
  15. s.push("Bob");
  16. s.push("Brian");
  17. System.out.println(s.peek());
  18. System.out.println(s.isEmpty() + " " + s.size());
  19. while (!s.isEmpty()) {
  20. System.out.println(s.pop());
  21. }
  22. System.out.println();
  23. System.out.println(s.isEmpty() + " " + s.size());
  24. System.out.println();
  25.  
  26. String str = "([{}])";
  27. if (str.isEmpty()) {
  28. System.out.println("Empty String");
  29. }
  30. System.out.println(checkParantes(str));
  31.  
  32. // -------- test af reverse --------
  33.  
  34. Integer[] tal = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  35. reverse(tal);
  36. for (int i = 0; i < tal.length; i++) {
  37. System.out.print(tal[i] + " ");
  38. }
  39. }
  40.  
  41. public static void reverse(Object[] tabel) {
  42. // StackI stack = new ArrayStack(tabel.length);
  43. // StackI stack = new ArrayListStack();
  44. StackI stack = new NodeStack();
  45. for (int i = 0; i < tabel.length; i++) {
  46. stack.push(tabel[i]);
  47. }
  48. int i = 0;
  49. while (!stack.isEmpty()) {
  50. tabel[i] = stack.pop();
  51. i++;
  52. }
  53. }
  54.  
  55. public static boolean checkParantes(String s) {
  56. ArrayListStack stack = new ArrayListStack();
  57.  
  58. for (int i = 0; i < s.length(); i++) {
  59. char ch = s.charAt(i);
  60. if (ch == '[' || ch == '(' || ch == '{') {
  61. stack.push(ch);
  62. } else if ((ch == ']' || ch == '}' || ch == ')') && (!stack.isEmpty())) {
  63. if (((char) stack.peek() == '(' && ch == ')') || ((char) stack.peek() == '{' && ch == '}')
  64. || ((char) stack.peek() == '[' && ch == ']')) {
  65. stack.pop();
  66. } else {
  67. return false;
  68. }
  69. } else {
  70. if ((ch == ']' || ch == '}' || ch == ')')) {
  71. return false;
  72. }
  73. }
  74. }
  75. if (stack.isEmpty()) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement