import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
interface Stack<T> {
void push(T ele);
T pop();
}
class StackLinkedlist<T> implements Stack<T> {
private Node head = null;
private class Node{
private T data;
private Node next;
public Node (T data){
this.data = data;
this.next = null;
}
}
public void push(T val){
Node node = new Node(val);
node.next = head;
head = node;
}
public T pop() {
if (head == null) {
return null;
}
T value = head.data;
head = head.next;
return value;
}
}
class Solution {
public static void main(String[] args) {
Stack<Integer> test = new StackLinkedlist<Integer>();
test.push(1);
test.push(2);
test.push(3);
System.out.println(test.pop());
System.out.println(test.pop());
System.out.println(test.pop());
System.out.println(test.pop());
}
}
Implement Stack Using LinkedList
最新推荐文章于 2022-01-24 19:41:14 发布