单向循环列表(Java实现)

package sincyclinkedlist;

public class Node {
    int item;
    Node next;

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

public class SinCycLinkedList {
    Node head;

    public SinCycLinkedList() {

    }

    public SinCycLinkedList(Node head) {
        this.head = head;
        this.head.next = 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 != head) {
            length++;
            cur = cur.next;
        }
        return length;
    }

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

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

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

    // 指定位置添加元素
    public static void insert(SinCycLinkedList 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.next = pre.next;
            pre.next = node;
        }
    }

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

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

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

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

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

            System.out
                    .println("************************************************");
            SinCycLinkedList.insert(list, 1, 77);
            SinCycLinkedList.insert(list, 0, 88);
            SinCycLinkedList.insert(list,
                    SinCycLinkedList.length(list.head) - 1, 66);
            SinCycLinkedList.insert(list, SinCycLinkedList.length(list.head),
                    55);
            SinCycLinkedList.insert(list, 5, 44);
            System.out.println("链表的长度为:" + SinCycLinkedList.length(list.head));
            SinCycLinkedList.travel(list.head);
            System.out
                    .println("************************************************");

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

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

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

            System.out
                    .println("************************************************");
            SinCycLinkedList.insert(list, 1, 77);
            SinCycLinkedList.insert(list, 0, 88);
            SinCycLinkedList.insert(list,
                    SinCycLinkedList.length(list.head) - 1, 66);
            SinCycLinkedList.insert(list, SinCycLinkedList.length(list.head),
                    55);
            SinCycLinkedList.insert(list, 5, 44);
            System.out.println("链表的长度为:" + SinCycLinkedList.length(list.head));
            SinCycLinkedList.travel(list.head);

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

 

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