Advertisement
Guest User

Untitled

a guest
May 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. package series01;
  2.  
  3. public class Stack<T> {
  4.  
  5. private Node<T> top;
  6.  
  7.  
  8. public Stack() {
  9. top=null;
  10. }
  11.  
  12. public boolean isEmpty() {
  13. return top==null;
  14. }
  15.  
  16.  
  17.  
  18. public void push(T e) {
  19. Node<T> newTop = new Node<T>(e, null);
  20. if (top == null) {
  21. top = newTop;}
  22. else {
  23. top.next=top;
  24. top=newTop;
  25. }
  26.  
  27. }
  28.  
  29. public T pop() {
  30.  
  31. if(isEmpty()) {
  32. throw new IllegalStateException();
  33. }
  34.  
  35. T current = this.top.value;
  36. top=top.next;
  37.  
  38. return current;
  39.  
  40. }
  41.  
  42. public T peek() {
  43.  
  44. if(isEmpty()) {
  45. throw new IllegalStateException();
  46. }
  47.  
  48. return top.value;
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement