Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions group14/857999411/RemoteSystemsTempFiles/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>RemoteSystemsTempFiles</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>org.eclipse.rse.ui.remoteSystemsTempNature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions group14/857999411/ThirdHomework/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group14/857999411/ThirdHomework/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group14/857999411/ThirdHomework/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ThirdHomework</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}
288 changes: 288 additions & 0 deletions group14/857999411/ThirdHomework/src/com/coding/basic/MyLinkedList.java
Original file line number Diff line number Diff line change
@@ -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; i++){
if(o.equals(this.get(i))){
return i;
}
}

return -1;
}
public Object get(int index){
return getNode(index).data;
}

public Object remove(int index){
if(index < 0 || index > 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<len/2;i++){
removeFirst();
}
}

/**
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
* @param i
* @param length
*/
public void remove(int i, int length){
if( 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;j<list.size;j++){
int index =(Integer) list.get(j);
if(index <= size-1){
array [j]=(Integer) get(index);
}else{
System.out.println("角标越界");
}
}
return array;
}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 从当前链表中中删除在listB中出现的元素
* @param list
*/

public void subtract(MyLinkedList list){

int len=list.size;
for(int j=0;j<len;j++){
int index =(Integer) list.get(j);
if(j != 0){
index=index-j*1;
}
if(index <= size-1){
remove(index);
}else{
System.out.println("角标越界");
}
}
}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){

for(int i=0; i<size; i++){
for(int j=i+1; j<size; j++){
if(this.get(i).equals(this.get(j))){
remove(j);
}
}
}
}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
* @param min
* @param max
*/
public void removeRange(int min, int max){

for(int i=0; i<size; i++){
int num = (Integer) this.get(i);
if(min < num && num < max){
remove(i);
i=0;
}
}
}

/**
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同)
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列
* @param list
*/
public MyLinkedList intersection( MyLinkedList list){
MyLinkedList newL=new MyLinkedList();

for(int i=0; i<size; i++){
for(int j=0; j<list.size; j++){
if(this.get(i).equals(list.get(j))){
newL.add(list.get(j));
}
}
}
return newL;
}
}
Loading