Advertisement
porteno

Untitled

Dec 5th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1.  
  2. public class Stack<T> {
  3. private Node<T> first;
  4.  
  5. //creates an empty stack
  6. public Stack() {
  7. this.first = first;
  8. }
  9. //returns true if stack is empty
  10. public boolean isEmpty() {
  11. return this.first == null;
  12. }
  13. //push value into stack - Last In First Out
  14. public void push(T x) {
  15. this.first = new Node<T>(x, this.first);
  16. }
  17. //extract the toped value of stacked - Last In First Out
  18. public T pop() {
  19. T x = this.first.getValue();
  20. this.first = this.first.getNext();
  21. return x;
  22. }
  23. //returns the value in the top without extracting him
  24. public T top() {
  25. return this.first.getValue();
  26. }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement