diff --git a/LinkedList.java b/LinkedList.java index 7836598c18ca0b770825087dc2deff812a699ec6..96a669f8aaece84d1ad5501495df2f1e10ca18a3 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -1,8 +1,11 @@ public class LinkedList { private Node head; + + public LinkedList() { head = null; + } public void addToFront(E item) { diff --git a/Main.java b/Main.java index 057428817fa315cf419ab3ea8cc74615dbf94f39..e6e045369ce4fa9acbc574888bfbbcfaa7bf7525 100644 --- a/Main.java +++ b/Main.java @@ -5,6 +5,7 @@ class Main { ll.addToFront(2); ll.addToRear(3); ll.print(); + } } \ No newline at end of file diff --git a/Node.java b/Node.java index ed7a3524e3c608d2a479f484e240452ad34c5a51..aa460f880d93fca80837fc96651fedd78e3cf70a 100644 --- a/Node.java +++ b/Node.java @@ -2,6 +2,7 @@ public class Node { E item; Node next = null; + Node(E item) { this.item = item; } diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList$Node.class" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList$Node.class" new file mode 100644 index 0000000000000000000000000000000000000000..32b32ef4f8d689f02d6e8ade3ec55ffe94922fe9 Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList$Node.class" differ diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList.class" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList.class" new file mode 100644 index 0000000000000000000000000000000000000000..6738bffcc608bed88593ca34a585a43791e23f12 Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList.class" differ diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList$Node.class" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList$Node.class" new file mode 100644 index 0000000000000000000000000000000000000000..282d0aa9c49ef1bddf5d4aadb7bccf68f3191bdc Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList$Node.class" differ diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList.class" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList.class" new file mode 100644 index 0000000000000000000000000000000000000000..567f4ff8fa0e20d669254930bdd9118b226278e8 Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList.class" differ diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList$Node.class" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList$Node.class" new file mode 100644 index 0000000000000000000000000000000000000000..4d6b129a8b5dfe81749a8276ab69c5c0ed672392 Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList$Node.class" differ diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList.class" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList.class" new file mode 100644 index 0000000000000000000000000000000000000000..b5584eb64405318e7455d3152cac2fa041417545 Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList.class" differ diff --git "a/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList.java" "b/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList.java" new file mode 100644 index 0000000000000000000000000000000000000000..06a93b334c13d15f128f3253390750a5f7a51dfc --- /dev/null +++ "b/\344\275\234\344\270\232\345\237\272\347\241\2001/SinglyLinkedList.java" @@ -0,0 +1,165 @@ +package 作业基础1; + +public class SinglyLinkedList{ + + private class Node{ + private T value; + private Node next; + public Node(T value){ + this.value=value; + this.next=null; + }//constructor + } + + private Node head; + private Node tail; + public SinglyLinkedList(){ + this.head=null; + this.tail=null; + } + //empty list? + public boolean Empty(){ + return this.head==null; + }; + + //add to front + //两种情况:链表为空和正常 + public void PushFront(T key){ + Node node=new Node(key); + node.next=this.head; + this.head=node; + if(this.tail==null) this.tail=this.head;//原本无元素的话,需要将尾节点设置为刚插入的新节点 + } + //return front item + public T TopFront(){ + if(Empty()) throw new IndexOutOfBoundsException("linked list is empty"); + return head.value; + } + //remove front item + //两种情况:只有一个元素和正常 + public void PopFront(){ + if(Empty()) return; + this.head=this.head.next; + if(this.head==null) this.tail=null;//只有一个元素,pop后变为空链表 + } + //add to back + public void PushBack(T key){ + Node node=new Node(key); + if(this.tail==null){ + this.head=this.tail=node; + } + else{ + this.tail.next=node; + this.tail=node; + } + } + //return back item + public T TopBack(){ + if(Empty()) throw new IndexOutOfBoundsException("linked list is empty"); + return tail.value; + } + //remove back item + //三种情况:只有一个元素和两个元素和正常 + public void PopBack(){ + if(Empty()) return; + if(this.head==this.tail) this.head=this.tail=null;//只有一个元素,无法遍历到倒数第二个 + else { + Node node = this.head; + while (node.next.next != null) node = node.next;//遍历到倒数第二个元素 + this.tail = node; + node.next=null; + } + } + //is key in list? + public boolean Find(T key){ + Node node=this.head; + while(node!=null){ + if(node.value==key) return true; + node=node.next; + } + return false; + } + //remove key from list + public void Erase(T key){ + Node node=this.head; + while(node!=null){ + if(node.value==key){ + if(this.head==this.tail) { + this.head = this.tail = null;//只有一个元素 + break; + } + + else if(node==this.head) this.head=this.head.next;//删除的是头元素 + else{ + Node temp=this.head; + while(temp.next!=node) temp=temp.next;//遍历到node的前一个节点 + temp.next=node.next; + if(node==this.tail) this.tail=temp;//如果删除的是尾节点,要改变尾节点 + } + } + node=node.next; + } + } + //adds keys before node + public void AddBefore(Node node,T key){ + Node New=new Node(key); + if(node==this.head){ + New.next=this.head; + this.head=New; + }//插在头节点前不用遍历 + else{ + Node temp=this.head; + while(temp!=null){ + if(temp.next==node){ + temp.next=New; + New.next=node; + } + temp=temp.next; + } + } + } + //adds key after node + public void AddAfter(Node node,T key){ + Node New=new Node(key); + New.next=node.next; + node.next=New; + if(node==this.tail) this.tail=New;//如果插在尾节点后要变更尾节点 + } + //print the value of the whole linked list + public void Print(){ + Node node=this.head; + while(node!=null) { + System.out.print(node.value+" "); + node=node.next; + } + System.out.println(); + } + + public Node getHead(){ + return head; + } + + public Node getNode(){ + return head.next.next.next; + } + + public static void main(String[] args){ + SinglyLinkedList list= new SinglyLinkedList<>(); + for(int i=5;i>0;i--) list.PushFront(i);//test PushFront + for(int i=6;i<11;i++) list.PushBack(i);//test PushBackT + list.Print(); + System.out.println(list.TopFront()+" "+list.TopBack());//test TopFront and TopBack + list.PopFront(); + list.PopBack(); + list.Print(); + list.AddBefore(list.getHead(),11); + list.AddBefore(list.getNode(),12); + list.Print(); + list.AddAfter(list.getHead(),13); + list.AddAfter(list.getNode(),14); + list.Print(); + System.out.println(list.Find(22)+" "+list.Find(9)); + list.Erase(11); + list.Print(); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList.java" "b/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList.java" new file mode 100644 index 0000000000000000000000000000000000000000..23e3547f243390229396561f18bb166eaf7979b0 --- /dev/null +++ "b/\344\275\234\344\270\232\345\237\272\347\241\2002/SinglyLinkedList.java" @@ -0,0 +1,165 @@ +package 作业基础2; + +public class SinglyLinkedList{ + + private class Node{ + private T value; + private Node next; + public Node(T value){ + this.value=value; + this.next=null; + }//constructor + } + + private Node head; + private Node tail; + public SinglyLinkedList(){ + this.head=null; + this.tail=null; + } + //empty list? + public boolean Empty(){ + return this.head==null; + }; + + //add to front + //两种情况:链表为空和正常 + public void PushFront(T key){ + Node node=new Node(key); + node.next=this.head; + this.head=node; + if(this.tail==null) this.tail=this.head;//原本无元素的话,需要将尾节点设置为刚插入的新节点 + } + //return front item + public T TopFront(){ + if(Empty()) throw new IndexOutOfBoundsException("linked list is empty"); + return head.value; + } + //remove front item + //两种情况:只有一个元素和正常 + public void PopFront(){ + if(Empty()) return; + this.head=this.head.next; + if(this.head==null) this.tail=null;//只有一个元素,pop后变为空链表 + } + //add to back + public void PushBack(T key){ + Node node=new Node(key); + if(this.tail==null){ + this.head=this.tail=node; + } + else{ + this.tail.next=node; + this.tail=node; + } + } + //return back item + public T TopBack(){ + if(Empty()) throw new IndexOutOfBoundsException("linked list is empty"); + return tail.value; + } + //remove back item + //三种情况:只有一个元素和两个元素和正常 + public void PopBack(){ + if(Empty()) return; + if(this.head==this.tail) this.head=this.tail=null;//只有一个元素,无法遍历到倒数第二个 + else { + Node node = this.head; + while (node.next.next != null) node = node.next;//遍历到倒数第二个元素 + this.tail = node; + node.next=null; + } + } + //is key in list? + public boolean Find(T key){ + Node node=this.head; + while(node!=null){ + if(node.value==key) return true; + node=node.next; + } + return false; + } + //remove key from list + public void Erase(T key){ + Node node=this.head; + while(node!=null){ + if(node.value==key){ + if(this.head==this.tail) { + this.head = this.tail = null;//只有一个元素 + break; + } + + else if(node==this.head) this.head=this.head.next;//删除的是头元素 + else{ + Node temp=this.head; + while(temp.next!=node) temp=temp.next;//遍历到node的前一个节点 + temp.next=node.next; + if(node==this.tail) this.tail=temp;//如果删除的是尾节点,要改变尾节点 + } + } + node=node.next; + } + } + //adds keys before node + public void AddBefore(Node node,T key){ + Node New=new Node(key); + if(node==this.head){ + New.next=this.head; + this.head=New; + }//插在头节点前不用遍历 + else{ + Node temp=this.head; + while(temp!=null){ + if(temp.next==node){ + temp.next=New; + New.next=node; + } + temp=temp.next; + } + } + } + //adds key after node + public void AddAfter(Node node,T key){ + Node New=new Node(key); + New.next=node.next; + node.next=New; + if(node==this.tail) this.tail=New;//如果插在尾节点后要变更尾节点 + } + //print the value of the whole linked list + public void Print(){ + Node node=this.head; + while(node!=null) { + System.out.print(node.value+" "); + node=node.next; + } + System.out.println(); + } + + public Node getHead(){ + return head; + } + + public Node getNode(){ + return head.next.next.next; + } + + public static void main(String[] args){ + SinglyLinkedList list= new SinglyLinkedList<>(); + for(int i=5;i>0;i--) list.PushFront(i);//test PushFront + for(int i=6;i<11;i++) list.PushBack(i);//test PushBackT + list.Print(); + System.out.println(list.TopFront()+" "+list.TopBack());//test TopFront and TopBack + list.PopFront(); + list.PopBack(); + list.Print(); + list.AddBefore(list.getHead(),11); + list.AddBefore(list.getNode(),12); + list.Print(); + list.AddAfter(list.getHead(),13); + list.AddAfter(list.getNode(),14); + list.Print(); + System.out.println(list.Find(22)+" "+list.Find(9)); + list.Erase(11); + list.Print(); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList.java" "b/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList.java" new file mode 100644 index 0000000000000000000000000000000000000000..abdcf921856348a3d58ee32152ab337c7816f376 --- /dev/null +++ "b/\344\275\234\344\270\232\345\237\272\347\241\2003/DoublyLinkedList.java" @@ -0,0 +1,169 @@ +package 作业基础3; + +public class DoublyLinkedList { + private class Node{ + private T value; + private Node next; + private Node prev; + public Node(T value){ + this.value=value; + this.next=null; + this.prev=null; + } + } + + private Node head; + private Node tail; + public DoublyLinkedList(){ + this.head=null; + this.tail=null; + } + //empty list? + public boolean Empty(){ + return this.head==null; + } + //add to front + //两种情况:链表为空和正常 + public void PushFront(T key){ + Node node=new Node(key);//node.prev=null; + node.next=this.head; + this.head=node; + if(this.tail==null) this.tail=this.head;//原本无元素的话,需要将尾节点设置为刚插入的新节点 + else this.head.next.prev=node;//原本有元素的话设置原head的prev节点 + } + //return front item + public T TopFront(){ + if(Empty()) throw new IndexOutOfBoundsException("linked list is empty"); + return head.value; + } + //remove front item + //两种情况:只有一个元素和正常 + public void PopFront(){ + if(Empty()) return; + if(this.head!=this.tail) this.head.next.prev=null;//两个元素及以上要设置prev节点 + this.head=this.head.next; + if(this.head==null) this.tail=null;//只有一个元素,pop后变为空链表 + } + //add to back + public void PushBack(T key){ + Node node=new Node(key); + if(this.tail==null){ + this.head=this.tail=node; + } + else{ + this.tail.next=node; + node.prev=this.tail; + this.tail=node; + } + } + //return back item + public T TopBack(){ + if(Empty()) throw new IndexOutOfBoundsException("linked list is empty"); + return tail.value; + } + //remove back item + //拥有prev节点后无须从头遍历 + public void PopBack(){ + if(Empty()) return; + if(this.head==this.tail) this.head=this.tail=null; + else{ + this.tail=this.tail.prev; + this.tail.next=null; + } + } + //is key in list? + public boolean Find(T key){ + Node node=this.head; + while(node!=null){ + if(node.value==key) return true; + node=node.next; + } + return false; + } + //remove key from list + public void Erase(T key){ + Node node=this.head; + while(node!=null){ + if(node.value==key){ + if(this.head==this.tail){ + this.head=this.tail=null;//只有一个元素 + break; + } + else if(node==this.head) { + this.head=this.head.next;//删除的是头元素 + this.head.prev=null; + } + else{ + node.prev.next=node.next; + if(node==this.tail) this.tail=node.prev;//如果删除的是尾节点,要改变尾节点 + else node.next.prev=node.prev; //非尾节点要改变被删节点的下一个节点的prev + } + } + node=node.next; + } + } + //adds keys before node + //拥有prev节点后不用遍历 + public void AddBefore(Node node, T key){ + Node New=new Node(key); + if(node==this.head){ + New.next=this.head; + this.head.prev=New; + this.head=New; + } + else{ + node.prev.next=New;//改变被插入节点前一个节点的next节点 + New.prev=node.prev;//设置插入节点的prev节点 + New.next=node; + node.prev=New; + } + } + //adds key after node + public void AddAfter(Node node, T key){ + Node New=new Node(key); + New.next=node.next; + New.prev=node; + node.next=New; + if(node==this.tail) this.tail=New;//如果插在尾节点后要变更尾节点 + else node.next.prev=New;//插入的不在尾节点后,就需要要改变插入位置后一个节点的prev节点 + } + //print the value of the whole linked list + public void Print(){ + Node node=this.head; + while(node!=null) { + System.out.print(node.value+" "); + node=node.next; + } + System.out.println(); + } + + public Node getHead(){ + return head; + } + + public Node getNode(){ + return head.next.next.next; + } + + public static void main(String[] args){ + DoublyLinkedList list= new DoublyLinkedList<>(); + for(int i=5;i>0;i--) list.PushFront(i);//test PushFront + for(int i=6;i<11;i++) list.PushBack(i);//test PushBackT + list.Print(); + System.out.println(list.TopFront()+" "+list.TopBack());//test TopFront and TopBack + list.PopFront(); + list.PopBack(); + list.Print(); + list.AddBefore(list.getHead(),11); + list.AddBefore(list.getNode(),12); + list.Print(); + list.AddAfter(list.getHead(),13); + list.AddAfter(list.getNode(),14); + list.Print(); + System.out.println(list.Find(22)+" "+list.Find(9)); + list.Erase(11); + list.Erase(9); + list.Erase(13); + list.Print(); + } +}