diff --git a/LinkedList.class b/LinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..08dfb3839a6526798ca964da429b0884fe8bf8db Binary files /dev/null and b/LinkedList.class differ diff --git a/LinkedList.java b/LinkedList.java index 7836598c18ca0b770825087dc2deff812a699ec6..4a23a181eff3ea3e2756f54c4c5d2f512a2cc606 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -1,5 +1,7 @@ public class LinkedList { private Node head; + private int size; + public LinkedList() { head = null; @@ -15,8 +17,68 @@ public class LinkedList { this.head = node; this.head.next = temp; } + size++; + } + + + public int size(){ + return this.size; + } + + public boolean findItems(E item){ + Node temp=this.head; + if(temp==null){ + System.out.println("empty"); + return false; + } + while(temp.next!=null){ + if(temp.item.equals(item)) + { + return true; + } + temp=temp.next; + } + return false; } + public void deleteItems(E item){ + Node temp=this.head; + Node pretemp=new Node(item); + pretemp=temp; + if(temp==null){ + System.out.println("empty"); + return; + } + while(temp.next!=null){ + // System.out.println("temp"+temp.item); + // System.out.println("ll"+this.head.item); + if(temp.item.equals(item)){ + if(this.head.item.equals(item)){ + pretemp=this.head.next; + this.head.next=pretemp.next; + this.head=pretemp; + + // System.out.println("testss"+this.head.item); + break; + } + System.out.println("delete"); + pretemp.next=temp.next; + break; + }else + { + System.out.println("no delete"); + pretemp=temp; + temp=temp.next; + + } + + } + if(temp.next==null){ + temp.item=null; + temp.next=null; + } + this.size--; + } public void addToRear(E item) { Node newNode = new Node(item); diff --git a/Main.class b/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..79215ba95bf9dd09b308d1e7f27954a18f2ddf40 Binary files /dev/null and b/Main.class differ diff --git a/Main.java b/Main.java index 057428817fa315cf419ab3ea8cc74615dbf94f39..5af8ea58e5a0528ed010edfc0bb22f3581bc1ee9 100644 --- a/Main.java +++ b/Main.java @@ -4,7 +4,9 @@ class Main { ll.addToFront(1); ll.addToFront(2); ll.addToRear(3); + ll.deleteItems(1); ll.print(); - + System.out.println(ll.size()); + System.out.println(ll.findItems(2)); } } \ No newline at end of file diff --git a/Node.class b/Node.class new file mode 100644 index 0000000000000000000000000000000000000000..16fa445d06d0391dcc0131ba0a8d4fb564ff90c2 Binary files /dev/null and b/Node.class differ diff --git a/mytest/.gitignore b/mytest/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d160a7b219f7fd0ea93ad4761fc8bf451476c7f0 --- /dev/null +++ b/mytest/.gitignore @@ -0,0 +1,5 @@ +gen-branch.js +package.json +package-lock.json +node_modules +id.txt diff --git a/mytest/LinkedList.class b/mytest/LinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..38ba53ad0a821bb4ebd82d1e19fc32f51ac95491 Binary files /dev/null and b/mytest/LinkedList.class differ diff --git a/mytest/LinkedList.java b/mytest/LinkedList.java new file mode 100644 index 0000000000000000000000000000000000000000..cb838cbbb40737490c5084c4d3b3ee5989938690 --- /dev/null +++ b/mytest/LinkedList.java @@ -0,0 +1,104 @@ +public class LinkedList { + private Node head; + private Node tail; //尾指针用来标记最后一个结点的位置 + private int size;//长度用来标记长度 + 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; + node.next=temp; + head = node; + } + this.size++; + } + + public int size(){ + return this.size; + } + + public void addToRear(E item) { + Node newNode = new Node(item); + if (this.head == null) { + this.head = newNode; + } else { + this.tail.next=newNode; + } + this.tail = newNode; + this.size++; + } + + public void deleteItems(E item){ + Node temp=this.head; + Node pretemp=new Node(item); + pretemp=temp; + if(temp==null){ + System.out.println("empty"); + return; + } + while(temp.next!=null){ + // System.out.println("temp"+temp.item); + // System.out.println("ll"+this.head.item); + if(temp.item.equals(item)){ + if(this.head.item.equals(item)){ + pretemp=this.head.next; + this.head.next=pretemp.next; + this.head=pretemp; + + // System.out.println("testss"+this.head.item); + break; + } + System.out.println("delete"); + pretemp.next=temp.next; + break; + }else + { + System.out.println("no delete"); + pretemp=temp; + temp=temp.next; + + } + + } + if(temp.next==null){ + temp.item=null; + temp.next=null; + } + this.tail=temp; + // System.out.println(tail.item); + this.size--; + } + + public boolean findItems(E item){ + Node temp=this.head; + if(temp==null){ + System.out.println("empty"); + return false; + } + while(temp.next!=null){ + if(temp.item.equals(item)) + { + return true; + } + temp=temp.next; + } + return false; + } + + public void printItems() { + Node temp = this.head; + // System.out.println("testss"+temp.item); + while(temp.next != null){ + System.out.print(temp.item + " -> "); + temp = temp.next; + } + System.out.println(temp.item); + } +} + diff --git a/mytest/Main.class b/mytest/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..f8ea09f314de6c3c1774fc67b0933aa59ab96b19 Binary files /dev/null and b/mytest/Main.class differ diff --git a/mytest/Main.java b/mytest/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..2a8ed4885c902831db027ba779f740f780bf7227 --- /dev/null +++ b/mytest/Main.java @@ -0,0 +1,15 @@ +class Main { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addToRear(1); + ll.addToRear(2); + ll.addToFront(888); + ll.addToFront(999); + //ll.deleteItems(888); + ll.deleteItems(999); + ll.deleteItems(888); + ll.printItems(); + System.out.println(ll.findItems(654)); + System.out.println(ll.size()); + } +} \ No newline at end of file diff --git a/mytest/Node.class b/mytest/Node.class new file mode 100644 index 0000000000000000000000000000000000000000..16fa445d06d0391dcc0131ba0a8d4fb564ff90c2 Binary files /dev/null and b/mytest/Node.class differ diff --git a/mytest/Node.java b/mytest/Node.java new file mode 100644 index 0000000000000000000000000000000000000000..c05fdc690019d72d2a1267d3d184ec147df28c00 --- /dev/null +++ b/mytest/Node.java @@ -0,0 +1,9 @@ +public class Node { + E item; + Node next = null; + + Node(E item) { + this.item = item; + } + +} \ No newline at end of file diff --git a/mytest2/.gitignore b/mytest2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d160a7b219f7fd0ea93ad4761fc8bf451476c7f0 --- /dev/null +++ b/mytest2/.gitignore @@ -0,0 +1,5 @@ +gen-branch.js +package.json +package-lock.json +node_modules +id.txt diff --git a/mytest2/LinkedList.class b/mytest2/LinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..796117812451b230fabf7225f06918ef0783bf05 Binary files /dev/null and b/mytest2/LinkedList.class differ diff --git a/mytest2/LinkedList.java b/mytest2/LinkedList.java new file mode 100644 index 0000000000000000000000000000000000000000..a65fbe7c36b1a33efc5f4723567034871afe0e11 --- /dev/null +++ b/mytest2/LinkedList.java @@ -0,0 +1,106 @@ +public class LinkedList { + private Node head; + private Node tail; //尾指针用来标记最后一个结点的位置 + private Node first; + private int size;//长度用来标记长度 + + + public LinkedList() { + head = null; + } + + public void addToFront(E item) { + Node node = new Node(item); + + if (head == null) { + this.head = node; + this.tail=node; + } else{ + node.next=this.head; + this.head.pre=node; + this.head=node; + } + this.first=node; + this.size++; + + } + + public int size(){ + return this.size; + } + + public void addToRear(E item) { + Node node = new Node(item); + + if (this.head == null) { + this.head = node; + } else{ + this.tail.next=node; + } + this.tail=node; + this.size++; + } + + public void deleteItems(E item){ + Node temp=this.head; + if(temp==null){ + System.out.println("empty"); + return; + } + while(temp.next!=null){ + if(this.first.item.equals(item)){ + System.out.println("first"+first.item+"item"+item); + this.first=this.first.next; + this.first.pre=null; + }else + { + temp=temp.next; + if(temp.next!=this.tail){ + System.out.println("no tail"); + if(temp.item.equals(item)){ + System.out.println("temp.item"+temp.item+"item"+item); + temp.pre.next=temp.next; + } + }else + { + System.out.println("tail.item"+tail.item+"item"+item); + if(tail.item.equals(item)){ + System.out.println("okk-->is tail"+temp.item); + temp.next=null; + + } + + } + + } + } + + this.size--; + } + + public boolean findItems(E item){ + Node temp=this.head; + if(temp==null){ + System.out.println("empty"); + return false; + } + while(temp!=null){ + if(temp.item.equals(item)) + { + return true; + } + temp=temp.next; + } + return false; + } + + public void printItems() { + Node temp = this.head; + while(temp.next != null){ + System.out.print(temp.item + " -> "); + temp = temp.next; + } + System.out.println(temp.item); + } +} + diff --git a/mytest2/Main.class b/mytest2/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..7ea76fa4e7a5ad674e3589172ecc55fb00f1b30a Binary files /dev/null and b/mytest2/Main.class differ diff --git a/mytest2/Main.java b/mytest2/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..27b4b2917cf5d135cdb26c91c7192c34decda8ab --- /dev/null +++ b/mytest2/Main.java @@ -0,0 +1,16 @@ +class Main { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addToFront(888); + ll.addToFront(999); + ll.addToFront(222); + ll.addToFront(333); + ll.addToRear(987); + //ll.deleteItems(222); + ll.deleteItems(987); + ll.printItems(); + System.out.println(ll.findItems(888)); + + System.out.println(ll.size()); + } +} \ No newline at end of file diff --git a/mytest2/Node.class b/mytest2/Node.class new file mode 100644 index 0000000000000000000000000000000000000000..fc28abd4e63e05d455d20032d4f07cff212a12dc Binary files /dev/null and b/mytest2/Node.class differ diff --git a/mytest2/Node.java b/mytest2/Node.java new file mode 100644 index 0000000000000000000000000000000000000000..3128436e9401e434428c8d55635f6df3f2a624ca --- /dev/null +++ b/mytest2/Node.java @@ -0,0 +1,10 @@ +public class Node { + E item; + public Node pre=null; + Node next = null; + + Node(E item) { + this.item = item; + } + +} \ No newline at end of file