Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package series01;
- public class Stack<T> {
- private Node<T> top;
- public Stack() {
- top=null;
- }
- public boolean isEmpty() {
- return top==null;
- }
- public void push(T e) {
- Node<T> newTop = new Node<T>(e, null);
- if (top == null) {
- top = newTop;}
- else {
- top.next=top;
- top=newTop;
- }
- }
- public T pop() {
- if(isEmpty()) {
- throw new IllegalStateException();
- }
- T current = this.top.value;
- top=top.next;
- return current;
- }
- public T peek() {
- if(isEmpty()) {
- throw new IllegalStateException();
- }
- return top.value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement