Midterm Review - INFS 519 Fall 2011
- 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. YourinsertAfter
should create a new node forE val
and insert it afterNode<E> prev
in the list. - Describe how an array-based implementation of a queue differs from a linked-list-based implementation.
- 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. - 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]); } }