diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..e208459b8afde5f7980720efd6bbb97f7ae24541 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..a9a967ebf636c25d481adbb480e9fb8fb3f25eb1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LinkList1.java b/LinkList1.java new file mode 100644 index 0000000000000000000000000000000000000000..c5087c317646d04d75adedd6568a8c86506cd7e2 --- /dev/null +++ b/LinkList1.java @@ -0,0 +1,86 @@ +package com.company; + +public class LinkList1 { + public MyNode headnode; + + public LinkList1() { + this.headnode = null; + } + + public void addToFront(E value) { + if (headnode == null) { + this.headnode = new MyNode(value); + } else { + MyNode step = headnode; + step.next = headnode.next; + this.headnode = new MyNode(value); + this.headnode.next = step; + } + } + + public void addToRear(E value) { + if (headnode == null) { + this.headnode = new MyNode(value); + } else { + MyNode node = headnode; + while (node.next != null) { + node = node.next; + } + node.next = new MyNode(value); + } + } + + public void printItems() { + MyNode node = this.headnode; + if (node == null) { + System.out.println("soory,No String"); + } else { + while (node != null) { + System.out.printf(node.item + "->"); + node = node.next; + } + } + } + + public void deleteItems(String value) { + MyNode node = headnode; + if (node != null) { + if (node.item.equals(value)) { + headnode = node.next; + return; + } else { + while (node.next != null) { + if (node.next.item.equals(value)) { + node.next = node.next.next; + return; + } + node = node.next; + } + } + } + } + + public boolean findItem(String value) { + MyNode node = headnode; + while (node != null) { + if (node.item.equals(value)) { + return true; + } else { + node = node.next; + } + } + return false; + } + + public void size() { + int num = 0; + MyNode node = headnode; + while (node != null) { + num++; + node = node.next; + } + System.out.println("链表长度为:" + num); + } +} + + diff --git a/LinkList2.java b/LinkList2.java new file mode 100644 index 0000000000000000000000000000000000000000..72eee86046021ce872c7d30c2c9a4964e7eb1bfb --- /dev/null +++ b/LinkList2.java @@ -0,0 +1,101 @@ +package com.company; + +public class LinkList2 { + public MyNode head; + public MyNode tail; + + + public LinkList2() { + this.head = null; + this.tail = null; + + } + + public void addToFront(E value) { + if (head == null) { + this.head = new MyNode(value); + this.tail = head; + } else { + MyNode step = head; + step.next = head.next; + this.head = new MyNode(value); + this.head.next = step; + } + } + + public void addToRear(E value) { + if (head == null) { + this.head = new MyNode(value); + this.tail = head; + } else { + /*第一种方法*/ +// MyNode node = head; +// while (node.next != null) { +// node = node.next; +// } +// node.next = new MyNode(value); +// tail = node.next; + /*第二种方法*/ + MyNode node = this.tail; + node.next = new MyNode(value); + tail = node.next; + } + } + + public void printItems() { + MyNode node = this.head; + if (node == null) { + System.out.println("soory,No String"); + } else { + while (node != null) { + System.out.printf(node.item + "->"); + node = node.next; + } + } + } + + public void deleteItems(String value) { + MyNode node = head; + if (node != null) { + if (node.item.equals(value)) { + head = node.next; + return; + } else { + while (node.next != null) { + if (node.next.item.equals(value)) { + if (node.next.next == null) { + this.tail = node; + } + node.next = node.next.next; + return; + } + node = node.next; + } + } + } + } + + public boolean findItem(String value) { + MyNode node = head; + while (node != null) { + if (node.item.equals(value)) { + return true; + } else { + node = node.next; + } + } + return false; + } + + public void size() { + int num = 0; + MyNode node = head; + while (node != null) { + num++; + node = node.next; + } + System.out.println("链表长度为:" + num); + } +} + + diff --git a/LinkList3.java b/LinkList3.java new file mode 100644 index 0000000000000000000000000000000000000000..512000eda2744f0fd037da6bd0b64bb8c9acc1ab --- /dev/null +++ b/LinkList3.java @@ -0,0 +1,102 @@ +package com.company; + +public class LinkList3 { + public MyNode headnode; + + public LinkList3() { + this.headnode = null; + } + + public void addToFront(E value) { + if (headnode == null) { + this.headnode = new MyNode(value); + headnode.prev = null; + headnode.next = null; + } else { + MyNode temp = headnode; + MyNode newnode = new MyNode(value); + temp.prev = newnode; + newnode.next = temp; + headnode = newnode; + } + } + + public void addToRear(E value) { + MyNode newnode = new MyNode(value); + if (headnode == null) { + this.headnode = new MyNode(value); + headnode.next = null; + headnode.prev = null; + } else { + MyNode node = headnode; + while (node.next != null) { + node = node.next; + } + node.next = newnode; + node.next.prev = node; + } + } + + public void printItems() { + MyNode node = this.headnode; + if (node == null) { + System.out.println("soory,No String"); + } else { + while (node != null) { + System.out.printf(node.item + "->"); + node = node.next; + } + } + } + + public void deleteItems(String value) { + MyNode node = headnode; + if (node != null) { + if (node.item.equals(value)) { + node = node.next; + node.prev = null; + headnode = node; + return; + } + node = node.next; + while (node != null) { + if (node.item.equals(value) && node.next != null) { + MyNode myNode = node.next; + myNode.prev = node.prev; + node.prev.next = myNode; + return; + } else if (node.item.equals(value) && node.next == null) { + node.prev.next = null; + return; + } else { + node = node.next; + } + } + System.out.println("元素不在链表中"); + } + } + + public boolean findItem(String value) { + MyNode node = headnode; + while (node != null) { + if (node.item.equals(value)) { + return true; + } else { + node = node.next; + } + } + return false; + } + + public void size() { + int num = 0; + MyNode node = headnode; + while (node != null) { + num++; + node = node.next; + } + System.out.println("链表长度为:" + num); + } +} + + diff --git a/MyMain.java b/MyMain.java new file mode 100644 index 0000000000000000000000000000000000000000..1726740c8f9cf99d199781dc4cda620bb17442ef --- /dev/null +++ b/MyMain.java @@ -0,0 +1,40 @@ +package com.company; + + +public class MyMain { + public static void main(String[] args) { + /*作业1*/ + LinkList1 lIst1Link = new LinkList1<>(); + lIst1Link.addToRear("a"); + lIst1Link.addToRear("b"); + lIst1Link.addToFront("c"); + lIst1Link.addToRear("d"); + lIst1Link.deleteItems("d"); + System.out.println(lIst1Link.findItem("b")); + lIst1Link.size(); + lIst1Link.printItems(); + + /*作业2*/ + LinkList2 linkList2 = new LinkList2<>(); + linkList2.addToRear("c"); + linkList2.addToFront("a"); + linkList2.addToFront("b"); + linkList2.printItems(); + linkList2.deleteItems("c"); + linkList2.size(); + System.out.println(linkList2.findItem("a")); + + /*作业3*/ + LinkList3 linkList3 = new LinkList3(); + linkList3.addToFront("b"); + linkList3.addToFront("a"); + linkList3.addToRear("d"); + linkList3.addToRear("p"); + linkList3.printItems(); + System.out.println('\n'); + linkList3.deleteItems("d"); + System.out.println(linkList3.findItem("2")); + linkList3.printItems(); + linkList3.size(); + } +} diff --git a/MyNode.java b/MyNode.java new file mode 100644 index 0000000000000000000000000000000000000000..ef6948cae6ef6d5e3c19cf71546915a182f13481 --- /dev/null +++ b/MyNode.java @@ -0,0 +1,10 @@ +package com.company; + +public class MyNode { + E item; + MyNode next=null; + public MyNode prev; + public MyNode(E item) { + this.item = item; + } +} diff --git a/Node.java b/Node.java deleted file mode 100644 index ed7a3524e3c608d2a479f484e240452ad34c5a51..0000000000000000000000000000000000000000 --- 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 diff --git a/lec02-linkedlist.iml b/lec02-linkedlist.iml new file mode 100644 index 0000000000000000000000000000000000000000..b107a2dd81165eaaf682ad3da030668b937fbb6c --- /dev/null +++ b/lec02-linkedlist.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file