Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class main {
- public static void main(String[] args) {
- Stack<Integer> s = new Stack<>();
- s.push(1);
- s.push(2);
- s.push(3);
- s.push(4);
- System.out.println(s.pop());
- System.out.println(s.pop());
- System.out.println(s.peek());
- System.out.println(s.pop());
- System.out.println(s.isEmpty());
- System.out.println(s.pop());
- System.out.println(s.isEmpty());
- }
- }
- class Stack<T> {
- class Node<T> {
- private T data;
- private Node<T> next;
- public Node(T data) {
- this.data = data;
- }
- }
- private Node<T> top;
- public T pop() {
- if(top == null) {
- }
- T item = top.data;
- top = top.next;
- return item;
- }
- public void push(T item) {
- Node<T> t = new Node<T>(item);
- t.next = top;
- top = t;
- }
- public T peek() {
- if (top == null) {
- }
- return top.data;
- }
- public boolean isEmpty() {
- return top == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement