zoukankan      html  css  js  c++  java
  • 链表从尾到头输出

    将一个链表从尾到头输出。

    使用了栈。

     1 package OnlineTest;
     2 
     3 /*
     4  * 一个链表,从尾到头打印链表每个节点的值。方法:先从头到尾一次入栈,在出栈,九可以实现倒序输出。
     5  * */
     6 
     7 import java.util.Stack;
     8 
     9 class Link{
    10     class Node{
    11         private int data;
    12         private Node next;
    13         public Node(int data){
    14             this.data = data;
    15         }
    16         public void addNode(Node newNode){
    17             if(this.next == null){
    18                 this.next = newNode;
    19             }else{
    20                 this.next.addNode(newNode);
    21             }
    22         }
    23         public void printNode(){
    24             sta.push(this.data);
    25             if(this.next != null){
    26                 this.next.printNode();
    27             }
    28         }
    29     }
    30     Stack sta = new Stack();
    31     Node root;
    32     public void add(int data){
    33         Node newNode = new Node(data);
    34         if(this.root == null){
    35             this.root = newNode;
    36         }else{
    37             this.root.addNode(newNode);
    38         }
    39     }
    40     public void Print(){
    41         this.root.printNode();
    42         while(!(sta.empty())){
    43             System.out.println(sta.pop());
    44         }
    45     }
    46 }
    47 
    48 public class LianBiaoFromTailToHead {
    49 
    50     public static void main(String[] args) {
    51         Link l = new Link();
    52         l.add(0);
    53         l.add(1);
    54         l.add(2);
    55         l.add(3);
    56         l.add(4);
    57         l.add(5);
    58         l.Print();
    59         
    60     }
    61 }
  • 相关阅读:
    Java异常超详细总结
    ArrayList去除集合中字符串的重复值
    scrum项目冲刺_day09
    scrum项目冲刺_day08
    scrum项目冲刺_day07
    scrum项目冲刺_day06
    scrum项目冲刺_day05
    scrum项目冲刺_day04
    scrum项目冲刺_day03
    scrum项目冲刺_day02
  • 原文地址:https://www.cnblogs.com/XuGuobao/p/7418965.html
Copyright © 2011-2022 走看看