Java数据结构学习脚印-列表-SLList

 1 public class SLList{
 2     //定义节点类型
 3     public static class IntNode{
 4         public int item;
 5         public IntNode next;
 6         //构造方法,对对象初始化
 7         public IntNode(int i,IntNode n){
 8             item=i;
 9             next=n;
10         }
11     }
12     //定义节点
13     private IntNode first;
14     private int size;
15     //SLList的构造方法
16     public SLList(int x){
17         first=new IntNode(x,null);
18         size=1;
19     }
20     //定义函数:增加第一项
21     public void addFirst(int x){
22         first=new IntNode(x,first);
23         size+=1;
24     }
25     //函数:增加最后一项
26     public void addLast(int x){
27         size+=1;
28         //为了避免空数组造成溢出的情况
29         if(first=null){
30             first=new IntNode(x,null);
31             return;
32         }
33         /*还可以用sentinel node来代替上面的判断
34         *IntNode p=sentinel;
35         */
36         IntNode p=first;
37         //将节点指向最后一项
38         while(p.next!=null){
39             p=p.next;
40         }
41         p.next=new IntNode(x,null);
42     }
43 }

  伯克利CS61B的课程中列表部分的讲解SLList的代码片段,Joshhug老师讲的非常详细,也比较深入,非常值得钻研和学习。希望自己能坚持把这部分内容跟着 学完,做完练习。

原文地址:https://www.cnblogs.com/zyycumt/p/13226607.html