meert meert - 12 days ago 5
Java Question

LinkedList getFirstElement and getLastElement Methods

I need to write a Java program (class LinkedList) that should do all the stuff without using import List and I have tried but I am not sure from if they are working or not.

Can someone please help me? Especially getFirstElement and getLastElement methods.

Here are my classes:

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;
}


}

Answer

getFirstElement() and getLastElement()

/* supposing the elements in your link list are stored in a variable data of the Node class */

public E getFirst() {
            if(head == null) {
                throw new NoSuchElementException("Element was null!");
            }else {
                return (E) head.data;
            }

        }

        public E getLast(){
            if(head == null) {
                throw new NoSuchElementException("Element was null!");
            }
            else{
                for(Node iterate=head; iterate!=null; iterate=iterate.next){
                    return (E) iterate.data;
                }

            }
        }
Comments