JavaAlgorithms
Elementary and no so elementary Java algorithms
listAlgorithms/Stack.java
Go to the documentation of this file.
00001 
00009 package listAlgorithms;
00010 
00022 public class Stack<T extends Comparable<T>> {
00023     private ListNode<T> mTOS = null;
00024 
00030     public void push(T val) {
00031         ListNode<T> newElem = new ListNode<T>(val);
00032         newElem.setNext(mTOS);
00033         mTOS = newElem;
00034     }
00035 
00039     protected void popTop() {
00040         if (mTOS != null) {
00041             ListNode<T> top = mTOS;
00042             mTOS = top.getNext();
00043             top.setNext(null);
00044         }
00045     }
00046 
00050     public T peek() {
00051         T tosVal = null;
00052         if (mTOS != null) {
00053             tosVal = mTOS.getValue();
00054         }
00055         return tosVal;
00056     }
00057 
00061     public T pop() {
00062         T tosVal = peek();
00063         popTop();
00064         return tosVal;
00065     }
00066 
00067     public void printStack() {
00068         if (mTOS != null) {
00069             mTOS.printList();
00070         }
00071     }
00072 }
 All Classes Namespaces Files Functions Variables