From 4d1525b9a193b798e5792788def3e05bd562bf5f Mon Sep 17 00:00:00 2001 From: wangzheng Date: Tue, 2 Oct 2018 23:42:34 +0800 Subject: [PATCH] 06_linkedlist --- java/06_linkedlist/SinglyLinkedList.java | 150 +++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 java/06_linkedlist/SinglyLinkedList.java diff --git a/java/06_linkedlist/SinglyLinkedList.java b/java/06_linkedlist/SinglyLinkedList.java new file mode 100644 index 00000000..3978c0b4 --- /dev/null +++ b/java/06_linkedlist/SinglyLinkedList.java @@ -0,0 +1,150 @@ +package linkedlist; + +/** + * 1)单链表的插入、删除、查找操作; + * 2)链表中存储的是int类型的数据; + * + * Author:Zheng + */ +public class SinglyLinkedList { + + private Node head = null; + + public Node findByValue(int value) { + Node p = head; + while (p != null && p.data != value) { + p = p.next; + } + + return p; + } + + public Node findByIndex(int index) { + Node p = head; + int pos = 0; + while (p != null && pos != index) { + p = p.next; + ++pos; + } + + return p; + } + + public void insertToHead(int value) { + Node newNode = new Node(value, null); + insertToHead(newNode); + } + + public void insertToHead(Node newNode) { + if (head == null) { + head = newNode; + } else { + newNode.next = head; + head = newNode; + } + } + + public void insertAfter(Node p, int value) { + Node newNode = new Node(value, null); + insertAfter(p, newNode); + } + + public void insertAfter(Node p, Node newNode) { + if (p == null) return; + + newNode.next = p.next; + p.next = newNode; + } + + public void insertBefore(Node p, int value) { + Node newNode = new Node(value, null); + insertBefore(p, newNode); + } + + public void insertBefore(Node p, Node newNode) { + if (p == null) return; + if (head == p) { + insertToHead(newNode); + return; + } + + Node q = head; + while (q != null && q.next != p) { + q = q.next; + } + + if (q == null) { + return; + } + + newNode.next = p; + q.next = newNode; + + } + + public void deleteByNode(Node p) { + if (p == null || head == null) return; + + if (p == head) { + head = head.next; + } + + Node q = head; + while (q != null && q.next != p) { + q = q.next; + } + + if (q == null) { + return; + } + + q.next = q.next.next; + } + + public void deleteByValue(int value) { + if (head == null) return; + + Node p = head; + Node q = null; + while (p != null && p.data != value) { + q = p; + p = p.next; + } + + if (p == null) return; + + if (q == null) { + head = head.next; + } else { + q.next = q.next.next; + } + } + + public void printAll() { + Node p = head; + while (p != null) { + System.out.print(p.data + " "); + p = p.next; + } + System.out.println(); + } + + public static Node createNode(int value) { + return new Node(value, null); + } + + public static class Node { + private int data; + private Node next; + + public Node(int data, Node next) { + this.data = data; + this.next = next; + } + + public int getData() { + return data; + } + } + +}