自己实现Single LinkedList

My_Single_LinkedList

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

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

1     class Node {
2         public int val;
3         public Node next;
4
5         public Node(int val) {
6             this.val = val;
7             this.next = null;
8         }
9     }

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

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

1. 添加

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

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

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

 1     public void addLast(int val) {
 2         last.next = new Node(val);
 3         last = last.next;
 4         this.length++;
 5     }
 6
 7     public void addFirst(int val) {
 8         Node newNode = new Node(val);
 9         newNode.next = head.next;
10         head.next = newNode;
11         this.length++;
12     }

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

 1     /**
 2      * insert before index
 3      * @param val
 4      * @param index
 5      */
 6     public void insertBefore(int val, int index) {
 7         if (index <= 0) { //在0之前加,相当于addFirst
 8             addFirst(val);
 9             return;
10         }
11         if (index >= length) {//在超出length之前加,相当于addLast
12             addLast(val);
13             return;
14         }
15         // 普通情况,两个指针,一前一后
16         Node cur = head.next;
17         Node prev = head;
18         int counter = 0;
19         while (cur != null && counter < index) {
20             cur = cur.next;
21             prev = prev.next;
22             counter++;
23         }
24         Node newNode = new Node(val);
25         newNode.next = cur;
26         prev.next = newNode;
27         this.length++;
28     }
29
30     /**
31      * insert after index
32      * @param val
33      * @param index certain index you want to insert at
34      */
35     public void insertAfter(int val, int index) {
36         if (index < 0) {
37             addFirst(val);
38             return;
39         }
40         if (index >= length - 1) {
41             addLast(val);
42             return;
43         }
44         Node cur = head.next;
45         int counter = 0;
46         while (cur != null && counter < index) {
47             cur = cur.next;
48             counter++;
49         }
50         Node newNode = new Node(val);
51         newNode.next = cur.next;
52         cur.next = newNode;
53         this.length++;
54     }

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

 1     public void updateAt(int val, int index) {
 2         if (index < 0 || index > length - 1) {
 3             return;
 4         }
 5         int counter = 0;
 6         Node cur = head.next;
 7         while (cur != null && counter < index) {
 8             cur = cur.next;
 9             counter++;
10         }
11         cur.val = val;
12     }

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

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

 1     public void deleteAt(int index) {
 2         if (index < 0 || index > length - 1) {
 3             return;
 4         }
 5         int counter = 0;
 6         Node prev = head;
 7         Node cur = head.next;
 8         while (cur != null && counter < index) {
 9             cur = cur.next;
10             prev = prev.next;
11             counter++;
12         }
13         prev.next = prev.next.next;
14         //更新length和last指针
15         length--;
16         while (prev.next != null) {
17             prev = prev.next;
18         }
19         this.last = prev;
20     }

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

 1     /**
 2      * 根据val搜索
 3      * 返回第一个符合条件node的index
 4      * 如果没有,返回-1
 5      */
 6     public int getByValue(int val) {
 7         Node cur = head.next;
 8         int index = 0;
 9
10         while (cur != null) {
11             if (cur.val == val) {
12                 return index;
13             }
14             cur = cur.next;
15             index++;
16         }
17         return -1;
18     }
19
20     /**
21      * 根据index搜索
22      */
23     public Node get(int index) {
24         if (index < 0 || index > length - 1) {
25             return null;
26         }
27         Node cur = head.next;
28         int counter = 0;
29         while (cur != null && counter < index) {
30             cur = cur.next;
31             counter++;
32         }
33         return cur;
34     }

