循环链表/双向链表

Java实现循环链表

/**
 * @auther: 巨未
 * @DATE: 2019/1/4 0004 20:12
 * @Description:  循环链表
 */

class ClinkDemo {
    class Entry {
        int data;
        Entry next;

        public Entry() {
            this.data = -1;
            this.next = null;
        }

        public Entry(int val) {
            this.data = val;
            this.next = null;
        }
    }

    private Entry head = null;

    public ClinkDemo() {
        this.head = new Entry();//得到头结点的引用
       this.head.next = this.head;

    }

    /*
     *头插法
     */
    public void insertHead(int val) {
        Entry entry = new Entry(val);
        entry.next = this.head.next;
        this.head.next = entry;
    }

    /*
     *尾插法
     */
    public void insertTail(int val) {
        Entry cur = this.head;
        while (cur.next != this.head) {
            cur = cur.next;
        }
        Entry entry = new Entry(val);
        cur.next = entry;
        entry.next = this.head; //让插入的next域指向头结点的地址
    }

    /*
     *删除所有值为key的节点
     */
    public void deleteAllkey(int key) {
        Entry pre = this.head;
        Entry cur = this.head.next;
        while (cur != this.head) {
            if (cur.data == key) {
                pre.next = cur.next;
                cur = cur.next;// cur = pre.next;
            } else
                pre = cur;
                cur = cur.next;
        }
    }
        /*
         *打印
         */
        public void show() {
            Entry cur = this.head.next;
            while (cur != this.head) {
                System.out.print(cur.data + " ");
                cur = cur.next;
            }
            System.out.println();
        }
}


public class TestCLinkDemo {

    public static void main(String[] args) {
      ClinkDemo clinkDemo = new ClinkDemo();
        for (int i = 0; i < 5; i++) {
            clinkDemo.insertHead(i);//头插 倒序
        }
        clinkDemo.show();
        for (int i = 0; i < 5; i++) {
            clinkDemo.insertTail(i); //尾插 正序
        }
        clinkDemo.show();
    }
}

Java实现双向链表

/**
 * @auther: 巨未
 * @DATE: 2019/1/4 0004 20:43
 * @Description: 双向链表
 */
class DlinkDemo{
    class Entry{
        int data;
        Entry prev;
        Entry next;

        public Entry() { //
            this.data = -1;
            this.prev = null;
            this.next = null;
        }

        public Entry(int val) {
            this.data = val;
            this.prev = null;
            this.next = null;
        }
    }

    private Entry head = null;

    public DlinkDemo() {
        this.head = new Entry();//得到头结点的引用
    }

    /*
     *头插法 *************第一次插入
     */
    public void insertHead(int val) {
        Entry entry = new Entry(val); //插入的节点
        entry.next = head.next;
        this.head.next = entry;
        entry.prev = this.head;
       // entry.next.prev = entry;  //java.lang.NullPointerException
         // entry.next本来就是null,空指针异常
        if(entry.next != null) {
            entry.next.prev = entry;
        }
    }
    /*
     *尾插法
     */
    public void insertTail(int val) {
     Entry cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        Entry entry = new Entry(val);
        cur.next = entry;
        entry.prev = cur;
    }
    /*
     *打印
     */
    public void show() {
        Entry cur = this.head.next;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    /*
     *删除一个值为Key的节点****************最后一个
     */
    public void deletekey(int key) {
        Entry cur = this.head.next;

        while (cur != null) {  //遍历双向链表
            if(cur.data == key) {
                cur.next.prev = cur.prev;   //删除节点的后继的前驱为 它的前驱

                if (cur.next != null) {
                    cur.prev.next = cur.next;  //删除节点的前驱的后继为 它的后继
                } cur = cur.next;
                }
            }
        }

        }

public class TestDlinkDemo {

    public static void main(String[] args) {
    DlinkDemo dlinkDemo = new DlinkDemo();
    /*    for (int i = 0; i < 5 ; i++) {
            dlinkDemo.insertHead(i);
        }
        dlinkDemo.show();*/

        for (int i = 0; i < 5 ; i++) {
            dlinkDemo.insertTail(i);
        }
        dlinkDemo.show();
        dlinkDemo.deletekey(1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值