【数据结构】模拟实现LinkedList

编程入门 行业动态 更新时间:2024-10-26 02:27:52

【<a href=https://www.elefans.com/category/jswz/34/1769880.html style=数据结构】模拟实现LinkedList"/>

【数据结构】模拟实现LinkedList

LinkedList是双向链表结构可以适用于任意插入场景下的插入和删除,效率较高,时间复杂度为O(1)。

模拟实现

public class MyLinkedList {static class ListNode{private int val;//值域private ListNode prev;//前驱private ListNode next;//后继public ListNode(int val) {this.val = val;}}public ListNode head;//双向链表的头节点public ListNode last;//双向链表的尾节点
}

LinkedList常用方法

//头插法
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 clear()

实现addFirst方法(头插法)

public void addFirst(int data){ListNode node = new ListNode(data);//如果链表为空 插入的元素就是头节点和尾节点if (head==null){head = node;last = node;}else {node.next = head;//使node的后继是现在的头节点head.prev = node;//使现在的头节点的前驱是nodehead = node;//让node成为新的头节点}
}

实现addList方法(尾插法)

public void addLast(int data){ListNode node = new ListNode(data);//和头插一样if (last==null){head = node;last = node;}else {last.next = node;//使现在尾节点的后继为nodenode.prev = last;//使node的前驱为现在的尾节点last = last.next;//让node成为尾节点}
}

实现size方法(求链表长度)

public int size(){ListNode cur = head;int count = 0;while (cur!=null){count++;cur = cur.next;}return count;
}

实现addIndex方法(在任意位置插入元素)

public void addIndex(int index,int data){//插入的位置如果为0 可以使用头插法if (index==0){addFirst(data);return;}//如果在最后一个位置插入 可以使用尾插法if (index==size()){addLast(data);return;}ListNode node = new ListNode(data);//判断要插入的下标是否合法if (index<0||index>size()){System.out.println("index 不合法"+index);return;}ListNode cur = head;//让cur走到要插入的位置while (index!=0){cur = cur.next;index--;}node.next = cur;cur.prev.next = node;node.prev = cur.prev;cur.prev = node;
}

实现contains方法(查找是否包含关键字key是否在单链表当中)

public boolean contains(int key){if (head==null){return false;}ListNode cur = head;while (cur!=null){if (cur.val==key){return true;}cur = cur.next;}return false;
}

实现remove方法(删除第一次出现关键字为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){//只有一个头节点cur.prev=null;}else {last=null;}}else {if (cur.next!=null){//删除中间节点cur.prev.next=cur.next;cur.next.prev=cur.prev;}else {//删除尾节点cur.prev.next=cur.next;last=last.prev;}}return;}else {cur=cur.next;}}
}

实现removeAllkey(删除所有值为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){//只有一个头节点cur.prev=null;}else {last=null;}}else {if (cur.next!=null){//删除中间节点cur.prev.next=cur.next;cur.next.prev=cur.prev;}else {//删除尾节点cur.prev.next=cur.next;last=last.prev;}}cur=cur.next;}else {cur=cur.next;}}
}

实现clear方法(清除链表)

public void clear(){ListNode cur = head;while (cur!=null){ListNode curNew = cur.next;cur.prev=null;cur.next=null;cur = curNew;}head=null;last=null;
}

更多推荐

【数据结构】模拟实现LinkedList

本文发布于:2023-12-05 08:26:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1663665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据结构   LinkedList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!