zoukankan      html  css  js  c++  java
  • 《Java数据结构与算法》笔记-CH5-链表-3双端链表

      1 /**
      2  * 双端链表的实现
      3  */
      4 class LinkA {
      5     public long dData;
      6     public LinkA next;
      7 
      8     public LinkA(long d) {
      9         dData = d;
     10     }
     11 
     12     public String toString() {
     13         return "[" + dData + "]";
     14     }
     15 
     16     public void displayLink() {
     17         System.out.println(toString());
     18     }
     19 }
     20 
     21 class FirstLastList {
     22     private LinkA first;// 头部
     23     private LinkA last;// 尾部
     24 
     25     public FirstLastList() {
     26         first = null;
     27         last = null;
     28     }
     29 
     30     public boolean isEmpty() {
     31         return first == null;
     32     }
     33 
     34     /**
     35      * 在头部插入节点
     36      */
     37     public void insertFirst(LinkA l) {
     38         if (isEmpty())
     39             last = l;
     40         l.next = first;
     41         first = l;
     42     }
     43 
     44     /**
     45      * 在尾部插入节点
     46      * 
     47      * @param l
     48      */
     49     public void insertLast(LinkA l) {
     50         if (isEmpty())
     51             first = l;
     52         else
     53             last.next = l;
     54         last = l;
     55     }
     56 
     57     /**
     58      * 从头部删除一个节点
     59      * 
     60      * @return
     61      */
     62     public long deleteFirst() {
     63         long temp = first.dData;
     64         if (first.next == null)
     65             last = null;
     66         first = first.next;
     67         return temp;
     68     }
     69 
     70     public String toString() {
     71         if (isEmpty())
     72             return "{}";
     73         LinkA current = first;
     74         StringBuilder sb = new StringBuilder();
     75         sb.append("{");
     76         while (current != null) {
     77             sb.append(current.toString()).append(",");
     78             if (current.next == null)
     79                 break;
     80             else
     81                 current = current.next;
     82         }
     83         sb.deleteCharAt(sb.length() - 1);
     84         sb.append("}");
     85         return sb.toString();
     86     }
     87 
     88     public void displayList() {
     89         System.out.println(toString());
     90     }
     91 }
     92 
     93 public class FirstLastListDemo {
     94     public static void main(String[] args) {
     95         FirstLastList fll = new FirstLastList();
     96         for (int i = 1; i < 10; i++) {
     97             System.out.println("插入:"+i);
     98             if (i % 2 == 0)//i为偶数调用insertFirst
     99                 fll.insertFirst(new LinkA(i));
    100             else//i为基数调用insertLast
    101                 fll.insertLast(new LinkA(i));
    102             fll.displayList();
    103         }
    104         System.out.println("插入完毕开始从头部删除");
    105         while(!fll.isEmpty()){
    106             fll.deleteFirst();
    107             fll.displayList();
    108         }
    109     }
    110 }
  • 相关阅读:
    界面布美观布局
    登陆界面验证码设置
    很好的JAVESRIPT控件
    c#导入EXCEL数据
    微软:系列课程 >Silverlight for Windows Phone 开发系列课程
    JavaScript动态网页制作宝库
    Silverlight经典教程书籍汇总
    SQL删除表中有重复的记录
    Javascript鼠标事件
    Android系统架构(转)
  • 原文地址:https://www.cnblogs.com/fstack/p/5617253.html
Copyright © 2011-2022 走看看