Example usage for java.util Stack Stack

List of usage examples for java.util Stack Stack

Introduction

In this page you can find the example usage for java.util Stack Stack.

Prototype

public Stack() 

Source Link

Document

Creates an empty Stack.

Usage

From source file:Main.java

public static void main(String args[]) {

    Stack st = new Stack();

    st.push("Java");
    st.push("Source");
    st.push("from java2s.com");

    // removing top object
    System.out.println("Removed object is: " + st.pop());

    // elements after remove
    System.out.println("Elements after remove: " + st);
}

From source file:StackExample.java

public static void main(String args[]) {
    Stack s = new Stack();
    s.push("Java");
    s.push("Source");
    s.push("and");

    System.out.println("Next: " + s.peek());
    s.push("Support");
    System.out.println(s.pop());/*from   w ww.jav a 2s . c o m*/
    s.push(".");
    int count = s.search("Java");
    while (count != -1 && count > 1) {
        s.pop();
        count--;
    }
    System.out.println(s.pop());
    System.out.println(s.empty());
}

From source file:MainClass.java

public static void main(String args[]) {
    Stack s = new Stack();
    s.push("A");/*from  www .j  a v  a  2 s . c om*/
    s.push("B");
    s.push("C");

    System.out.println("Next: " + s.peek());
    s.push("D");

    System.out.println(s.pop());
    s.push("E");
    s.push("F");

    int count = s.search("E");
    while (count != -1 && count > 1) {
        s.pop();
        count--;
    }
    System.out.println(s);
}

From source file:Main.java

public static void main(String args[]) {
    Stack<String> s = new Stack<String>();
    s.push("A");//from   www  .j a  va2  s  .  com
    s.push("B");
    s.push("C");

    System.out.println("Next: " + s.peek());
    s.push("D");

    System.out.println(s.pop());
    s.push("E");
    s.push("F");

    int count = s.search("E");
    while (count != -1 && count > 1) {
        s.pop();
        count--;
    }
    System.out.println(s);
}

From source file:Main.java

public static void main(String[] args) {
    String input = "test";
    Stack<Character> stack = new Stack<Character>();
    for (int i = 0; i < input.length(); i++) {
        stack.push(input.charAt(i));/*from  w ww . j  a v a 2  s .co m*/
    }
    String reverseInput = "";
    while (!stack.isEmpty()) {
        reverseInput += stack.pop();
    }
    if (input.equals(reverseInput)) {
        System.out.println("Yo! that is a palindrome.");
    } else {
        System.out.println("No! that isn't a palindrome.");
    }
}

From source file:Main.java

public static void main(String[] args) {
    String input = "This is a sentence";
    char[] charinput = input.toCharArray();
    Stack<String> stack = new Stack<String>();
    for (int i = input.length() - 1; i >= 0; i--) {
        stack.push(String.valueOf(charinput[i]));
    }//  w ww  .  j a v a2 s  . c o  m
    StringBuilder StackPush = new StringBuilder();
    for (int i = 0; i < stack.size(); i++) {
        StackPush.append(stack.get(i));
    }
    System.out.println(StackPush.toString());
}

From source file:Main.java

public static void main(String args[]) {
    Stack st = new Stack();
    System.out.println("stack: " + st);
    st.push(new Integer(42));
    System.out.println("push(" + 42 + ")");
    System.out.println("stack: " + st);

    System.out.print("pop -> ");
    Integer a = (Integer) st.pop();
    System.out.println(a);/*from   w  ww  .  j av  a  2s  .com*/
    System.out.println("stack: " + st);

    try {
        st.pop();
    } catch (EmptyStackException e) {
        e.printStackTrace();
        System.out.println("empty stack");
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Stack st = new Stack();
    System.out.println("stack: " + st);
    st.push(new Integer(42));
    System.out.println("push(" + 42 + ")");
    System.out.println("stack: " + st);

    System.out.print("pop -> ");
    Integer a = (Integer) st.pop();
    System.out.println(a);/*from   w  w  w.  jav a 2s.c o m*/
    System.out.println("stack: " + st);

    try {
        st.pop();
    } catch (EmptyStackException e) {
        System.out.println("empty stack");
    }
}

From source file:Stacks.java

public static void main(String[] args) {
    Stack stack = new Stack();
    for (int i = 0; i < 10; i++)
        stack.push(new Integer(i));
    System.out.println("stack = " + stack);
    // Treating a stack as a Vector:
    stack.addElement("The last line");
    System.out.println("element 5 = " + stack.elementAt(5));
    System.out.println("popping elements:");
    while (!stack.empty())
        System.out.println(stack.pop());
}

From source file:ExceptionalTest.java

public static void main(String[] args) {
    int i = 0;//w  w  w.  ja  v  a  2 s  .  com
    int ntry = 10000000;
    Stack<String> s = new Stack<String>();
    long s1;
    long s2;

    // test a stack for emptiness ntry times
    System.out.println("Testing for empty stack");
    s1 = new Date().getTime();
    for (i = 0; i <= ntry; i++)
        if (!s.empty())
            s.pop();
    s2 = new Date().getTime();
    System.out.println((s2 - s1) + " milliseconds");

    // pop an empty stack ntry times and catch the resulting exception
    System.out.println("Catching EmptyStackException");
    s1 = new Date().getTime();
    for (i = 0; i <= ntry; i++) {
        try {
            s.pop();
        } catch (EmptyStackException e) {
        }
    }
    s2 = new Date().getTime();
    System.out.println((s2 - s1) + " milliseconds");
}