高分求JAVA高手解惑

2024-12-18 05:27:04
推荐回答(5个)
回答1:

import java.io.*;
public class LinkList implements List{

Node head;
int length;

LinkList(){
head = new Node(null);
length = 0;
}

public void insert(int i,Element elem) throws Exception{
if (i<1 || i>length+1)
throw new Exception("参数错误,无法执行插入操作!");

Node current = index(i-1);

current.setNext(new Node(elem,current.getNext()));
length++;

}

public Node index(int i) throws Exception{
if (i<0 || i>length+1)
throw new Exception("参数错误,无法执行插入操作!");
Node current = head;
int j=0;
while(j current = current.getNext();
j++;
}
return current;
}
public Element delete(int i) throws Exception{
if (i<1 || i>length)
throw new Exception("参数错误,无法执行删除操作!");

Node current = index(i-1);
Element elem = current.getNext().getElement();

current.setNext(current.getNext().getNext());
length--;
return elem;

}

public Element getElem(int i) throws Exception{
if (i<1 || i>length)
throw new Exception("参数错误,无法执行取元素操作!");
Node current = index(i);
Element elem = current.getElement();
return elem;
}

public int size(){
return length;
}

public boolean isEmpty(){
return length==0;
}

public int locate(Element elem){
Node current = head.getNext();
int j=1;
while(current!=null){
if (current.getElement().Compare(elem)==0)
return j;
else{
j++;
current = current.getNext();
}
}
return 0;
}

public void clear(){
head.setNext(null);
length=0;

}

public void print(){
Node current=head.getNext();
Element elem;
while(current!=null){
elem = current.getElement();
elem.printElem();
current = current.getNext();
}
}
}

class Node{
Element data;
Node next;

Node(Node next){
this.next = next;
}

Node(Element data,Node next){
this.data = data;
this.next = next;
}

public Node getNext(){
return next;
}

public void setNext(Node next){
this.next = next;
}

public Element getElement(){
return this.data;
}

public void setElement(Element elem){
this.data = elem;
}
}

回答2:

你可以看LinkedList的源代码

回答3:

先看看理论吧,理论+实践就ok。
光看程序也没有用的,至少要有基础。

回答4:

懂得C学JAVA就不难了,我也是只会C后来才学的JAVA,入门很简单的

回答5:

JAVA里没有指针