无头单向不循环链表
发布日期: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; i 
size){ 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; }}
上一篇:JAVA初窥-DAY13
下一篇:SpringMVC框架学习(十五)——服务端数据校验

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月21日 15时40分34秒