链表(IntList,SLList…)

  1. If the main method in one java file not exists or incomplete, idea have no optons for run the program.
  2. We need two classes, one is the naked recursion IntNode, the other is SLList class to cover the naked one, making it more convenient and clear for users to use it.
  3. SLList is for interactive with users. The key is it construct an IntNode object using the IntNode class, like clothes wrap the whole naked recursion structure.
    : In the past, we have to construct Intlist:
IntNode first = new IntNode(5,null);

SLList:

SLList first = new SLList(5);

Below is the IntNode class

public class IntNode {public int item;public IntNode next;public IntNode(int f,IntNode r){first = f;rest = r;}
}
Here is the SLList class:
public class SLList {private IntNode first;/*prevent other class directly access the naked data sturcture, we nee private to prevent.ould be only accessed within the same class.*/public SLList(int x){first = new IntNode(x,null);}public static void main(){SLList L = new SLList(1);}
}

Methods:

  1. addFirst
 public void addFirst(int x){first = new IntNode(x,first);}
  1. getFirst
public int getFirst(){return first.item}
  1. size:
    When I use first as an IntNode variable to run the method, I find this method is destructive, which means the original list will be changed.
    The method shown below:
public int size(){int size = 1;for(int n = 0; n >= 0;n++){if(first.next! = null){size++;first=first.next;}else{return size;}}return size;}

Instead, we can refer an IntNode p as an variable to escape the destruction. this is called non-destructive

