赞
踩
上一节我们描述了顺序表:【Java数据结构】初识线性表之一:顺序表-CSDN博客
并且进行了简单模拟实现。通过源码知道,ArrayList底层使用数组来存储元素。
由于其底层是一段连续空间,当在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景。因此:java集合中又引入了LinkedList,即链表结构。
链表的概念及结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
注意:
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
本章节我们来描述其中两种:
模拟实现无头单向非循环链表主要有以下的方法:
public class SingleLinkedList {
//头插法
public void addFirst(int data){
}
//尾插法
public void addLast(int data){
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
}//删除所有值为key的节点
public void removeAllKey(int key){
}
//得到单链表的长度
public int size(){
return -1;
}
public void clear() {
}
public void display() {}
}
链表的插入:
链表的删除:
模拟链表的代码实现:
- public class MySingLeList {
- static class ListNode{
- public int val;
- public ListNode next;
-
- public ListNode(int val) {
- this.val = val;
- }
- }
- public ListNode head;
-
- public void createList(){
- ListNode node1 = new ListNode(12);
- ListNode node2 = new ListNode(34);
- ListNode node3 = new ListNode(45);
- ListNode node4 = new ListNode(56);
- ListNode node5 = new ListNode(67);
-
- node1.next = node2;
- node2.next = node3;
- node3.next = node4;
- node4.next = node5;
- this.head = node1;
- }
-
- public void display(){
- ListNode tmp = this.head;
- while(!(tmp == null)){
- System.out.print(tmp.val+" ");
- tmp = tmp.next;
- }
- }
-
- public int size(){
- ListNode tmp = this.head;
- int count = 0;
- while(!(tmp == null)){
- count++;
- tmp = tmp.next;
- }
- return count;
- }
-
- public boolean contains(int findData){
- ListNode tmp = this.head;
- while(!(tmp == null)){
- if(tmp.val == findData){
- return true;
- }
- }
- return false;
- }
-
- public void addFirst(int data){
- ListNode node = new ListNode(data);
- node.next = this.head;
- this.head = node;
- }
-
- public void addLast(int data){
- ListNode node = new ListNode(data);
- ListNode tmp = this.head;
- if(tmp == null){
- this.head = node;
- return;
- }
- while(!(tmp.next == null)){
- tmp = tmp.next;
- }
- tmp.next = node;
- }
-
- public void addIndex(int pos,int data){
- ListNode node = new ListNode(data);
- ListNode tmp = this.head;
- if(pos == 0){
- node.next = tmp;
- this.head = node;
- return;
- } else if (pos == this.size()) {
- this.addLast(data);
- } else if (pos > this.size()) {
- System.out.println("输入的位置错误!");
- }else{
- for (int i = 0; i < pos -1; i++) {
- tmp = tmp.next;
- }
- node.next = tmp.next;
- tmp.next = node;
- }
- }
-
- public void remove(int data){
- int flag = 1;
- ListNode tmp = this.head;
- if(this.head.val == data){
- this.head = this.head.next;
- return;
- }
- while(tmp.next != null){
- if(tmp.next.val== data){
- flag = 0;
- break;
- }
- tmp = tmp.next;
- }
- if(flag == 0){
- tmp.next = tmp.next.next;
- }else{
- System.out.println("链表中没有该元素!");
- }
- }
- public void removeAll(int data){
- ListNode prv = this.head;
- ListNode tmp = this.head.next;
- while(tmp != null){
- if(tmp.val == data){
- prv.next = tmp.next;
- tmp = tmp.next;
- }else{
- prv = tmp;
- tmp = tmp.next;
- }
- }
- if(this.head.val == data){
- this.head = this.head.next;
- }
- }
-
- public void clear(){
- this.head = null;
- }
- }
无头双向链表主要有以下的方法:
public class MyLinkedList {
//头插法
public void addFirst(int data){ }
//尾插法
public void addLast(int data){}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){}
//删除第一次出现关键字为key的节点
public void remove(int key){}
//删除所有值为key的节点
public void removeAllKey(int key){}
//得到单链表的长度
public int size(){}
public void display(){}
public void clear(){}
}
双向链表的插入:
双向链表的删除:
模拟代码实现:
- public class MyLinkedList {
- static class ListNode{
- private int val;
- private ListNode prev;
- private ListNode next;
-
- public ListNode(int val) {
- this.val = val;
- }
- }
- ListNode head;
- ListNode last;
-
- //头插法
- public void addFirst(int data){
- ListNode node = new ListNode(data);
- if(head == null){
- head = node;
- last = node;
- }else{
- node.next = head;
- head.prev = node;
- head = node;
- }
- }
- //尾插法
- public void addLast(int data){
- ListNode node = new ListNode(data);
- if(head == null){
- head = node;
- last = node;
- }else{
- last.next = node;
- node.prev = last;
- last = node;
- }
- }
- //任意位置插入,第一个数据节点为0号下标
- public void addIndex(int pos,int data){
- ListNode node = new ListNode(data);
- ListNode cur = head;
- if(pos == 0){
- addFirst(data);
- }else if(pos == this.size()){
- addLast(data);
- }else if(pos < 0 || pos > this.size()){
- System.out.println("插入位置错误!");
- }else{
- for (int i = 0; i < pos; i++) {
- cur = cur.next;
- }
- cur.prev.next = node;
- node.prev = cur.prev;
- node.next = cur;
- cur.prev = node;
- }
-
- }
- //查找是否包含关键字key是否在单链表当中
- public boolean contains(int key){
- ListNode tmp = this.head;
- while(!(tmp == null)){
- if(tmp.val == key){
- return true;
- }
- }
- return false;
- }
- //删除第一次出现关键字为key的节点
- public void remove(int key){
- ListNode cur = head;
- while(cur != null){
- if(cur.val == key){
- if(cur == head){
- head = head.next;
- if(head != null){
- head.prev =null;
- }else{
- last = null;
- }
- return;
- }else if(cur == last){
- last = last.prev;
- last.next = null;
- return;
- }else{
- cur.prev.next = cur.next;
- cur.next.prev = cur.prev;
- return;
- }
- }else{
- cur = cur.next;
- }
- }
- System.out.println("没有该元素!");
- return;
- }
- //删除所有值为key的节点
- public void removeAllKey(int key){
- ListNode cur = head;
- while(cur != null){
- if(cur.val == key){
- if(cur == head){
- head = head.next;
- if(head != null){
- head.prev =null;
- }else{
- last = null;
- }
- }else if(cur == last){
- last = last.prev;
- last.next = null;
- }else{
- cur.prev.next = cur.next;
- cur.next.prev = cur.prev;
- }
- }
- cur = cur.next;
- }
- System.out.println("没有该元素!");
- return;
- }
- //得到链表的长度
- public int size(){
- int count = 0;
- ListNode cur = head;
- while(cur != null){
- count++;
- cur = cur.next;
- }
- return count;
- }
- public void display(){
- ListNode tmp = this.head;
- while(!(tmp == null)){
- System.out.print(tmp.val+" ");
- tmp = tmp.next;
- }
- }
- public void clear(){
- ListNode cur = head;
- while(cur != null){
- ListNode curNext = cur.next;
- cur.prev = null;
- cur.next = null;
- cur = curNext;
- }
- head = null;
- last = null;
- }
- }
LinkedList的底层是双向链表结构(链表后面介绍),由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。
在集合框架中,LinkedList也实现了List接口,具体如下:
注意 :
- public class Main {
- public static void main(String[] args) {
- List<Integer> list1 = new LinkedList<>();//无参构造
-
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);
- list2.add(2);
- list2.add(3);
- List<Integer> list3 = new LinkedList<>(list2);//使用其他集合容器中元素构造List
- list3.add(4);
- System.out.println(list3);
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Integer> list1 = new LinkedList<>();
- list1.add(5);
- list1.add(6);
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);//尾插
- list2.add(2);
- list2.add(3);
- list2.add(1,4);//在指定位置插入节点
- list2.addAll(list1);//尾插其他容器中的所有节点
- System.out.println(list2);
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);
- list2.add(2);
- list2.add(3);
- list2.remove(1);//删除指定位置的节点
- list2.remove(new Integer(3));//删除指定元素的节点
- System.out.println(list2);
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);
- list2.add(2);
- list2.add(3);
-
- System.out.println(list2.get(1));
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);
- list2.add(2);
- list2.add(3);
- list2.set(1,4);
- System.out.println(list2);
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);
- list2.add(2);
- list2.add(3);
- System.out.println(list2.contains(new Integer(2)));
- }
- }
- public class Main {
- public static void main(String[] args) {
- List<Integer> list2 = new LinkedList<>();
- list2.add(1);
- list2.add(2);
- list2.add(3);
- System.out.println(list2.subList(0,2));
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。