You are on page 1of 6

Diagram for 5.

24: below:

For question 5.25:


public interface TrueStack<E> extends Collection<E>{
public boolean isEmpty();
public E peek();
public E pop();
public void push(E element);
public int size();
}
public class LinkedStack<E> extends AbstractCollection<E> implements
TrueStack<E>{
private Node<E> items;
private int size;

public LinkedStack(){

this.items = null;

this.size = 0;

}
public void push(E element){

this.items = new Node<E>(element, items);

this.size += 1;

}
public E pop(){

E element = this.items.data;

this.items = this.items.next;

this.size -= 1;

return element;

}
public E peek(){

return this.items.data;

}
public boolean isEmpty(){

return this.size() == 0;

}
public int size(){

return this.size;

}
public Iterator<E> iterator(){

return null;

private class Node<E>{

private E data;

private Node next;

private Node(E data, Node next){

this.data = data
this.next = next;

public class ArrayStack <E> extends AbstractCollection <E> implements


TrueStack<E>
{
private E container[];
private int top;
private final static int DEFAULT_SIZE = 10;

public ArrayStack ()
{
this(DEFAULT_SIZE);
}

public ArrayStack (int initSize)


{
container = (E[]) new Object [initSize];
top = -1;
}

public E getTop()
{
if (top == -1)
return null;
return container[top];
}

public boolean isEmpty()


{
return (top == -1);
}

public E pop()
{
if (top == -1)
return null;
return container[top--];
}

public void push(E itm)

{
container[++top] = itm;
}

public int size()


{
return (top + 1);
}
}

You might also like