造作网站开发,全网营销胡涛,工商局注册公司流程和费用,wordpress高级设置#x1f435;本篇文章将对双向链表进行讲解#xff0c;模拟实现双向链表的常用方法 一、什么是双向链表
双向链表在指针域上相较于单链表#xff0c;每一个节点多了一个指向前驱节点的引用prev以及多了指向最后一个节点的引用last#xff1a; 二、双向链表的模拟实现
首先… 本篇文章将对双向链表进行讲解模拟实现双向链表的常用方法 一、什么是双向链表
双向链表在指针域上相较于单链表每一个节点多了一个指向前驱节点的引用prev以及多了指向最后一个节点的引用last 二、双向链表的模拟实现
首先将要模拟实现的方法写到IList接口中
public interface IList {//头插法插入节点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();//显示链表public void display();}之后再创建一个MyLinkedList类实现上述接口并重写接口中所有的方法
public class MySingleList implements IList{static class ListNode {public int val;public ListNode next;public ListNode prev;public ListNode(int val) {this.val val;}}public ListNode head; //指向第一个节点public ListNode last; //指向最后一个节点/*以下是要重写IList接口中的方法*/...} 2.1 模拟实现
public void addFirst(int data);
双向链表的头插法和单链表基本一致只不过当链表为空时不仅要让head指向新节点还要让last也指向新节点 public void addFirst(int data) {ListNode newNode new ListNode(data);if (head null) {head newNode;last newNode;return;}newNode.next head;head.prev newNode;head newNode;}public void addLast(int data);
当链表为空时要让head和last都指向新节点当链表部不为空时要让最后一个节点的next指向新节点之后让新节点的prev指向原来的最后一个节点 public void addLast(int data) {ListNode newNode new ListNode(data);if (head null) {head newNode;last newNode;return;}last.next newNode;newNode.prev last;last newNode;}public void addLast(int data);
在任意位置处插入一个节点第一个节点的索引为0
首先要判断一下index是否合法
public class IndexException extends RuntimeException{public IndexException(String message) {super(message);}
}public void addIndex(int index, int data) {if (index 0 || index size()) { //size()为链表长度throw new IndexException(下标错误);}...
}
在任意位置插入节点所以如果index为0或等于链表长度就可以直接使用刚刚实现过的头插和尾插方法
public void addIndex(int index, int data) {if (index 0 || index size()) {throw new IndexException(下标错误);}if (index 0) {addFirst(data);}if (index size()) {addLast(data);}...
}
一般情况下在中间插入单链表中要通过循环找到插入位置的前一个节点但在双向链表中直接循环到插入位置插入位置记为cur即可 public void addIndex(int index, int data) {if (index 0 || index size()) {throw new IndexException(下标错误);}if (index 0) {addFirst(data);return;}if (index size()) {addLast(data);return;}ListNode newNode new ListNode(data);ListNode cur head;for (int i 0; i index; i) {cur cur.next;}/*一般情况下*/newNode.next cur;cur.prev.next newNode;newNode.prev cur.prev;cur.prev newNode;}public void remove(int key);
删除第一次出现val key的节点
先考虑常规情况即通过遍历找到要删除的节点这里记为cur 让cur的前驱节点的next指向cur的后继节点cur的后继节点的prev指向cur的前驱节点 public void remove(int key) {ListNode cur head;while(cur ! null) {if (cur.val key) {cur.prev.next cur.next;cur.next.prev cur.prev;return;}cur cur.next;}}之后有两种特殊情况需要考虑1.cur为第一个节点2.cur为最后一个节点当cur为这两种情况时如果使用上述代码会引发空指针异常所以这两种情况要单独考虑
1.cur为第一个节点此时需要让cur的后继节点prev指向空cur.prev null)并让head head.next但是这样还有一个小问题当链表中只有一个节点时也会引发空指针异常这个问题也要单独处理只需要直接让head null即可
if (cur head) {head head.next;if (head null) {last null;} else {head.prev null;}return;
}2.cur为最后一个节点只需让cur的前驱节点的next指向空并让last last.prev;即可
if (cur.next null) {cur.prev.next null;last last.prev;return;
}remove的最终代码如下 public void remove(int key) {ListNode cur head;while(cur ! null) {if (cur.val key) {if (cur head) {head head.next;if (head null) {last null;} else {head.prev null;}return;}if (cur.next null) {cur.prev.next null;last last.prev;return;}cur.prev.next cur.next;cur.next.prev cur.prev;return;}cur cur.next;}}void removeAllKey(int key);
删除所有val key的节点这里只需要将remove方法修改以下即可 public void removeAllKey(int key) {ListNode cur head;while(cur ! null) {if (cur.val key) {if (cur head) {head head.next;if (head null) {last null;} else {head.prev null;}cur head;continue;}if (cur.next null) {cur.prev.next null;last last.prev;break;}cur.prev.next cur.next;cur.next.prev cur.prev;}cur cur.next;}}剩下的contains() 、size()、clear()、display()方法和上篇文章的单链表实现方法一致 三、LinkedList类讲解
LinkedList类是Java提供的类底层是一个双向链表包含我们刚刚实现过的方法LinkedList也实现了List接口 3.1 LinkedList构造方法
LinkedList有两个构造方法
1.无参构造方法
public LinkedList()
2. 带参构造方法
public LinkedList(Collection? extends E c)
该构造方法可以将c构造为双向链表前提是c实现了Collection接口并且其泛型必须是E或是E的子类例如
ArrayListInteger list1 new ArrayList();
list1.add(1);
list1.add(2);LinkedListInteger list new LinkedList(list1); //list1属于ArrayList类实现了Collection接口泛型和list1一样都是Integer此时顺序表list1就被构造为了双向链表list3.2 LinkedList类常用方法 boolean add(E e) //尾插 evoid add(int index, E element) //将 e 插入到 index 位置boolean addAll(Collection? extends E c) //尾插 c 中的元素E remove(int index) //删除 index 位置元素boolean remove(Object o) //删除遇到的第一个 oE get(int index) //获取下标 index 位置元素E set(int index, E element) //将下标 index 位置元素设置为 elementvoid clear() //清空链表boolean contains(Object o) //判断 o 是否在线性表中int indexOf(Object o) //返回第一个 o 所在下标int lastIndexOf(Object o) //返回最后一个 o 的下标ListE subList(int fromIndex, int toIndex) //截取链表按左闭右开的区间截取[fromIndex, toIndex)这些方法的底层实现方式和我们上述模拟实现的方法的实现方式相同 3.3 LinkedList遍历
1. for循环
for (int i 0; i list.size(); i) {System.out.print(list.get(i) );
}2. for-each
for (int x : list) {System.out.print(x );
} 3.迭代器
顺向遍历
ListIteratorInteger it list.listIterator();
while(it.hasNext()) {System.out.print(it.next() );
}逆向遍历
ListIteratorInteger it1 list.listIterator(list.size());
while(it1.hasPrevious()) {System.out.print(it1.previous() );
}本篇文章到此结束下篇文章会对栈相关知识进行讲解
文章转载自: http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.cnlmp.cn.gov.cn.cnlmp.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.zwdrz.cn.gov.cn.zwdrz.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.dschz.cn.gov.cn.dschz.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.dpbdq.cn.gov.cn.dpbdq.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn