Can someone please help me? in Java, please. You will need two classes: 1. Linke
ID: 3740184 • Letter: C
Question
Can someone please help me? in Java, please.
You will need two classes: 1. Linkedlist class, which will represent the full list. 2. Node class. containing a #value method and a link to the #next node . Set both as nil by default. Build the following methods in your linked list class 1. #append adds a new node to the end of the list 2. #prepend adds a new node to the start of the list 3. #size returns the total number of nodes in the list 4. #head returns the first node in the list 5. #tail returns the last node in the list 6. #at (index) returns the node at the given index 7. #pop removes the last element from the list 8. #contains? returns true if the passed in value is in the list and otherwise returns false. 9·#find (data) returns the index of the node containing data, or nil if not found. 10. #to_s represent your LinkedList objects as strings, so you can print them out and preview them in the console. The format should be: ( data ) ( data ) .> ( data ) .> nil ### Extra Credit 1. (tinsert at(index)) that inserts the node at the given index 2. #remove-at(index) that removes the node at the given index. (You will need to update the links of your nodes in the list when you remove a node.)Explanation / Answer
I have done few modification as per some requirement, go through it. Copy the Program as it is and run.
package LinkedList;
public class LinkedList<T> {
private Node<T> head;
private Node<T> tail;
int size = 0;
public void append(T element) {
Node<T> nd = new Node<T>();
nd.setValue(element);
// check linked list is empty or not
if (head == null) {
head = nd;
tail = nd;
} else {
tail.setNextNode(nd);
tail = nd;
}
size++;
}
public void prepend(T element) {
Node<T> nd = new Node<T>();
nd.setValue(element);
// check linked list is empty or not
if (head == null) {
head = nd;
tail = nd;
} else {
nd.setNextNode(head);
head = nd;
}
size++;
}
public int size() {
return size;
}
public T head() {
return head.getValue();
}
public T tail() {
return tail.getValue();
}
public T at(int index) {
Node<T> nd = head;
for (int i = 0; i < index; i++) {
nd = nd.getNextNode();
}
return nd.getValue();
}
public void pop() {
Node<T> temp = head;
if (size > 0) {
for (int i = 0; i < size - 2; i++) {
temp = temp.getNextNode();
}
temp.setNextNode(null);
tail = temp;
size--;
}
}
public boolean contains(T temp) {
boolean ndPresent = false;
Node<T> nd = head;
if (size > 0 && null != head) {
for (int i = 0; i < size; i++) {
if (nd.getValue().equals(temp)) {
return true;
}
nd = nd.getNextNode();
}
}
return ndPresent;
}
public Integer find(T temp) {
Node<T> nd = head;
if (size > 0 && null != head) {
for (int i = 0; i < size; i++) {
if (nd.getValue().equals(temp)) {
return i;
}
nd = nd.getNextNode();
}
}
return null;
}
public void to_s() {
Node<T> nd = head;
if (size > 0 && null != head) {
while (null != nd.getNextNode()) {
System.out.print("(" + nd.getValue() + ")->");
nd = nd.getNextNode();
}
}
System.out.println("(" + nd.getValue() + ")->(nil)");
}
public void insetAt(int index, T newElem) {
Node<T> tmp = head;
Node<T> refNode = null;
for (int i = 0; i < index-1; i++) {
refNode = tmp;
tmp = tmp.getNextNode();
}
if (refNode != null) {
Node<T> nd = new Node<T>();
nd.setValue(newElem);
nd.setNextNode(tmp.getNextNode());
if (tmp == tail) {
tail = nd;
}
tmp.setNextNode(nd);
size++;
} else {
System.out.println("WARNING: Element " + newElem
+ " does not exist in linked list. Insertion failed.");
}
}
public void deleteAt(int index) {
Node<T> tmp = head;
Node<T> refNode = null;
for (int i = 0; i <= index-1; i++) {
refNode = tmp;
tmp = tmp.getNextNode(); }
if (refNode != null) {
refNode.setNextNode(tmp.getNextNode());
size--;
} else {
System.out.println("WARNING: Element Deletion failed.");
}
}
public static void main(String a[]) {
LinkedList<Integer> sl = new LinkedList<Integer>();
sl.append(1);
sl.append(1);
sl.append(3);
sl.append(4);
sl.append(4);
sl.append(6);
sl.prepend(12);
sl.to_s();
System.out.println(sl.size);
System.out.println(sl.head.getValue());
System.out.println(sl.tail.getValue());
System.out.println(sl.at(3));
sl.pop();
sl.to_s();
System.out.println(sl.find(12));
System.out.println(sl.contains(4));
sl.insetAt(3, 99);
sl.to_s();
sl.deleteAt(4);
sl.to_s();
}
}
class Node<T> implements Comparable<T> {
private T value;
private Node<T> nextNode;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNextNode() {
return nextNode;
}
public void setNextNode(Node<T> nextNode) {
this.nextNode = nextNode;
}
@Override
public int compareTo(T arg) {
if (arg == this.value) {
return 0;
} else {
return 1;
}
}
}