Push
by Isai Damier, Android Engineer @ Google

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/***************************************************************************
 * Author: Isai Damier
 * Title: Stack with Linked List
 *     Project: geekviewpoint
 *     Package: datastructure
 *
 * Description: Stack implementation using LinkedList
 *
 * Technical Detail:
 *    Java uses addFirst() instead of addToHead(). Also getFirst
 *    returns the head without removing it; whereas removeFirst
 *    deletes the head.
 *
 *    A single LinkedList is used to implement this
 *    stackWithLinkedList. To implement stackWithLinkedList.push(E)
 *    addFirst(E) is used; for stackWithLinkedList.pop() revomeFirst()
 *    is used; for stackWithLinkedList.peep() getFirst() is used. The
 *    trick is to remove elements from the same end they are added.
 *
 *    E is a generic that may be replaced with any actual object such
 *    as String or Integer or Object.
 **************************************************************************/
 import java.util.LinkedList;
 
public class StackWithLinkedList<E> {
 
  private LinkedList<E> list = new LinkedList<E>();
    
  public void push(E e) {
    list.addFirst(e);
  }
}