为什么要有链表
上一节我们描述了顺序表:【java数据结构】初识线性表之一:顺序表-csdn博客
并且进行了简单模拟实现。通过源码知道,arraylist底层使用数组来存储元素。
由于其底层是一段连续空间,当在arraylist任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为o(n),效率比较低,因此arraylist不适合做任意位置插入和删除比较多的场景。因此:java集合中又引入了linkedlist,即链表结构。
链表
链表的概念及结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
注意:
- 链表的结构在逻辑上是连续的,但是在物理位置上不一定连续。
- 节点一般都是从堆上申请出来的。
- 从堆上申请空间是按一定策略来分配的,两次申请的空间可能会连续,也可能不连续。
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
- 单向或者双向
- 带头或者不带头
- 循环或者非循环
本章节我们来描述其中两种:
- 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。
- 无头双向链表:在java的集合框架库中linkedlist底层实现就是无头双向循环链表。
模拟实现无头单向非循环链表
模拟实现无头单向非循环链表主要有以下的方法:
链表的插入:
链表的删除:
模拟链表的代码实现:
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 {
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
linkedlist的底层是双向链表结构(链表后面介绍),由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。
在集合框架中,linkedlist也实现了list接口,具体如下:
注意 :
- 1. linkedlist实现了list接口
- 2. linkedlist的底层使用了双向链表
- 3. linkedlist没有实现randomaccess接口,因此linkedlist不支持随机访问
- 4. linkedlist的任意位置插入和删除元素时效率比较高,时间复杂度为o(1)
- 5. linkedlist比较适合任意位置插入的场景
linkedlist 的构造方法
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);
}
}
linkedlist 常用方法介绍
插入节点
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)));
}
}
截取部分 list
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));
}
}
发表评论