public class MyList {

    static class Node {// 节点类
Object data;
Node next; public Node(Object data) {// 构造方法,为data赋值
this.data = data;
this.next = null;
}
} Node head; public MyList() {
head = null;// 链表的构造方法
} public void clear() {// 清除链表
head = null;
} public void bianli()// 遍历
{
Node p = head;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
} public boolean isEmpty()// 推断是否为空
{
return head == null;
} public int size() {// 节点个数
Node p = head;
int sum = 0;
while (p != null) {
sum++;
p = p.next;
}
return sum;
} // 在指定位置插入元素。下标从0開始
public void insert(Object d, int pos) {
if (pos < 0 || pos > size()) {
throw new RuntimeException("下标错误");
}
Node newNode = new Node(d);
if (pos == 0) {
newNode.next = head;
head = newNode;
} else if (pos >= size() - 1) {
get(size() - 1).next = newNode;
} else {
newNode.next = get(pos);
get(pos - 1).next = newNode;
}
} public Node get(int pos) {
if (pos < 0 || pos > size()) {
throw new RuntimeException("下标错误");
}
if (pos == 0)
return head;
Node p = head;
for (int i = 0; i < pos; i++)
p = p.next;
return p;
} public static void main(String[] args) { MyList list = new MyList();
list.insert(10, 0);
list.insert(20, 1);
list.insert(30, 0);
list.insert(40, 1); System.out.println(list.size());
list.bianli();
System.out.println(list.isEmpty());
System.out.println(list.get(2).data);
list.clear();
System.out.println(list.isEmpty());
} }

最新文章

  1. 电路相关知识--读&lt;&lt;继电器是如何成为CPU的&gt;&gt;
  2. 最简实例说明wait、notify、notifyAll的使用方法
  3. junit单元测试中私有方法测试
  4. POJ 3525 半平面交+二分
  5. 如何在oracle中导入dmp数据库文件
  6. 实现Linux select IO复用C/S服务器代码
  7. Android开发心得(转)
  8. microchip PIC芯片使用方法
  9. C# 面向对象 , 类与对象
  10. PPT 制作必备工具
  11. UVALive 5102 Fermat Point in Quadrangle 极角排序+找距离二维坐标4个点近期的点
  12. 关于PCA算法的一点学习总结
  13. .Net Webapi Swagger增加登录功能
  14. SDN网络虚拟化中有效协调的映射算法
  15. java中的编译时常量与运行时常量
  16. Burp Suite Intruder中爆破模式介绍
  17. flume初识
  18. java生成zip压缩文件,解压缩文件
  19. String 字符串中含有 Unicode 编码时,转为UTF-8
  20. 薛兆丰吴军何帆曾鸣万维刚李笑来罗永浩等得到APP专栏作者的书23本

热门文章

  1. CRegKey
  2. 签名mobileconfig 重签ipa
  3. 01Oracle Database
  4. 第2节 hive基本操作:12、hive当中的hql语法
  5. vue 中slot 的具体用法
  6. 页面jsp向后端发送:HTTP 400错误 - 请求无效(Bad request)
  7. 自媒体人Chrome浏览器必备插件精选神器!
  8. PAT顶级 1002. Business (35)
  9. 圆角计算 Shader
  10. 每天学点Python之collections