net.datastructures
Class NodeStack<E>

java.lang.Object
  extended by net.datastructures.NodeStack<E>
All Implemented Interfaces:
Stack<E>

public class NodeStack<E>
extends java.lang.Object
implements Stack<E>

Implementation of the stack ADT by means of a singly linked list.

Author:
Natasha Gelfand, Roberto Tamassia
See Also:
Node

Constructor Summary
NodeStack()
          Creates an empty stack.
 
Method Summary
 boolean isEmpty()
          Return whether the stack is empty.
static void main(java.lang.String[] args)
          Test program that performs a series of operations on on a stack and prints the operation performed, the returned element and the content of the stack after each operation.
 E pop()
          Remove the top element from the stack.
 void push(E elem)
          Insert an element at the top of the stack.
 int size()
          Return the number of elements in the stack.
static void status(Stack S, java.lang.String op, java.lang.Object element)
          Prints information about an operation and the stack.
 E top()
          Inspect the element at the top of the stack.
 java.lang.String toString()
          Returns a string representation of the stack as a list of elements, with the top element at the end: [ ...
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NodeStack

public NodeStack()
Creates an empty stack.

Method Detail

size

public int size()
Return the number of elements in the stack.

Specified by:
size in interface Stack<E>
Returns:
number of elements in the stack.

isEmpty

public boolean isEmpty()
Return whether the stack is empty.

Specified by:
isEmpty in interface Stack<E>
Returns:
true if the stack is empty, false otherwise.

push

public void push(E elem)
Insert an element at the top of the stack.

Specified by:
push in interface Stack<E>
Parameters:
element - to be inserted.

top

public E top()
      throws EmptyStackException
Inspect the element at the top of the stack.

Specified by:
top in interface Stack<E>
Returns:
top element in the stack.
Throws:
EmptyStackException - if the stack is empty.

pop

public E pop()
      throws EmptyStackException
Remove the top element from the stack.

Specified by:
pop in interface Stack<E>
Returns:
element removed.
Throws:
EmptyStackException - if the stack is empty.

toString

public java.lang.String toString()
Returns a string representation of the stack as a list of elements, with the top element at the end: [ ... , prev, top ]. This method runs in O(n) time, where n is the size of the stack.

Overrides:
toString in class java.lang.Object
Returns:
textual representation of the stack.

status

public static void status(Stack S,
                          java.lang.String op,
                          java.lang.Object element)
Prints information about an operation and the stack.

Parameters:
op - operation performed
element - element returned by the operation

main

public static void main(java.lang.String[] args)
Test program that performs a series of operations on on a stack and prints the operation performed, the returned element and the content of the stack after each operation.