Complete the following classes: class Node { T data; Node next; public Node (T x
ID: 3817621 • Letter: C
Question
Complete the following classes: class Node { T data; Node next; public Node (T x,Node n){} //init the node with the given data } class Linkedlist { Node head; int size; public Linkedlist(){} //creates empty linked list public Linkedlist(Node n){} //creates linked list with a given node public void add( int index,Node n){} //adds a node at the ith index of the list public void delete( int ith ){} //deletes ith index node from the list public int search (T d){} //return the index if first node with d otherwise-1 public void print(){} //print the list from head to tail recursively public void revPrint(){} //print the list from tail to head recursively }; Write the main such that it tests all the methods in the classExplanation / Answer
package chegg.april;
/**
*
* @author Sam
* @param <T>
*/
public class Linkedlist<T> {
Node<T> head;
int size;
//creates empty linked list
public Linkedlist() {
head = null;
size = 0;
}
//creates linked list with a given node
public Linkedlist(Node n) {
head = n;
size = 0;
while (n != null) {
size++;
n = n.next;
}
}
// adds a node at the ith index of the list
public void add(int index, Node n) {
if (index == 0) {
n.next = head;
head = n;
size++;
return;
}
Node<T> temp = head;
try {
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
n.next = temp.next;
temp.next = n;
size++;
} catch (NullPointerException ex) {
System.err.println("INVALID INDEX");
}
}
//deletes ith index node from the list
public void delete(int ith) {
if (ith == 0) {
head = head.next;
return;
}
Node<T> temp = head;
try {
for (int i = 0; i < ith - 1; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
size--;
} catch (NullPointerException ex) {
System.err.println("INVALID INDEX");
}
}
//return the index if first node with d otherwise -1
public int search(T d) {
int pos = 0; //pos to indicate position being searched
Node<T> temp = head;
while (temp != null) {
if (temp.data.equals(d)) { //return pos if found
return pos;
}
temp = temp.next;
pos++; //update pos
}
return -1; //indicate not found
}
//print the list from head to tail recursively
public void print(Node<T> curr) {
if (curr == null) {
return;
}
System.out.println(curr.data.toString()); //first print then proceed next
print(curr.next);
}
//print the list from tail to head recursively
public void revPrint(Node<T> curr) {
if (curr == null) {
return;
}
revPrint(curr.next); //first proceed next
System.out.println(curr.data.toString()); //then print
}
}
class Node<T> {
T data;
Node next;
public Node(T x, Node n) {
data = x;
next = n;
} //init the node with the given data
}
class LinkedlistDriver{
public static void main(String[] args) {
Linkedlist<String> list1 = new Linkedlist<>();
list1.add(0, new Node<>("A", null));
list1.add(1, new Node<>("C", null));
list1.add(2, new Node<>("D", null));
System.out.println("---------print--------");
list1.print(list1.head);
list1.add(1, new Node<>("B", null));
System.out.println("---------print--------");
list1.print(list1.head);
System.out.println("---------rev print--------");
list1.revPrint(list1.head);
list1.delete(2);
System.out.println("---------print--------");
list1.print(list1.head);
System.out.println(""A" found at position " + list1.search("A"));
System.out.println(""B" found at position " + list1.search("B"));
System.out.println(""C" found at position " + list1.search("C"));
}
}
Here you go champ. I hope you like the code. I have also commented the code to make your life easy. Incase you are facing difficulties understanding the answer please let me know through the comment section below. I shall be glad to help you with the problem.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.