双向链表(Java实现)

package doublelinklist;

public class Node {
    int item;
    Node pre;
    Node next;

    public Node(int item) {
        this.item = item;
        this.pre = null;
        this.next = null;
    }
}
package doublelinklist;

public class DoubleLinkList {
    Node head;

    public DoubleLinkList() {

    }

    public DoubleLinkList(Node head) {
        this.head = head;
    }

    // 链表是否为空
    public static boolean is_empty(Node head) {
        return head == null ? true : false;
    }

    // 链表长度
    public static int length(Node head) {
        if (is_empty(head)) {
            return 0;
        }
        int length = 1;
        Node cur = head;
        while (cur.next != null) {
            length++;
            cur = cur.next;
        }
        return length;
    }

    // 遍历整个链表
    public static void travel(Node head) {
        Node cur = head;
        System.out.println("链表遍历开始!!");
        while (cur != null) {
            System.out.print(cur.item + " ");
            cur = cur.next;
        }
        System.out.println("\n链表遍历结束!!");
    }

    // 链表头部添加元素
    public static void add(DoubleLinkList list, int item) {
        Node node = new Node(item);
        if (is_empty(list.head)) {
            list.head = node;
        } else {
            node.next = list.head;
            list.head.pre = node;
            list.head = node;
        }
    }

    // 链表尾部添加元素
    public static void append(DoubleLinkList list, int item) {
        Node node = new Node(item);
        if (is_empty(list.head)) {
            list.head = node;
        } else {
            Node cur = list.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
            node.pre = cur;
        }
    }

    // 指定位置添加元素
    public static void insert(DoubleLinkList list, int pos, int item) {
        if (pos <= 0) {
            add(list, item);
        } else if (pos >= length(list.head)) {
            append(list, item);
        } else {
            Node node = new Node(item);
            Node pre = list.head;
            for (int i = 0; i < pos - 1; i++) {
                pre = pre.next;
            }
            node.pre = pre;
            node.next = pre.next;
            pre.next.pre = node;
            pre.next = node;
        }
    }

    // 删除节点
    public static void remove(DoubleLinkList list, int item) {
        Node cur = list.head;
        while (cur != null) {
            if (cur.item == item) {
                // 头结点的删除
                if (cur == list.head) {
                    list.head = cur.next;
                    if (cur.next != null) {
                        cur.next.pre = null;
                    }
                } else {
                    // 中间结点和尾结点的删除
                    cur.pre.next = cur.next;
                    if(cur.next!=null){
                        cur.next.pre = cur.pre;
                    }
                }
                break;
            } else {
                cur = cur.next;
            }
        }
    }

    // 查找节点是否存在
    public static boolean search(Node head, int item) {
        Node cur = head;
        while (cur != null) {
            if (cur.item == item) {
                return true;
            } else {
                cur = cur.next;
            }
        }
        return false;
    }
}
package doublelinklist;

public class DoubleLinkListTest {
    public static void main(String[] args) {
        Node head = new Node(100);
        DoubleLinkList list = new DoubleLinkList(head);
        if (DoubleLinkList.is_empty(list.head)) {
            System.out.println("链表为空,添加元素麻利的~");
            System.out
                    .println("************************************************");
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);

            System.out
                    .println("************************************************");
            System.out.println("头部插入节点");
            DoubleLinkList.add(list, 10);
            DoubleLinkList.add(list, 20);
            DoubleLinkList.add(list, 30);
            DoubleLinkList.add(list, 15);
            DoubleLinkList.add(list, 25);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);

            System.out
                    .println("************************************************");
            System.out.println("尾部插入几个节点");
            DoubleLinkList.append(list, 100);
            DoubleLinkList.append(list, 200);
            DoubleLinkList.append(list, 500);
            DoubleLinkList.append(list, 400);
            DoubleLinkList.append(list, 300);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);

            System.out
                    .println("************************************************");
            DoubleLinkList.insert(list, 1, 77);
            DoubleLinkList.insert(list, 0, 88);
            DoubleLinkList.insert(list, DoubleLinkList.length(list.head) - 1,
                    66);
            DoubleLinkList.insert(list, DoubleLinkList.length(list.head), 55);
            DoubleLinkList.insert(list, 5, 44);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);
            
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 88));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 55));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 300));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 100));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 5));

            System.out
                    .println("************************************************");
            System.out.println("删除几个节点试试....");
            DoubleLinkList.remove(list, 88);
            DoubleLinkList.remove(list, 55);
            DoubleLinkList.remove(list, 300);
            DoubleLinkList.remove(list, 44);
            DoubleLinkList.remove(list, 25);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);
        } else {
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);

            System.out
                    .println("************************************************");
            System.out.println("头部插入节点");
            DoubleLinkList.add(list, 10);
            DoubleLinkList.add(list, 20);
            DoubleLinkList.add(list, 30);
            DoubleLinkList.add(list, 15);
            DoubleLinkList.add(list, 25);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);

            System.out
                    .println("************************************************");
            System.out.println("尾部插入几个节点");
            DoubleLinkList.append(list, 100);
            DoubleLinkList.append(list, 200);
            DoubleLinkList.append(list, 500);
            DoubleLinkList.append(list, 400);
            DoubleLinkList.append(list, 300);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);

            System.out
                    .println("************************************************");
            DoubleLinkList.insert(list, 1, 77);
            DoubleLinkList.insert(list, 0, 88);
            DoubleLinkList.insert(list, DoubleLinkList.length(list.head) - 1,
                    66);
            DoubleLinkList.insert(list, DoubleLinkList.length(list.head), 55);
            DoubleLinkList.insert(list, 5, 44);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);
            
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 88));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 55));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 300));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 100));
            System.out.println("节点搜索:" + DoubleLinkList.search(list.head, 5));

            System.out
                    .println("************************************************");
            System.out.println("删除几个节点试试....");
            DoubleLinkList.remove(list, 88);
            DoubleLinkList.remove(list, 55);
            DoubleLinkList.remove(list, 300);
            DoubleLinkList.remove(list, 44);
            DoubleLinkList.remove(list, 25);
            System.out.println("链表的长度为:" + DoubleLinkList.length(list.head));
            DoubleLinkList.travel(list.head);
            System.out.println(DoubleLinkList.search(list.head, 15));
        }
    }
}

 

posted @ 2018-11-12 17:06  人工智能之路上的菜鸡  阅读(355)  评论(0编辑  收藏  举报