完整代码:

  1 /**
  2  * Created by Mingxiao on 9/5/2016.
  3  */
  4 public class My_Single_LinkedList {
  5
  6     class Node {
  7         public int val;
  8         public Node next;
  9
 10         public Node(int val) {
 11             this.val = val;
 12             this.next = null;
 13         }
 14     }
 15
 16     public Node head; //头指针指向虚拟的node,真正的list是取head.next
 17     public Node last; //尾指针指向最后一个元素
 18     public int length; //list的长度,不是index。在insert的时候,判断insert的index
 19
 20     public My_Single_LinkedList() {
 21         this.head = new Node(-1);
 22         this.last = head;
 23         this.length = 0;
 24     }
 25 // =============== INSERT==========================
 26     public void addLast(int val) {
 27         last.next = new Node(val);
 28         last = last.next;
 29         this.length++;
 30     }
 31
 32     public void addFirst(int val) {
 33         Node newNode = new Node(val);
 34         newNode.next = head.next;
 35         head.next = newNode;
 36         this.length++;
 37     }
 38
 39     /**
 40      * insert before index
 41      * @param val
 42      * @param index
 43      */
 44     public void insertBefore(int val, int index) {
 45         if (index <= 0) { //在0之前加,相当于addFirst
 46             addFirst(val);
 47             return;
 48         }
 49         if (index >= length) {//在超出length之前加,相当于addLast
 50             addLast(val);
 51             return;
 52         }
 53         // 普通情况,两个指针,一前一后
 54         Node cur = head.next;
 55         Node prev = head;
 56         int counter = 0;
 57         while (cur != null && counter < index) {
 58             cur = cur.next;
 59             prev = prev.next;
 60             counter++;
 61         }
 62         Node newNode = new Node(val);
 63         newNode.next = cur;
 64         prev.next = newNode;
 65         this.length++;
 66     }
 67
 68     /**
 69      * insert after index
 70      * @param val
 71      * @param index certain index you want to insert at
 72      */
 73     public void insertAfter(int val, int index) {
 74         if (index < 0) {
 75             addFirst(val);
 76             return;
 77         }
 78         if (index >= length - 1) {
 79             addLast(val);
 80             return;
 81         }
 82         Node cur = head.next;
 83         int counter = 0;
 84         while (cur != null && counter < index) {
 85             cur = cur.next;
 86             counter++;
 87         }
 88         Node newNode = new Node(val);
 89         newNode.next = cur.next;
 90         cur.next = newNode;
 91         this.length++;
 92     }
 93
 94 //===================update=========================
 95     public void updateAt(int val, int index) {
 96         if (index < 0 || index > length - 1) {
 97             return;
 98         }
 99         int counter = 0;
100         Node cur = head.next;
101         while (cur != null && counter < index) {
102             cur = cur.next;
103             counter++;
104         }
105         cur.val = val;
106     }
107 //=====================delete=======================
108     public void deleteAt(int index) {
109         if (index < 0 || index > length - 1) {
110             return;
111         }
112         int counter = 0;
113         Node prev = head;
114         Node cur = head.next;
115         while (cur != null && counter < index) {
116             cur = cur.next;
117             prev = prev.next;
118             counter++;
119         }
120         prev.next = prev.next.next;
121         //更新length和last指针
122         length--;
123         while (prev.next != null) {
124             prev = prev.next;
125         }
126         this.last = prev;
127     }
128 //===================search================================
129
130     /**
131      * 根据val搜索
132      * 返回第一个符合条件node的index
133      * 如果没有,返回-1
134      */
135     public int getByValue(int val) {
136         Node cur = head.next;
137         int index = 0;
138
139         while (cur != null) {
140             if (cur.val == val) {
141                 return index;
142             }
143             cur = cur.next;
144             index++;
145         }
146         return -1;
147     }
148
149     /**
150      * 根据index搜索
151      */
152     public Node get(int index) {
153         if (index < 0 || index > length - 1) {
154             return null;
155         }
156         Node cur = head.next;
157         int counter = 0;
158         while (cur != null && counter < index) {
159             cur = cur.next;
160             counter++;
161         }
162         return cur;
163     }
164
165     public void print() {
166         if (head.next == null) {
167             System.out.println("null");
168             return;
169         }
170         Node cur = head.next;
171         while (cur.next != null) {
172             System.out.print(cur.val + "->");
173             cur = cur.next;
174         }
175         System.out.println(cur.val);
176     }
177
178
179     public static void main(String[] args) {
180         My_Single_LinkedList list = new My_Single_LinkedList();
181         list.addLast(1);
182         list.addLast(2);
183         list.addLast(0);
184         list.insertAfter(5,0);
185         list.updateAt(9, 1);
186         list.deleteAt(2);
187         System.out.println(list.get(2).val);
188         list.print();
189     }
190 }
时间: 2024-10-14 16:57:56

自己实现Single LinkedList的相关文章

Remove duplicate in single linkedlist

1/* 1.Node.java*/ 2 package Chapter2; 3 4 public class Node<T> { //T可以是int,string等 5 public T data; 6 public Node<T> next; 7 public Node(T d, Node<T> n){ 8 data = d; 9 next = n; 10 } 11 } 12 13 /*2.MyLinkedList.java*/ 14 15 package Chapt

Enhancing the Scalability of Memcached

原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduction - Memcached and Web Services Memcached is a Key-Value cache used by cloud and web service delivery companies, such as Facebook [1], Twitter [2], R

Java AVL、BST编程作业代写、代做JDK实验

Java AVL.BST编程作业代写.代做JDK实验1 IntroductionNeeding a way to manage all your PlayStation friends, you decide to build a backendsystem for adding, removing and maintaining them. The idea is to organiseyour friends so you can search for individuals, search

Java中ArrayList和LinkedList区别

一般大家都知道ArrayList和LinkedList的大致区别:      1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针.      3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据. ArrayList和LinkedList是两个集合类,用于存储一系列的对象引用

ArrayList&amp;LinkedList&amp;Map&amp;Arrays

Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将obj加入到调用类集合中,加入返回true 否则 返回 false Boolean addAll(Collection c) 将c中所有元素都加入到类集合中,都加入返回true否则 false Void clean() 从调用类集合中删除所有元素 Boolean contains(Object obj

Java集合类库 LinkedList 源码解析

基于JDK 1.7,和ArrayList进行比较分析 Java已经有了ArrayList,用来存放元素,对元素的操作都很方便.为什么还会有LinkedList呢?我们都知道ArrayList获取元素很快,但是插入一个元素很慢,因为ArrayList底层维护的是一个数组,往数组中的某个位置插入一个元素,是很消耗资源的. 而LinkedList插入元素很快,获取任意位置的元素却很慢.这是为什么呢?底层又是怎样实现的呢? 1.继承关系 LinkedList的继承关系图: LinkedList继承的是A

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

ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: public static void main(String[] args) { List<Integer> arrayList = new ArrayList<Integer>(); for (int i = 0; i < 100; i++) arrayList.add(i);

从头认识java-9.7 LinkedList

这一章节我们来简单介绍一个LinkedList的一些方法与特性. 1.特性 在中间插入或者删除元素会比ArrayList的性能好,但是有不一定的情况,请点击(List的简介与性能),里面有一个简单的测试 2.方法演示 package com.ray.ch09; import java.util.Arrays; import java.util.LinkedList; public class Test { public static void main(String[] args) { Link

java的List接口的实现类 ArrayList,LinkedList,Vector 的区别

Java的List接口有3个实现类,分别是ArrayList.LinkedList.Vector,他们用于存放多个元素,维护元素的次序,而且允许元素重复. 3个具体实现类的区别如下: 1. ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中.当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制.移动.代价比较高.因此,它