使用堆栈向后打印单个链表的数据

时间:2015-04-08 11:09:11

标签: java

我正在尝试使用堆栈向后打印单个链表的数据但我被卡住了。 Stack类如下:

public class Stack{

    public boolean isEmpty(){};    
    public void push(int n){};    
    public int peek(){};
    public int pop(){};

}

public class node{
    int data;
    node next;  

}

public class list{    
    node first;    
}

1 个答案:

答案 0 :(得分:0)

    Stack<String> stack = new Stack<String>();
    List<String> list = new ArrayList<String>();

    list.add("a");
    list.add("b");
    list.add("c");

    for (String s: list){
        stack.push(s);
    }

    while (!stack.isEmpty()){
        System.out.println(stack.pop());
    }
相关问题