题目:定义一个链表的头结点,从尾到头反过来打印出每个结点的值
import java.util.Scanner;
import java.util.Stack;
class NodeList{
int value;
NodeList next;
public NodeList(int value){
this.value=value;
this.next=null;
}
}
public class exercise3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入5个值构造一个链表:");
Scanner s=new Scanner(System.in);
NodeList nl=new NodeList(s.nextInt());
NodeList nltemp =nl;
for(int i=1;i<5;i++){
NodeList nlnext=new NodeList(s.nextInt());
nltemp.next=nlnext;
nltemp=nlnext;
}
System.out.println("打印链表的值");
NodeList nltemp2=nl;
while(nltemp2!=null)
{ System.out.println(nltemp2.value);
nltemp2=nltemp2.next;
}
System.out.println("反续输出:");
NodeList nltemp3=nl;
Stack stack=new Stack();
while(nltemp3!=null)
{ stack.push(nltemp3.value);
nltemp3=nltemp3.next;
}
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}