Example usage for java.util Stack push

List of usage examples for java.util Stack push

Introduction

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

Prototype

public E push(E item) 

Source Link

Document

Pushes an item onto the top of this stack.

Usage

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 w w .  j a  va2 s  . co  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");
    s.push("B");/*from   www.  j  av a 2 s. c o m*/
    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 st = new Stack();

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

    System.out.println("Is stack empty: " + st.empty());
}

From source file:Main.java

public static void main(String args[]) {
    Stack<String> s = new Stack<String>();
    s.push("A");
    s.push("B");//  w  w w  .ja v  a2s .com
    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: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: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);//  www. j a  va2  s.  c o m
    System.out.println("stack: " + st);

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

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);/*w ww. j  a v a 2 s  .  com*/
    System.out.println("stack: " + st);

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

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]));
    }/*from w  w  w.ja  v  a2  s  .  co  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) {
    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  ava  2 s.c om*/
    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:StringReverse.java

public static void main(String[] argv) {
    //+/*w  w  w .  j  av a2s .c o m*/
    String s = "Father Charles Goes Down And Ends Battle";

    // Put it in the stack frontwards
    Stack myStack = new Stack();
    StringTokenizer st = new StringTokenizer(s);
    while (st.hasMoreTokens())
        myStack.push(st.nextElement());

    // Print the stack backwards
    System.out.print('"' + s + '"' + " backwards by word is:\n\t\"");
    while (!myStack.empty()) {
        System.out.print(myStack.pop());
        System.out.print(' ');
    }
    System.out.println('"');
    //-
}