public int size(IntNode p){if(p.next == null){return 1;}else{return size(p.next)+1;}}
public int Size(){return size(first)

addLast:

  1. p=p.next 不会改变first,但是p=new会改变first,前者只是address一样,所以只是p=p.next都是first内部的,所以不会改变p,为什么p.next=new IntNode(x,null) 会改变first呢:
IntNode L1 =new IntNode(2,null);
L1 = new IntNode(4,L1);
IntNode L2 = L1;
L2 = L1.next;/*只改指针,不改内容,因为相当于reference type赋值,只是更改了address*/
L2.item = 3; /*会改变L1与L2,因为是reference type, reference type会改变二者,primitive type不会*/
L2.next = new IntNode(5,null)/*会改变L1和L2,因为是给L2.next创建了一个address并指向里面*/

size improvement
Obviously, the size() method is unefficient, so we will add a integer variable to track the size of the list.

private int size;public SLList (int x) {size = 1;...
}public void addFirst(int x) {size += 1;...
}public void addLast (int x) {size += 1;...
}public int size() {return size;
}

parameter passing: copy the bits of those variables into the parameters variables.
golden rule of equals (GroE) : when we assign a value with equals, we are just copying the bits from one memory box to another!

Nested class

public class SLList {/*here is the nested class*/private static class IntNode {public int item;public IntNode next;public IntNode(int i, IntNode n) {item = i;next = n;}}private IntNode first; public SLList(int x) {first = new IntNode(x, null);}
...

nested class is for clear and organized. has no practical meannings.
Besides, nested class should be static, so that it can not access the variables and instance in SLList class.

因为empty list没有.next,所以如果以empty list开头,很多方法就会变得很复杂,so we need a object called sentinal node to tackle the issue.

  1. change first in sentinel
/** The first item, if it exists, is at sentinel.next. */
private IntNode sentinel;public SLList() {sentinel = new IntNode(63, null);size = 0;
}public SLList(int x) {sentinel = new IntNode(63, null);sentinel.next = new IntNode(x, null);size = 1;
}public void addFirst(int x) {sentinel.next = new IntNode(x, sentinel.next);size = size + 1;
}public int getFirst() {return sentinel.item;
}public void addLast(inx x) {IntNode p = sentinel;while (p.next != null) {p = p.next;}p.next = new IntNode(x, null);size = size + 1;
}

A SLList with a sentinel node has at least the following invariants:

The sentinel reference always points to a sentinel node. (the first node must be sentinel node)
The front item (if it exists), is always at sentinel.next.item.(first = sentinel.next)
The size variable is always the total number of items that have been added.

Generic List
We could improve our IntList to make it available for other types.

public class DLList<BleepBlorp> {private IntNode sentinel;private int size;public class IntNode {public IntNode prev;public BleepBlorp item;public IntNode next;...}...
}

We put the desired type inside of angle brackets during declaration, and also use empty angle brackets during instantiation.

DLList d2 = new DLList<>(“hello”);
d2.addLast(“world”);

CS61B :链表(IntList,SLList....)相关推荐

  1. 原创:2016.4.25-2016.5.1 C# informal essay and tittle_tattle

    1.Some  tips of the Time in C sharp (1) How to define the category of the "Datetime"? 1 da ...

  2. vxworks pci驱动

    PCI配置空间 当VxWorks内核起来之后,PCI设备的第一次使用都是必须在调用sysHwInit2()例程之后.但是由于MMU内存映射的初始化和激活是在例程sysHwInit()和sysHwIni ...

  3. 《Go语言圣经》第六章

    6.1.方法声明 函数声明时,在函数名字前面加一个参数,就是这个参数对应类型的方法. geometry.go type Point struct {X, Y float64 }// Distance ...

  4. PCI总线在VxWorks中的实现

    8D Spaces Reliability & Stability & Efficiency 目录视图 摘要视图 订阅 PCI总线在VxWorks中的实现 2013-01-22 23: ...

  5. CS61B sp2018笔记 | Lists

    Lists 1. IntLists   下面我们来一步一步的实现List类,首先你可以实现一个最简单的版本: public class IntList {public int first;public ...

  6. 带哨兵节点的链_关于链表中哨兵结点问题的深入剖析

    算法 数据结构 关于链表中哨兵结点问题的深入剖析 最近正在学习UC Berkeley的CS61B这门课,主要是采用Java语言去实现一些数据结构以及运用数据结构去做一些project.这门课不仅告诉你 ...

  7. c++标准库中,含有链表的类list

    Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.STL中 end()指向的总是无效值,取值都用迭代器,用法跟指针差不多.assig ...

  8. 用模板写单链表 尹成

    转载:http://blog.csdn.net/itcastcpp/article/details/39081953 为了加深对模板的理解,我们今天一起用模板写一个单链表,希望通过这个例子,能够帮助大 ...

  9. 数据结构--链表--单链表中环的检测,环的入口,环的长度的计算

    就如数字6一样的单链表结构,如何检测是否有6下部的○呢,并且求交叉点位置 思路 使用快慢指针(一个一次走2步,一个走1步),若快慢指针第一次相遇,则有环 慢指针路程 s=a+bs = a+bs=a+b ...

最新文章

  1. Android开发之dp转像素,像素转换为dp工具类,详细代码,带有源文件下载地址。...
  2. volley imagerequest
  3. java mvc引擎_SpringMvc+JavaConfig+Idea 搭建项目
  4. mysql 参数化 c_MySQL(16):参数化、封装
  5. 【MyBatis框架】SqlMapConfig剖析
  6. Centos为什么比不过Ubuntu和Debian?
  7. matlab2c使用c++实现matlab函数系列教程-unifstat函数
  8. Xml读取和写入以及新建
  9. 移动端的click事件延迟触发的原理是什么?如何解决这个问题?
  10. 前端页面使用ace插件优化脚本
  11. cmd使用conda建立python虚拟环境
  12. 解决ubuntu12.04下安装gitlabError Compiling CSS asset的错误以及401资源错误
  13. 合并多个 SQL 文件,并用 Navicat 执行
  14. cad计算机功能键,【答疑】Auto CAD2014计算器快捷键是什么呢?? - 视频教程线上学...
  15. 基于SSM的网上购物系统的设计与开发
  16. 计算机单位kb和m比较,G、GB、KB、M和MB是怎么回事?
  17. 【致敬童年】Funcode实现坦克大战
  18. vscode: Code Runner直接运行多文件C++程序
  19. 小米运动蓝牙耳机重新配对_小米运动蓝牙耳机怎么连接手机
  20. 2017年的Microsoft Imagine Cup提供的免费Azure申请及使用方法

热门文章

  1. linux卸载oracle11,centos7卸载oracle11g
  2. 2013年中国软件开发者薪资调查报告
  3. 如何快速制作PCB线路板?
  4. 基于Web的图书管理系统设计与实现(附源码地址)
  5. python输出最大的素数_python-最大素数
  6. vs2008整合sp1补丁
  7. python 校验身份证号码 并输出对应省市县生日 demo
  8. 微信API接口目录大全
  9. 国内竞拍网抢拍网前景如何,面对竞拍网抢拍网的暴利
  10. SDUT实验七编程题7-3 求算式的和[1]