Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class LinkList {
  4. private Node begin;
  5.  
  6. public LinkList() {
  7. begin = null;
  8. }
  9.  
  10. public String peek() {
  11. Node res = Optional.ofNullable(begin).orElseThrow(() -> new NoSuchElementException());
  12. return res.data;
  13. }
  14.  
  15. public void push(String param) {
  16. Node n = new Node();
  17. n.data = param;
  18. n.next = begin;
  19. begin = n;
  20. }
  21.  
  22. public String pop() {
  23. Node temp = Optional.ofNullable(begin).orElseThrow(() -> new NoSuchElementException());
  24. begin = begin.next;
  25. return temp.data;
  26. }
  27.  
  28. public static void main(String[] args) {
  29. LinkList list = new LinkList();
  30. list.push("A");
  31. list.push("B");
  32. list.push("C");
  33.  
  34. System.out.println("Peek : " + list.peek());
  35. System.out.println("Pop 1: " + list.pop());
  36. System.out.println("Pop 2: " + list.pop());
  37. System.out.println("Pop 3: " + list.pop());
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement