
JAVA初窥-DAY13
发布日期:2021-05-07 10:02:00
浏览次数:13
分类:精选文章
本文共 6553 字,大约阅读时间需要 21 分钟。
JAVA初窥-DAY13
两个链表的合成
两个有序链表合成为一个有序链表:
public static MyList addList(MyList list1,MyList list2){ Node f =new Node(-1); Node e =f; if (list1==null&&list2==null){ return null; } for (;list1.head!=null && list2.head!=null;){ if (list1.head.val
两个链表的公共节点
寻找两个链表的公共节点:
main函数MyList list1 = new MyList();//链表1 MyList list2 = new MyList();//链表2 list1.addHead(11); list2.addHead(22); Node node = new Node(999);//创建公共节点 list1.lastNode().next=node;//得到list1的最后一个节点并让其next等于node list2.lastNode().next=node;//得到list2的最后一个节点并让其next等于node list1.addHead(11); list1.addHead(11); list1.addLast(888);//公共部分 list1.addLast(777);//公共部分 list1.addLast(666);//公共部分 list1.show(); list2.show(); System.out.println(MyList.publicNode(list1, list2).val);//输出公共节点的val值
相关方法函数
public static Node publicNode(MyList list1,MyList list2){ Node node = new Node(-1); Node cur1 = list1.head; Node cur2 = list2.head; int i; for (;cur1.next!=null && cur2.next!=null;){ cur1 = cur1.next; cur2 = cur2.next; } if(cur1.next == null){ for (i=0;cur2.next != null;i++){ cur2=cur2.next; } cur1=list1.head; cur2=list2.head; for (;i>0;i--){ cur2 = cur2.next; } for (;cur1 !=cur2;){ cur1 = cur1.next; cur2 = cur2.next; } }else { for (i=0;cur1.next != null;i++){ cur1=cur1.next; } cur1=list1.head; cur2=list2.head; for (;i>0;i--){ cur1 = cur1.next; } for (;cur1 !=cur2;){ cur1 = cur1.next; cur2 = cur2.next; } } if (cur1 != cur2){ System.out.println("没有公共节点"); return node; } return cur1; } public Node lastNode(){ Node cur = this.head; for (;cur.next != null;){ cur = cur.next; } return cur; }
无头双向不循环链表的实现
main函数
public class Demo013 { public static void main(String[] args) { List list = new List(); list.addFirst(3); list.addFirst(2); list.show(); list.addLast(4); list.addFirst(1); list.show(); list.addIndex(4,99); list.addIndex(0,99); list.addIndex(3,99); list.show(); System.out.println(list.listSize); System.out.println("----------1----------"); list.contains(1); list.remove(1); list.show(); System.out.println("----------2----------"); list.removeAllKey(99); list.show(); System.out.println("----------3----------"); list.clear(); list.addLast(1); list.show(); }}
节点及链表及功能
public class List { public Node head;//头 public Node tail;//尾巴 public int listSize=0; //头插法 public void addFirst(int data) { Node node = new Node(data); if(this.head == null){ this.head = node; this.tail = node; }else { this.head.prev = node; node.next =head; this.head = this.head.prev; } listSize++; } //尾插法 public void addLast(int data) { Node node = new Node(data); if (this.tail == null){ this.tail = node; this.head = node; }else { this.tail.next = node; node.prev = this.tail; this.tail = this.tail.next; } listSize++; } //任意位置插入,第一个数据节点为0号下标 public void addIndex(int index,int data){ Node node = new Node(data); Node cur = this.head; if (index<0||index>listSize){ System.out.println("位置不合法!!"); return; } if (index == 0){ this.head.prev = node; node.next =head; this.head = this.head.prev; listSize++; return; }else if (index == listSize){ this.tail.next = node; node.prev = this.tail; this.tail = this.tail.next; listSize++; return; }else { for (; index != 0 ; ) { cur = cur.next; index--; } cur.prev.next = node; node.prev = cur.prev; node.next = cur; cur.prev = node; listSize++; } } //查找是否包含关键字key是否在单链表当中 public boolean contains(int key){ Node cur = this.head; for (;cur!=null;){ if (cur.val == key){ return true; } cur = cur.next; } return false; } //删除第一次出现关键字为key的节点 public void remove(int key){ Node cur = this.head.next; for (;cur!=this.tail;){ if (cur.val == key){ cur.next.prev = cur.prev; cur.prev.next = cur.next; listSize--; return; } cur = cur.next; } if (this.head.val == key){ this.head = this.head.next; this.head.prev =null; listSize--; return; }else if (this.tail.val == key){ this.tail = this.tail.prev; this.tail.next =null; listSize--; return; } System.out.println("未找到要删除的节点,请重新确认!"); } //删除所有值为key的节点 public void removeAllKey(int key){ Node cur = this.head.next; int tmpSize = listSize; for (;cur!=this.tail;){ if (cur.val == key){ cur.next.prev = cur.prev; cur.prev.next = cur.next; listSize--; } cur = cur.next; } if (this.head.val == key){ this.head = this.head.next; this.head.prev=null; listSize--; } if (this.tail.val == key){ this.tail = this.tail.prev; this.tail.next = null; listSize--; } if (tmpSize == listSize){ System.out.println("未找到要删除的节点,请重新确认!"); }else { System.out.println("删除成功!"); } } public void show(){ Node cur = this.head; for (;cur != null;){ System.out.print(cur.val+" "); cur = cur.next; } System.out.println(); } public void clear(){ this.head = null; this.tail = null; listSize = 0; }}class Node { public int val; public Node prev; public Node next; public Node(int val) { this.val = val; }}
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月11日 23时37分18秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
EXTJS4.2——3.1 添加文本框
2019-03-05
WEB基础——AJAX
2019-03-05
one + two = 3
2019-03-05
Kali Day01 --- arpspoof命令进行断网攻击(ARP欺骗)
2019-03-05
echart关系图平分节点删除时自动平衡问题
2019-03-05
【Coursera】Internet History 读书笔记
2019-03-05
《ODAY安全:软件漏洞分析技术》学习心得-----shellcode的一点小小的思考
2019-03-05
Decision tree(决策树)算法初探
2019-03-05
《Unity3D/2D游戏开发从0到1(第二版本)》 书稿完结总结
2019-03-05
sctf_2019_easy_heap
2019-03-06
AT 杂题泛做
2019-03-06
StringBuilder拼接字符串,“,”在前还是在后问题
2019-03-06
给asterisk1.8.7添加menuselct选项
2019-03-06
组合模式
2019-03-06
PyQt5之音乐播放器
2019-03-06
css居中方法与双飞翼布局
2019-03-06
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
2019-03-06