@Test
  public void sizeAfterTwoElementsAdded() {
    stack.push(100);
    stack.push(200);

    assertEquals(2, stack.size());
  }
  public void inorderIT() {
    LinkedStack stack = new LinkedStack();
    BinaryNode current = root;

    if (root == null) throw new IllegalArgumentException("Error tree is empty");
    if (root.left == null && root.right == null) System.out.println(root.data);

    BinaryNode l = current.left;
    BinaryNode r = current.right;

    Node c = new Node(root.data);

    // prob need 2 whiles one for left side one for right
    while (true) {

      if (l != null) {
        Node left = new Node((Integer) l.data);
        stack.push((Integer) left.data);
        l = l.left;
      } else if (r != null) {
        Node right = new Node((Integer) r.data);
        stack.push((Integer) right.data);
        r = r.right;
      }

      if (l == null && r == null) {
        break;
      }
    }

    // test
    System.out.println(stack.pop());
  }
  public T peek() throws EmptyStackException {
    LinkedStack<T> last = getLastStack();
    if (null == last) {
      throw new EmptyStackException();
    }

    return last.peek();
  }
  @Test
  public void pushPopOneElement() {

    stack.push(100);
    assertEquals(1, stack.size());

    assertEquals(100, stack.pop());
    assertEquals(0, stack.size());
  }
Exemple #5
0
 public static void main(String[] args) {
   LinkedStack<String> lss = new LinkedStack<String>();
   for (String s : "Phasers on stun!".split(" ")) {
     lss.push(s);
   }
   String s;
   while ((s = lss.pop()) != null) {
     System.out.println(s);
   }
 }
 public void push(T t) {
   LinkedStack<T> last = getLastStack();
   if (null != last && !last.isFull()) {
     last.push(t);
   } else {
     LinkedStack<T> stack = new LinkedStack<T>(capacity);
     stack.push(t);
     stacks.add(stack);
   }
 }
  @Test
  public void pushPopTwoElements() {

    stack.push(100);
    stack.push(200);
    assertEquals(2, stack.size());

    assertEquals(200, stack.pop());
    assertEquals(1, stack.size());

    assertEquals(100, stack.pop());
  }
  public T pop() throws EmptyStackException {
    LinkedStack<T> last = getLastStack();
    if (null == last) {
      throw new EmptyStackException();
    }

    T t = last.pop();
    if (last.isEmpty()) {
      stacks.remove(stacks.size() - 1);
    }

    return t;
  }
  @Override
  public UnboundedQueueInterface<T> reversed() {
    // TODO 8
    // Hint: Maybe save this one until after you finish enqueue()/dequeue()
    Queue<T> temp = new Queue<T>(this);
    Queue<T> reverse = new Queue<T>();
    LinkedStack<T> stack = new LinkedStack<T>();
    while (!temp.isEmpty()) {
      stack.push(temp.dequeue());
    }
    while (!stack.isEmpty()) reverse.enqueue(stack.pop());

    return reverse;
  }
Exemple #10
0
  public static void main(String[] args) {
    ArrayStack<Integer> aS = new ArrayStack<Integer>();
    LinkedStack<Integer> lS = new LinkedStack<Integer>();

    aS.push(3);
    aS.push(5);
    aS.push(10);
    aS.push(11);
    System.out.println("" + aS.peek() + ", " + aS.pop() + ", " + aS.peek2());
    aS.remove(1);

    lS.push(3);
    lS.push(4);
    lS.push(5);
    lS.push(7);
    lS.push(11);
    lS.push(35);
    System.out.println("" + lS.peek() + ", " + aS.pop() + ", " + aS.peek2());
  }
Exemple #11
0
  public void printDepthFirstTraversal() {
    BSTNode b;
    LinkedStack<BSTNode> s = new LinkedStack<BSTNode>();
    s.push(root);

    while (!s.isEmpty()) {
      b = s.top();
      s.pop();
      System.out.println(b.getInfo());
      if (b.getRight() != null) s.push(b.getRight());
      if (b.getLeft() != null) s.push(b.getLeft());
    }
  }
  @Test
  public void pushPopElementsMultipleTimes() {

    stack.push(300);
    stack.push(400);
    stack.push(500);
    stack.push(600);
    stack.push(700);
    assertEquals(5, stack.size());

    assertEquals(700, stack.pop());
    assertEquals(4, stack.size());

    assertEquals(600, stack.pop());
    assertEquals(3, stack.size());

    assertEquals(500, stack.pop());
    assertEquals(2, stack.size());

    assertEquals(400, stack.pop());
    assertEquals(1, stack.size());

    assertEquals(300, stack.pop());
    assertEquals(0, stack.size());

    stack.push(99);
    assertEquals(1, stack.size());
    assertEquals(99, stack.pop());
    assertEquals(0, stack.size());
  }
