diff --git a/group14/857999411/RemoteSystemsTempFiles/.project b/group14/857999411/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group14/857999411/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group14/857999411/ThirdHomework/.classpath b/group14/857999411/ThirdHomework/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group14/857999411/ThirdHomework/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group14/857999411/ThirdHomework/.gitignore b/group14/857999411/ThirdHomework/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group14/857999411/ThirdHomework/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group14/857999411/ThirdHomework/.project b/group14/857999411/ThirdHomework/.project new file mode 100644 index 0000000000..97d4071a84 --- /dev/null +++ b/group14/857999411/ThirdHomework/.project @@ -0,0 +1,17 @@ + + + ThirdHomework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/857999411/ThirdHomework/.settings/org.eclipse.jdt.core.prefs b/group14/857999411/ThirdHomework/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group14/857999411/ThirdHomework/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group14/857999411/ThirdHomework/src/com/coding/basic/List.java b/group14/857999411/ThirdHomework/src/com/coding/basic/List.java new file mode 100644 index 0000000000..c86b745572 --- /dev/null +++ b/group14/857999411/ThirdHomework/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group14/857999411/ThirdHomework/src/com/coding/basic/MyLinkedList.java b/group14/857999411/ThirdHomework/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..a7aa4c9afd --- /dev/null +++ b/group14/857999411/ThirdHomework/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,288 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + + +public class MyLinkedList implements List { + + //用内部类定义链表中的节点 + private class Node{ + //节点中包含数据和引用 + Object data; + Node next; + + + //每个节点包含数据和引 + public Node (Object data,Node next){ + this.data =data; + this.next =next; + } + } + //定义头节点和尾节 + public Node head; + public Node tail; + public int size; + + //无参数构造函数创建空链表 + public MyLinkedList(){ + head =null; + tail =null; + } + + //链表中传入元 + public MyLinkedList(Object element){ + head.data =element; + head.next =tail; + size++; + } + + public void add(Object o){ + addLast(o); + } + public void addFirst(Object element) { + + head =new Node(element,head); + if(tail == null){ + tail=head; + } + size++; + } + + public void addLast(Object element) { + if(head == null) { + head =new Node (element,null); + tail =head; + }else{ + Node newNode =new Node(element,null); + tail.next =newNode; + tail=newNode; + } + size++; + + } + + public void add(int index,Object element){ + + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + if(index == 0) { + head =new Node(element,head); + } + Node frontNode =getNode(index-1); + frontNode.next =new Node(element,frontNode.next); + size++; + } + public Node getNode(int index) + { + if(index < 0 || index > size-1) { + + throw new IndexOutOfBoundsException("索引越界"); + } + Node current=head; + for(int i=0;i < size; i++,current =current.next) { + if(i == index) { + return current; + } + } + return null; + } + public int indexOf(Object o){ + + for(int i=0; i size-1) { + throw new IndexOutOfBoundsException("索引越界"); + } + Node delNode =null; + if(index == 0) { + delNode =head; + head =head.next; + }else{ + Node frontNode =getNode(index-1); + delNode =frontNode.next; + frontNode.next =delNode.next; + delNode.next =null; + } + size--; + return delNode.data; + } + + public Object removeFirst(){ + if(head == null || head.next == null) + throw new NoSuchElementException(); + Node oldhead =head; + head =head.next; + oldhead.next =null; + size--; + return oldhead.data; + + } + + public Object removeLast(){ + return remove(size - 1); + + } + + + public int size() { + return size; + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + * @return + */ + public void reverse(){ + Node tail =null,a,b; + a = head; + while(a!= null){ + b=a.next; + a.next=tail; + tail =a; + a=b; + } + head=tail; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + int len =size; + + for(int i=0;i=0 && i+length <= size){ + int len=0; + if(i == 0){ + len =i+length; + }else{ + len =i+length-1; + } + for(int j=1; j <=len; j++){ + remove(i); + } + }else{ + System.out.println("参数错误"); + } + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(MyLinkedList list){ + + int [] array =new int [list.size]; + + for(int j=0;j101->201->301->401->501->601->701 + ml.add(11); + ml.add(101); + ml.add(201); + ml.add(301); + ml.add(401); + ml.add(501); + ml.add(601); + ml.add(701); + + MyLinkedList list=new MyLinkedList(); + //1->3->4->6 + list.add(1); + list.add(3); + list.add(4); + list.add(6); + + int[] elements = ml.getElements(list); + + for (int i : elements) { + System.out.println(i); + } + } + @Test + public void subtractTest(){ + MyLinkedList ml= new MyLinkedList(); + //11->101->201->301->401->501->601->701 + ml.add(11); + ml.add(101); + ml.add(201); + ml.add(301); + ml.add(401); + ml.add(501); + ml.add(601); + ml.add(701); + + MyLinkedList list=new MyLinkedList(); + //1->3->4->6 + list.add(1); + list.add(3); + list.add(4); + list.add(6); + + ml.subtract(list); + for(int i=0;i