インポートリストを使用せずにすべての処理を行うJavaプログラム(LinkedListクラス)を作成する必要がありますが、試してみましたが、動作しているかどうかはわかりません。LinkedList getFirstElementとgetLastElementのメソッド
誰かお手伝いできますか?特にgetFirstElementメソッドとgetLastElementメソッドここで
は私のクラスです:getFirst
で
package main.java.a3;
public interface List<E> {
public void add(E e);
public void add(int index, E e);
public int size();
public E get(int index);
public boolean isEmpty();
}
package main.java.a3;
import java.util.NoSuchElementException;
public class LinkedList<E> implements List<E>{
private ListNode head;
@Override
public void add(E e) {
if(e == null){
throw new NullPointerException("Element was null");
}
if(head == null){
head = new ListNode(e,null);
}else{
ListNode temp = head;
while(temp.next!=null){
temp=temp.next;
}
temp.setNext(new ListNode(e,null));
}
}
@Override
public void add(int index, E e) {
if(e == null) {
throw new NullPointerException("Element was null!");
}
else if(index<0){
throw new IndexOutOfBoundsException("Index was negative");
}
else if(index>=size() + 1){
throw new IndexOutOfBoundsException("Index was bigger than size");
} else {
ListNode temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.setNext(new ListNode(e, null));
}
}
@Override
public int size() {
int size = 0;
ListNode temp = head;
while(temp != null) {
size++;
temp = temp.getNext();
}
return size;
}
@Override
public E get(int index) {
if(index<0){
throw new IndexOutOfBoundsException("Index was negative");
}
if(index>=size()){
throw new IndexOutOfBoundsException("Index was bigger than size");
}
ListNode temp = head;
for (int i = 0; i<index;i++){
temp = temp.next;
}
return temp.data;
}
@Override
public boolean isEmpty() {
if(head == null) {
return true;
} else {
return false;
}
}
// innere Klasse
private class ListNode{
E data;
ListNode next;
public ListNode(E data, ListNode next){
setData(data);
setNext(next);
}
public void setData(E data){
this.data = data;
}
public void setNext(ListNode next){
this.next = next;
}
public E getData() {
return data;
}
public ListNode getNext() {
return next;
}
}
// innere Klasse
public String toString() {
return head.toString();
}
public void addFirst(E elem) {
if(elem == null) {
throw new NullPointerException("Element was null!");
} else {
ListNode temp = new ListNode(elem, head);
if(head != null) {
temp.setNext(head);
}
head = temp;
}
}
public void addLast(E elem) {
if(elem == null) {
throw new NullPointerException("Element was null!");
} else {
ListNode tail = new ListNode(elem, null);
while(head != null) {
tail.getNext();
if(tail.getNext() == null) {
tail.setNext(head);
}
}
}
}
public E getFirst() {
if(head == null) {
throw new NoSuchElementException("Element was null!");
}else {
return (E) head;
}
}
public E getLast(){
E elem = null;
ListNode tail = new ListNode(elem, null);
if(tail == null) {
throw new NoSuchElementException("Element was null!");
}
return (E) tail;
}
}
getFirstElementは単に 'head.getData()' – MrKickkiller
getLastElementある 'IF(head.getNext()== NULL)が返さなければならない{戻りデータ;}そうでなければ{(getNextをを返します).getLastElement();} 'next()。getLastElementは再帰呼び出しです。 – MrKickkiller
getFirstメソッドは機能していますが、getLastは機能しません。とにかくありがとう。 – meert