Exemple #13
0
  /**
   * The purpose of this program is to create and test two implementations of the Stack Interface.
   * The first implementation is backed by an Array and the class is called ArrayStack. The second
   * implementation is backed by a LinkedList and the class is called LinkedStack. Credit for most
   * of the code in both classes goes to Frank M. Carrano. Mr. Carrano's code was taken from the
   * third edition of the book Data Structures and Abstractions with Java
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    ArrayStack<String> sAStack = new ArrayStack();
    ArrayStack<Integer> iAStack = new ArrayStack();
    LinkedStack<String> sLStack = new LinkedStack();
    LinkedStack<Integer> iLStack = new LinkedStack();

    // Begin ArrayStack testing
    System.out.println("===== Begin ArrayStack Test =====\n");

    // Empty String Stack
    System.out.println("Empty String Stack " + printArrayStackSizeAndCap(sAStack) + "\n");

    // Empty Integer Stack
    System.out.println("Empty Integer Stack " + printArrayStackSizeAndCap(iAStack) + "\n");

    // Add 5 Strings to String Stack
    for (int a = 0; a < 5; a++) {
      sAStack.push(Integer.toString(a));
    }
    System.out.println(
        "After adding 5 Strings String Stack " + printArrayStackSizeAndCap(sAStack) + "\n");

    // Add integers 1 to 4
    for (int a = 1; a < 5; a++) {
      iAStack.push(a);
    }
    System.out.println(
        "After adding Integers 1 to 4 Integer Stack " + printArrayStackSizeAndCap(iAStack) + "\n");

    // Perform peek on Integer Array Stack iStack
    System.out.println("After peek Integer Stack top value is: " + iAStack.peek() + "\n");

    // Perform pop on Integer Array Stack iStack
    System.out.println("After pop Integer Stack top value was: " + iAStack.pop() + "\n");
    System.out.println("After pop Integer Stack " + printArrayStackSizeAndCap(iAStack) + "\n");

    // Check isEmpty on both Stacks
    System.out.println("String Stack is empty: " + sAStack.isEmpty() + "\n");
    System.out.println("Integer Stack is empty: " + iAStack.isEmpty() + "\n");

    // Clear both stacks
    sAStack.clear();
    iAStack.clear();
    System.out.println("After clear String Stack is empty: " + sAStack.isEmpty() + "\n");
    System.out.println("After clear Integer Stack is empty: " + iAStack.isEmpty() + "\n");

    // Add integers 1 to 4
    for (int a = 1; a < 5; a++) {
      iAStack.push(a);
    }
    System.out.println(
        "After adding Integers 1 to 4 Integer Stack " + printArrayStackSizeAndCap(iAStack) + "\n");

    // End ArrayStack testing
    System.out.println("===== End ArrayStack Test =====\n");

    // Begin LinkedStack testing
    System.out.println("===== Begin LinkedStack Test =====\n");

    // Empty String Stack
    System.out.println("Empty String Stack " + printLinkedStackSizeAndCap(sLStack) + "\n");

    // Empty Integer Stack
    System.out.println("Empty Integer Stack " + printLinkedStackSizeAndCap(iLStack) + "\n");

    // Add 5 Strings to String Stack
    for (int a = 0; a < 5; a++) {
      sLStack.push(Integer.toString(a));
    }
    System.out.println(
        "After adding 5 Strings String Stack " + printLinkedStackSizeAndCap(sLStack) + "\n");

    // Add integers 1 to 4
    for (int a = 1; a < 5; a++) {
      iLStack.push(a);
    }
    System.out.println(
        "After adding Integers 1 to 4 Integer Stack " + printLinkedStackSizeAndCap(iLStack) + "\n");

    // Perform peek on Integer Array Stack iStack
    System.out.println("After peek Integer Stack top value is: " + iLStack.peek() + "\n");

    // Perform pop on Integer Array Stack iStack
    System.out.println("After pop Integer Stack top value was: " + iLStack.pop() + "\n");
    System.out.println("After pop Integer Stack " + printLinkedStackSizeAndCap(iLStack) + "\n");

    // Check isEmpty on both Stacks
    System.out.println("String Stack is empty: " + sLStack.isEmpty() + "\n");
    System.out.println("Integer Stack is empty: " + iLStack.isEmpty() + "\n");

    // Clear both stacks
    sLStack.clear();
    iLStack.clear();
    System.out.println("After clear String Stack is empty: " + sLStack.isEmpty() + "\n");
    System.out.println("After clear Integer Stack is empty: " + iLStack.isEmpty() + "\n");

    // Add integers 1 to 4
    for (int a = 1; a < 5; a++) {
      iLStack.push(a);
    }
    System.out.println(
        "After adding Integers 1 to 4 Integer Stack " + printLinkedStackSizeAndCap(iLStack) + "\n");

    // End ArrayStack testing
    System.out.println("===== End LinkedStack Test =====");
  }
Exemple #14
0
 /**
  * Creates a string that contains the current size and current capacity of a Linked Stack
  *
  * @param stack - The Linked Stack being passed
  * @return String containing current size and capacity
  */
 public static String printLinkedStackSizeAndCap(LinkedStack stack) {
   return "Size: " + stack.getCurrentSize();
 }
  @Test
  public void sizeAfterOneElementAdded() {

    stack.push(100);
    assertEquals(1, stack.size());
  }
  @Test
  public void sizeWhenNoElementAdded() {

    assertEquals(0, stack.size());
  }
  @Test
  public void popEmptyStackExceptionAfterMultiplePushPop() {

    stack.push(100);
    stack.push(200);
    stack.push(300);

    assertEquals(3, stack.size());

    assertEquals(300, stack.pop());
    assertEquals(2, stack.size());

    assertEquals(200, stack.pop());
    assertEquals(1, stack.size());

    assertEquals(100, stack.pop());
    assertEquals(0, stack.size());

    stack.push(999);
    assertEquals(1, stack.size());
    assertEquals(999, stack.pop());

    ex.expect(StackEmptyException.class);
    stack.pop();
  }
  @Test
  public void popWithEmptyStack() {

    ex.expect(StackEmptyException.class);
    stack.pop();
  }