Develop your own linked list class in Java. For simplicity, we will consider an
ID: 3790187 • Letter: D
Question
Develop your own linked list class in Java. For simplicity, we will consider an integer linked list. Recall that a linked list is a recursively defined data structure which consists of a list node, which is an object that contains a data field as well as a reference to the next list node of the list. The first node of the list is usually called the head node (or more correctly stated, is referred to by a reference variable called head).
Part 1: Write a class called IntegerListNode that contains the following two instance fields:
private int data;
private IntegerListNode next;
Class should have appropriate constructors and getter/setter functions as needed to support the rest of the questions.
Part 2: Write a class called IntegerLinkedList that contains the following instance field:
private IntegerListNode head;
Class should have a default constructor that creates an empty list (i.e., head == null). You should also write the following member functions:
public void insertNode(int newdata);
public boolean deleteNode(int data_to_delete);
@Override public String toString();
The first method should insert newdata at the end of the list. The second method should search the list for data_to_delete. If it finds it, it should remove the list node containing it and return true. If it does not find it, then it should return false. The third method should return a string containing the integers in the list, separated by commas.
Part 3: Write a main function that demonstrates the use of your IntegerLinkedList class.
Explanation / Answer
Hi, Please find my code.
Please let me know in case of any issue.
class IntegerListNode{
int data;
IntegerListNode next;
IntegerListNode(int data){
this.data = data;
next = null;
}
}
public class IntegerLinkedList {
private IntegerListNode head;
public IntegerLinkedList() {
head = null;
}
public void insertNode(int newdata){
if(head == null)
head = new IntegerListNode(newdata);
else{
IntegerListNode temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new IntegerListNode(newdata);
}
}
public boolean deleteNode(int data_to_delete){
if(head == null)
return false;
if(head.data == data_to_delete){
head = head.next;
return true;
}
IntegerListNode temp = head;
while(temp.next != null){
if(temp.next.data == data_to_delete){
temp.next = temp.next.next;
return true;
}
}
return false;
}
@Override public String toString(){
String list = "";
IntegerListNode temp = head;
while(temp!= null){
if(temp.next == null)
list = list + temp.data;
else
list = list + temp.data+", ";
temp = temp.next;
}
return list;
}
}
##################
public class IntegerLinkedListTest {
public static void main(String[] args) {
IntegerLinkedList integerLinkedList = new IntegerLinkedList();
integerLinkedList.insertNode(23);
integerLinkedList.insertNode(3);
integerLinkedList.insertNode(50);
integerLinkedList.insertNode(98);
System.out.println(integerLinkedList);
integerLinkedList.deleteNode(23);
System.out.println(integerLinkedList);
}
}
/*
Sample run:
23, 3, 50, 98
3, 50, 98
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.