Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class LinkList {
- private Node begin;
- public LinkList() {
- begin = null;
- }
- public String peek() {
- Node res = Optional.ofNullable(begin).orElseThrow(() -> new NoSuchElementException());
- return res.data;
- }
- public void push(String param) {
- Node n = new Node();
- n.data = param;
- n.next = begin;
- begin = n;
- }
- public String pop() {
- Node temp = Optional.ofNullable(begin).orElseThrow(() -> new NoSuchElementException());
- begin = begin.next;
- return temp.data;
- }
- public static void main(String[] args) {
- LinkList list = new LinkList();
- list.push("A");
- list.push("B");
- list.push("C");
- System.out.println("Peek : " + list.peek());
- System.out.println("Pop 1: " + list.pop());
- System.out.println("Pop 2: " + list.pop());
- System.out.println("Pop 3: " + list.pop());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement