Source Code : The GenStack Class

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

The GenStack Class

    


import java.util.LinkedList;

class GenStack<E> {
  private LinkedList<E> list = new LinkedList<E>();

  public void push(E item) {
    list.addFirst(item);
  }

  public E pop() {
    return list.poll();
  }

  public E peek() {
    return list.peek();
  }

  public boolean hasItems() {
    return !list.isEmpty();
  }

  public int size() {
    return list.size();
  }
}

public class GenStackTest {
  public static void main(String[] args) {
    GenStack<String> gs = new GenStack<String>();

    gs.push("One");
    gs.push("Two");
    gs.push("Three");

    gs.push("Four");

    System.out.println("There are " + gs.size() + " items in the stack.
");
    System.out.println("The top item is: " + gs.peek() + "
");
    System.out.println("There are still " + gs.size() + " items in the stack.
");
    System.out.println("Popping everything:");

    while (gs.hasItems())
      System.out.println(gs.pop());
    System.out.println("There are now " + gs.size() + " items in the stack.
");
    System.out.println("The top item is: " + gs.peek() + "
");
  }
}

   
    
    
  

Thank with us