Midterm Review - INFS 519 Fall 2011

  1. In pseudocode, implement a function, public void (Node<E> prev, E val), which could be found in a List class that used the Node<E> class. Your insertAfter should create a new node for E val and insert it after Node<E> prev in the list.
  2. Describe how an array-based implementation of a queue differs from a linked-list-based implementation.
  3. A stack class, Stack, uses a linked list in its implementation. Implement both of the following methods:
    • public void push(E item)
    • public E pop()

    The pop should return null if no item is available. Remember to handle all cases.

  4. Show the printed output of the call foo(0) based on the definition provided in the following source code.
class Node<E> {
    private Node<E> link;
    private E data;

    public Node(E data) {
        this.data = data;
    }

    public Node<E> getLink() {
        return link;
    }

    public void setLink(Node<E> newLink) {
        this.link = newLink;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

    // Other implementation details would follow below...
}
class Stack<E> {
    private Node<E> top;

    public Stack() {
        top = null;
    }

    // Other implementation details would follow below...
}
int[] data = new int[] {2, 4, 6, 8, 10};

void foo(int i) {
    if (i == data.length) {
        return;
    }
    else if (i >= 0) {
        foo(i + 1);
        System.out.println(data[i]);
    }
}
Validate XHTML 1.0