| 123456789101112131415161718192021222324252627282930313233 |
- public class LinkedStack<E> {
- Node top;
- class Node {
- E element;
- Node next;
- }
- public void push(E element) {
- Node newNode = new Node();
- newNode.element = element;
- newNode.next = top;
- top = newNode;
- }
- public E pop() {
- if (top == null) {
- return null;
- }
- E data = top.element;
- top = top.next;
- return data;
- }
- public E peek() {
- if (top == null) {
- return null;
- }
- return top.element;
- }
- }
|