Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prep_27_stack;
- import java.util.Scanner;
- public class StackDemo {
- public static void main(String[] args) {
- StackI s = new NodeStack();
- // StackI s = new ArrayStack(5);
- // StackI s = new ArrayListStack();
- s.push("Tom");
- s.push("Diana");
- s.push("Harry");
- s.push("Thomas");
- s.push("Bob");
- s.push("Brian");
- System.out.println(s.peek());
- System.out.println(s.isEmpty() + " " + s.size());
- while (!s.isEmpty()) {
- System.out.println(s.pop());
- }
- System.out.println();
- System.out.println(s.isEmpty() + " " + s.size());
- System.out.println();
- String str = "([{}])";
- if (str.isEmpty()) {
- System.out.println("Empty String");
- }
- System.out.println(checkParantes(str));
- // -------- test af reverse --------
- Integer[] tal = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- reverse(tal);
- for (int i = 0; i < tal.length; i++) {
- System.out.print(tal[i] + " ");
- }
- }
- public static void reverse(Object[] tabel) {
- // StackI stack = new ArrayStack(tabel.length);
- // StackI stack = new ArrayListStack();
- StackI stack = new NodeStack();
- for (int i = 0; i < tabel.length; i++) {
- stack.push(tabel[i]);
- }
- int i = 0;
- while (!stack.isEmpty()) {
- tabel[i] = stack.pop();
- i++;
- }
- }
- public static boolean checkParantes(String s) {
- ArrayListStack stack = new ArrayListStack();
- for (int i = 0; i < s.length(); i++) {
- char ch = s.charAt(i);
- if (ch == '[' || ch == '(' || ch == '{') {
- stack.push(ch);
- } else if ((ch == ']' || ch == '}' || ch == ')') && (!stack.isEmpty())) {
- if (((char) stack.peek() == '(' && ch == ')') || ((char) stack.peek() == '{' && ch == '}')
- || ((char) stack.peek() == '[' && ch == ']')) {
- stack.pop();
- } else {
- return false;
- }
- } else {
- if ((ch == ']' || ch == '}' || ch == ')')) {
- return false;
- }
- }
- }
- if (stack.isEmpty()) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement