From 5441497ca6c34f42cb32acaf9c9b9a0f37864cc1 Mon Sep 17 00:00:00 2001 From: austin_ht Date: Wed, 4 Nov 2020 00:30:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LinkedList.java | 44 ----------------- Main.java | 123 ++++++++++++++++++++++++++++++++++++++++++++---- Node.java | 8 ---- 3 files changed, 114 insertions(+), 61 deletions(-) delete mode 100644 LinkedList.java delete mode 100644 Node.java diff --git a/LinkedList.java b/LinkedList.java deleted file mode 100644 index 7836598..0000000 --- a/LinkedList.java +++ /dev/null @@ -1,44 +0,0 @@ -public class LinkedList { - private Node head; - - public LinkedList() { - head = null; - } - - public void addToFront(E item) { - Node node = new Node(item); - - if (this.head == null) { - this.head = node; - } else{ - Node temp = head; - this.head = node; - this.head.next = temp; - } - } - - - public void addToRear(E item) { - Node newNode = new Node(item); - if (this.head == null) { - this.head = newNode; - } else { - - Node temp = this.head; - while(temp.next != null){ - temp = temp.next; - } - temp.next = newNode; - } - } - - public void print() { - Node temp = this.head; - while(temp.next != null){ - System.out.print(temp.item + " -> "); - temp = temp.next; - } - System.out.println(temp.item); - } -} - diff --git a/Main.java b/Main.java index 0574288..c5454d1 100644 --- a/Main.java +++ b/Main.java @@ -1,10 +1,115 @@ -class Main { - public static void main(String[] args) { - LinkedList ll = new LinkedList(); - ll.addToFront(1); - ll.addToFront(2); - ll.addToRear(3); - ll.print(); - - } +class LinkedList { + private Node head; + + public LinkedList() { + head = null; + } + + public void addToFront(E item) { + Node node = new Node(item); + + if (this.head == null) { + this.head = node; + } else { + Node temp = head; + this.head = node; + this.head.next = temp; + } + } + + + public void addToRear(E item) { + Node newNode = new Node(item); + if (this.head == null) { + this.head = newNode; + } else { + + Node temp = this.head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = newNode; + } + } + + public void printItems() { + Node temp = this.head; + while (temp.next != null) { + System.out.print(temp.item + " -> "); + temp = temp.next; + } + System.out.println(temp.item); + } + + public boolean deleteItems(int a) { + if (a < 1 || a > length()) { + return false; + } + if (a == 1) { + head = head.next; + return true; + } + int i = 1; + Node preNode = head; + Node curNode = preNode.next; + while (curNode != null) { + if (i == a) { + preNode.next = curNode.next; + return true; + } + preNode = curNode; + curNode = curNode.next; + i++; + } + return false; + } + + //链表长度 + public int length() { + int length = 0; + Node tmp = head; + while (tmp != null) { + length++; + tmp = tmp.next; + } + return length; + } + + + + //计算链表长度数值 + public int size() { + int linkedListLength = 0; + Node temp = head; + while (temp != null) { + linkedListLength++; + temp = temp.next; + } + return linkedListLength; + } + + public class Node { + E item; + Node next = null; + + Node(E item) { + this.item = item; + } + } + + + public static void main(String[] args) { + + LinkedList ll = new LinkedList(); + ll.addToFront(2); + ll.addToFront(1); + ll.printItems(); + ll.addToRear(3); + ll.addToRear(4); + ll.printItems(); + System.out.println("链表长度为" + ll.size()); + ll.deleteItems(2); + System.out.println("删除节点:"); + ll.printItems(); + } } \ No newline at end of file diff --git a/Node.java b/Node.java deleted file mode 100644 index ed7a352..0000000 --- a/Node.java +++ /dev/null @@ -1,8 +0,0 @@ -public class Node { - E item; - Node next = null; - - Node(E item) { - this.item = item; - } -} \ No newline at end of file -- Gitee