62,614
社区成员
发帖
与我相关
我的任务
分享
public class LinkedList<T> {
private Node<T> head;
public LinkedList(){
head=null;
}
public void insertNode(T value){
if(head==null){
head.data=value;
head.next=null;
}
Node<T> node=new Node<T>(value);
node.next=head;
head=node;
}
public T remove(){
Node<T> temp=head;
head=head.next;
return temp.data;
}
public boolean isEmpty(){
return (head==null);
}
class Node<T>{
private T data;
private Node<T> next;
public Node(){
data=null;
next=null;
}
public Node(T value){
data=value;
next=null;
}
public void changeData(T value){
data=value;
}
}
}
public class Stack_LinkedList<T> {
private LinkedList<T> stack;
public Stack_LinkedList(){
stack=new LinkedList<T>();
}
public void push(T value){
stack.insertNode(value);
}
public T pop(){
return stack.remove();
}
public boolean isEmpty(){
return stack.isEmpty();
}
}
class test7{
public static void main(String[] args){
Stack_LinkedList<Integer> stack=new Stack_LinkedList<Integer>();
for(int i=0;i<100;i++){
stack.push(i);
}
System.out.println();
while (!stack.isEmpty()){
System.out.print(stack.pop()+" ");
}
System.out.println();
}
}