Start with the following Java code: class Node { private String data; private No
ID: 3728217 • Letter: S
Question
Start with the following Java code:
class Node {
private String data;
private Node next;
public Type getData() {
return data;
}
public Node getNext() {
return next;
}
public void setData(Type data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public String toString() {
return "Node: " + getData();
}
}
///////////////////
Modify the code to complete the following conditions:
Update/Modify the LinkList class so that it supports the following operations: void removeFirst- remove the first node of the list. If the list is empty, do nothing. Set the next field of the removed node to null. int size ( ) -return the number of elements (nodes) in the list toString ( ) Size: . Elements: -implement so that the printout should be like the following · Node: Node: String getFirst - return the first Node. Return null if empty. Do not remove the node. void addFirst (String data)- Add a node at the beginning of the list with the data passed false. valid (e.g., size is less than n or if n is negative). n is negative or n is greater or equal to size) boolean contains (String target) - return true if target is found, otherwise return . void insertNode (int n, String data) - insert data at nth position. No effect if n is not . void removeNode (int n) - remove the node at nth position. No effect if n is not valid (e.g., set (int n, String data) - set nth node with the new value Provide main() method in the LinkList.java to test your code.Explanation / Answer
Please find my implementation.
public class Node {
private String data;
private Node next;
public String getData() {
return data;
}
public Node getNext() {
return next;
}
public void setData(String data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public String toString() {
return "Node: " + getData();
}
}
####################
public class LinkedList {
private Node head;
private int N;
public LinkedList() {
head = null;
N = 0;
}
public void removeFirst() {
if(head != null) {
head = head.getNext();
N--;
}
}
public int size() {
return N;
}
@Override
public String toString() {
String result = "";
result = result + "Size: "+N+" ";
result = result + "Elements: ";
Node t = head;
while(t != null) {
result = result+" Node: "+t.getData()+" ";
t = t.getNext();
}
return result;
}
String getFirst() {
return head==null ? null : head.getData();
}
public void addFirst(String data) {
Node newNode = new Node();
newNode.setData(data);
newNode.setNext(head);
head = newNode;
N++;
}
public boolean contains(String target) {
Node t = head;
while(t != null) {
if(t.getData().equalsIgnoreCase(target))
return true;
t = t.getNext();
}
return false;
}
public void indertNode(int n, String data) {
if(n < 0 || n > N)
return ;
Node newNode = new Node();
newNode.setData(data);
if(n == 0) {
newNode.setNext(head);
head = newNode;
}else{
int i = 0;
Node t = head;
while(i < n-1) {
t = t.getNext();
}
newNode.setNext(t.getNext());
t.setNext(newNode);
}
N++;
}
public void removeNode(int n) {
if(n < 0 || n >= N)
return ;
if(n == 0) {
head = head.getNext();
}else{
int i = 0;
Node t = head;
while(i < n-1) {
t = t.getNext();
}
t.setNext(t.getNext().getNext());
}
N--;
}
public void set(int n, String data) {
if(n < 0 || n > N)
return ;
if(n == 0) {
head.setData(data);
}else{
int i = 0;
Node t = head;
while(i < n-1) {
t = t.getNext();
}
t.getNext().setData(data);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.