
无头单向不循环链表
发布日期:2021-05-07 10:01:59
浏览次数:23
分类:精选文章
本文共 7054 字,大约阅读时间需要 23 分钟。
无头单向不循环链表
main函数(测试):
public class Demo11 { public static void main(String[] args) { MyList list = new MyList(); list.create();//穷举建4个节点,若链表为空直接添加,若不为空则尾插 list.show(); list.addHead(6);//头插6 list.show(); list.addLast(8);//尾插8 list.show(); list.create(); list.show(); list.addAny(999,9);//9位置插入999节点 list.show(); System.out.println(list.size);//输出长度 System.out.println("-------------1--------------"); System.out.println(list.Key(999));//查找是否有999 System.out.println(list.keyNum(999));//查找999的下标 System.out.println("--------------2-------------"); list.delKey(999);//删除数据为5的第一个节点 list.show(); System.out.println(list.size); System.out.println("------------3--------------"); list.del(1);//删除所有为1的节点 list.show(); System.out.println(list.size); System.out.println("------------4---------------"); System.out.println(list.midNode().val);//输出中间节点的值 list.inverse();//逆序 list.show(); System.out.println("-------------5--------------"); System.out.println(list.back(4).val); System.out.println("----------6-----------------"); list.sort(4); list.show(); System.out.println("------------7---------------"); list.addLast(1); list.addLast(2); list.addLast(1); System.out.println(list.plalindrome()); System.out.println("-------------8--------------"); list = new MyList(); list.create(); list.ringC(); System.out.println(list.ring()); System.out.println(list.ringB()); System.out.println("-------------9--------------"); list = new MyList(); list.addLast(1); list.addLast(1); list.addLast(2); list.addLast(2); list.addLast(2); list.addLast(4); list.addLast(5); list.clear(); list.show(); }}
链表及节点
public class MyList { public Node head = null; public int size = 0;//获取长度 public void create() { //穷举 if (size==0){ Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); node1.next = node2; node2.next = node3; node3.next = node4; this.head = node1; size = 4; }else{ Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); node1.next = node2; node2.next = node3; node3.next = node4; Node cur = this.head; for (;cur.next!=null;cur = cur.next); cur.next=node1; size = size+4; } } public void addHead(int val){ //头插法 Node node = new Node(val); node.next = this.head; this.head = node; size ++; } public void addLast(int val) { //尾插法 Node cur = this.head; if (cur == null){ Node node = new Node(val); this.head = node; } for (;cur != null;){ if (cur.next == null) { Node node = new Node(val); cur.next = node; size ++; return; } cur = cur.next; } } public void addAny(int val,int pos){ //任意位置插入,头结点为0 Node cur = this.head; int i; if (pos<0||pos>size){ System.out.println("pos位置不合法"); return ; }else if (pos == 0){ Node add = new Node(val); add.next = cur; this.head = add; size++; return; } for (i = 1; isize){ return null; } for (;k>0;k--){ fast=fast.next; } for (;fast != null;){ fast=fast.next; slow=slow.next; } return slow; } public void sort(int x){ //排序 if (head ==null){ return; } Node af = null; Node ae = null; Node bf = null; Node be = null; Node cur = this.head; for (;cur !=null;cur=cur.next){ if (cur.val < x){ if (af == null){ af = cur ; ae = cur ; }else { ae.next = cur; ae = cur; } }else { if (bf == null){ bf = cur ; be = cur ; }else { be.next = cur; be = cur; } } } be.next=null; if (af == null){ this.head = bf; return; }else this.head = af; if (bf == null){ ae.next = null; this.head = af; return; } ae.next=bf; return; } public boolean plalindrome(){ //判断回文 Node prev =midNode(); Node cur = prev.next; Node curNext = cur.next; for (;curNext!=null;){ cur.next = prev; prev = cur; cur = curNext; curNext = curNext.next; } cur.next = prev; prev = this.head; for (;prev != cur && prev.next != cur ;){ if (cur.val != prev.val){ return false; } prev=prev.next; cur = cur.next; } if (cur.val != prev.val){ return false; } return true; } //清除重复 public void clear() { Node cur = this.head; for (;cur.next!=null;){ if (cur.val == cur.next.val){ del(cur.val); } cur=cur.next; } } //创建环 public void ringC(){ Node cur = this.head.next; Node prev = cur; for (;cur.next!=null;){ cur = cur.next; } cur.next = prev; } //判断环 public boolean ring(){ Node fast = this.head; Node slow = this.head; for (;fast!=null && fast.next!=null;){ fast=fast.next.next; slow=slow.next; if (slow == fast){ break; } } if (fast == null || fast.next==null){ return false; } return true; } //判断环,并输出环的起点 public int ringB(){ Node fast = this.head; Node slow = this.head; for (;fast!=null && fast.next!=null;){ fast=fast.next.next; slow=slow.next; if (slow == fast){ break; } } if (fast == null || fast.next==null){ return -1; } fast = this.head; for (;fast != slow;){ fast = fast.next; slow = slow.next; } return slow.val; }}class Node { public int val; public Node next; public Node(int val) { //构造函数 this.val = val; }}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月21日 15时40分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
《带你装B,带你飞》pytest成魔之路4 - fixture 之大解剖
2019-03-06
互联网App应用程序测试流程及测试总结
2019-03-06
根据轨迹分析出用户家在哪
2019-03-06
PostgreSQL查询表名称及表结构
2019-03-06
是什么?评估分类器的常用概念----准确率,精确率,召回率
2019-03-06
linux中使用awk命令
2019-03-06
LAB2 内核的内存管理
2019-03-06
如何使用google搜索?
2019-03-06
Redis分布式锁的正确实现方式
2019-03-06
设计模式-抽象工厂模式
2019-03-06
MySQL Explain查看执行计划详解
2019-03-06
IntelliJ IDEA 中,项目文件右键菜单没有svn选项解决办法
2019-03-06
Spring 动态绑定多实现类实例综述
2019-03-06
IDEA 调试Java代码的两个技巧
2019-03-06
MyBatis常见面试题:#{}和${}的区别是什么?
2019-03-06
Vue 数组和对象更新,但视图未更新,背后的故事
2019-03-06
剑指Offer面试题:9.二进制中1的个数
2019-03-06
《你是在做牛做马还是在做主管》- 读书笔记
2019-03-06
ASP.NET Core on K8S学习之旅(12)Ingress
2019-03-06