My_Single_LinkedList

分4个部分实现(CRUD - 增删改查)。

首先要有一个Node(节点类)

     class Node {
public int val;
public Node next; public Node(int val) {
this.val = val;
this.next = null;
}
}

instance variables and constructer - 成员变量和构造函数

     public Node head; //头指针指向虚拟的node,真正的list是取head.next
public Node last; //尾指针指向最后一个元素
public int length; //list的长度,不是index。在insert的时候,判断insert的index public My_Single_LinkedList() {
this.head = new Node(-1);
this.last = head;
this.length = 0;
}

1. 添加

  添加部分有两大部分,一个是添加在头和尾,一个是添加在某个index的左和右。

  添加的同时要保持length和last指针的更新

  1) 添加在头和尾:O(1)的时间复杂度

     public void addLast(int val) {
last.next = new Node(val);
last = last.next;
this.length++;
} public void addFirst(int val) {
Node newNode = new Node(val);
newNode.next = head.next;
head.next = newNode;
this.length++;
}

  2) 在某个index的左和右添加:O(n)时间复杂度

     /**
* insert before index
* @param val
* @param index
*/
public void insertBefore(int val, int index) {
if (index <= 0) { //在0之前加,相当于addFirst
addFirst(val);
return;
}
if (index >= length) {//在超出length之前加,相当于addLast
addLast(val);
return;
}
// 普通情况,两个指针,一前一后
Node cur = head.next;
Node prev = head;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur;
prev.next = newNode;
this.length++;
} /**
* insert after index
* @param val
* @param index certain index you want to insert at
*/
public void insertAfter(int val, int index) {
if (index < 0) {
addFirst(val);
return;
}
if (index >= length - 1) {
addLast(val);
return;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur.next;
cur.next = newNode;
this.length++;
}

2. 更新:O(n)时间复杂度

     public void updateAt(int val, int index) {
if (index < 0 || index > length - 1) {
return;
}
int counter = 0;
Node cur = head.next;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
cur.val = val;
}

3. 删除:O(n)时间复杂度

删除的时候也要注意length和last的更新

     public void deleteAt(int index) {
if (index < 0 || index > length - 1) {
return;
}
int counter = 0;
Node prev = head;
Node cur = head.next;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
prev.next = prev.next.next;
//更新length和last指针
length--;
while (prev.next != null) {
prev = prev.next;
}
this.last = prev;
}

4. 搜索:O(n)时间复杂度

     /**
* 根据val搜索
* 返回第一个符合条件node的index
* 如果没有,返回-1
*/
public int getByValue(int val) {
Node cur = head.next;
int index = 0; while (cur != null) {
if (cur.val == val) {
return index;
}
cur = cur.next;
index++;
}
return -1;
} /**
* 根据index搜索
*/
public Node get(int index) {
if (index < 0 || index > length - 1) {
return null;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
return cur;
}

完整代码:

 /**
* Created by Mingxiao on 9/5/2016.
*/
public class My_Single_LinkedList { class Node {
public int val;
public Node next; public Node(int val) {
this.val = val;
this.next = null;
}
} public Node head; //头指针指向虚拟的node,真正的list是取head.next
public Node last; //尾指针指向最后一个元素
public int length; //list的长度,不是index。在insert的时候,判断insert的index public My_Single_LinkedList() {
this.head = new Node(-1);
this.last = head;
this.length = 0;
}
// =============== INSERT==========================
public void addLast(int val) {
last.next = new Node(val);
last = last.next;
this.length++;
} public void addFirst(int val) {
Node newNode = new Node(val);
newNode.next = head.next;
head.next = newNode;
this.length++;
} /**
* insert before index
* @param val
* @param index
*/
public void insertBefore(int val, int index) {
if (index <= 0) { //在0之前加,相当于addFirst
addFirst(val);
return;
}
if (index >= length) {//在超出length之前加,相当于addLast
addLast(val);
return;
}
// 普通情况,两个指针,一前一后
Node cur = head.next;
Node prev = head;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur;
prev.next = newNode;
this.length++;
} /**
* insert after index
* @param val
* @param index certain index you want to insert at
*/
public void insertAfter(int val, int index) {
if (index < 0) {
addFirst(val);
return;
}
if (index >= length - 1) {
addLast(val);
return;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur.next;
cur.next = newNode;
this.length++;
} //===================update=========================
public void updateAt(int val, int index) {
if (index < 0 || index > length - 1) {
return;
}
int counter = 0;
Node cur = head.next;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
cur.val = val;
}
//=====================delete=======================
public void deleteAt(int index) {
if (index < 0 || index > length - 1) {
return;
}
int counter = 0;
Node prev = head;
Node cur = head.next;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
prev.next = prev.next.next;
//更新length和last指针
length--;
while (prev.next != null) {
prev = prev.next;
}
this.last = prev;
}
//===================search================================ /**
* 根据val搜索
* 返回第一个符合条件node的index
* 如果没有,返回-1
*/
public int getByValue(int val) {
Node cur = head.next;
int index = 0; while (cur != null) {
if (cur.val == val) {
return index;
}
cur = cur.next;
index++;
}
return -1;
} /**
* 根据index搜索
*/
public Node get(int index) {
if (index < 0 || index > length - 1) {
return null;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
return cur;
} public void print() {
if (head.next == null) {
System.out.println("null");
return;
}
Node cur = head.next;
while (cur.next != null) {
System.out.print(cur.val + "->");
cur = cur.next;
}
System.out.println(cur.val);
} public static void main(String[] args) {
My_Single_LinkedList list = new My_Single_LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(0);
list.insertAfter(5,0);
list.updateAt(9, 1);
list.deleteAt(2);
System.out.println(list.get(2).val);
list.print();
}
}

自己实现Single LinkedList的更多相关文章

  1. Enhancing the Scalability of Memcached

    原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...

  2. cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  3. JDK源代码学习-ArrayList、LinkedList、HashMap

    ArrayList.LinkedList.HashMap是Java开发中非常常见的数据类型.它们的区别也非常明显的,在Java中也非常具有代表性.在Java中,常见的数据结构是:数组.链表,其他数据结 ...

  4. [CareerCup] Single Valid Tree

    https://www.careercup.com/question?id=5103530547347456 Given a list of nodes, each with a left child ...

  5. To Java程序员:切勿用普通for循环遍历LinkedList

    ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...

  6. 计算机程序的思维逻辑 (39) - 剖析LinkedList

    上节我们介绍了ArrayList,ArrayList随机访问效率很高,但插入和删除性能比较低,我们提到了同样实现了List接口的LinkedList,它的特点与ArrayList几乎正好相反,本节我们 ...

  7. 深入理解java中的ArrayList和LinkedList

    杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...

  8. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  9. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

随机推荐

  1. 学习Hadoop的资料

    1)Cygwin相关资料 (1)Cygwin上安装.启动ssh服务失败.ssh localhost失败的解决方案 地址:http://blog.163.com/pwcrab/blog/static/1 ...

  2. ACM - ICPC World Finals 2013 B Hey, Better Bettor

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这题真心的麻烦……程序不长但是推导过程比较复杂,不太好想 ...

  3. iOS开发:记录开发中遇到的编译或运行异常以及解决方案

    1.部署到真机异常 dyld`dyld_fatal_error: ->  0x120015088 <+0>: brk    #0x3 dyld: Library not loaded ...

  4. 转载:Unobtrusive JavaScript in ASP.NET MVC 3 隐式的脚本在MVC3

    Unobtrusive JavaScript 是什么? <!--以下是常规Javascript下写出来的Ajax--> <div id="test"> &l ...

  5. 通过CSS禁止Chrome自动为输入框添加橘黄色边框,修改/禁止 chrome input边框颜色,

    1   /*Chrome浏览器 点击input 黄色边框 禁用*/ .NoOutLine:focus{outline: none} <asp:TextBox ID="txtTeleph ...

  6. wdcp系统升级mysql5.7.11

    1.下载解压 下载地址为:http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz ...

  7. HDU 2063 过山车 (最大匹配,匈牙利算法)

    题意:中文题目 思路:匈牙利算法解决二分图最大匹配问题. #include <bits/stdc++.h> using namespace std; ; int mapp[N][N]; / ...

  8. oracle 回收站管理

    oracle10g,在pl/sql中选中删除后会出现类似:BIN$nJ5JuP9cQmqPaArFei384g==$0的表. 1.查看回收站 select * from user_recyclebin ...

  9. HWM的实验

    HWM是数据段中使用空间和未使用空间之间的界限,假如现有自由链表上的数据块不能满足需求,Oracle把HWM指向的数据块加入到自由链表上,HWM向前移动到下一个数据块.简单说,一个数据段中,HWM左边 ...

  10. getDeclaredMethods()和getMethods()区别

    getDeclaredMethods()          返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共.保护.默认(包)访问和私有方法, ...