IN java please The following is a class named ListElement. This class demonstrat
ID: 3595869 • Letter: I
Question
IN java please
The following is a class named ListElement. This class demonstrates the functionality of a Double Linked List.
public class ListElement {
private ListElement next;
private ListElement previous;
private int data;
public ListElement(){
this.data = 0;
this.next = null;
this . previous = null ; }
Notice the private members. If a user wants to access these members, we must give them access via setters and getters.
/
@param data
an integer to be stored in the node @return
None
**/
public void setData(int data) {
this.data = data; }
public int getData () {
return this . data ; }
}
Now create an object of this class and access the members:
ListElement le = new ListElement(); le.setData(5);
A ) you will create a single linked list. in java please
Your have to write a Linked List Program that has the following requirements. Runs a program which allows the user to:
• Add a node public void addElement(ListElement le)
• Retrieve a node public ListElement getElement(int index)
• Delete a node public ListElement deleteElement(int index)
• Print the linked list from the tail public void printLinkedListTail()
• Print the linked list from the head public void printLinkedListHead()
B) AFTER THAT: Now refactor your design so that it is a Double Linked List as the class above indicates. develop the Double Linked List code.
Explanation / Answer
Hi, Please find my implementation.
public class ListElement {
private ListElement next;
private ListElement previous;
private int data;
public ListElement(){
this.data = 0;
this.next = null;
this . previous = null ; }
public void setData(int data) {
this.data = data;
}
public int getData () {
return this . data ;
}
public ListElement getNext() {
return next;
}
public ListElement getPrevious() {
return previous;
}
public void setNext(ListElement next) {
this.next = next;
}
public void setPrevious(ListElement previous) {
this.previous = previous;
}
}
##############
public class DLList {
private ListElement head;
public DLList() {
head = null;
}
// add new node at head
public void addElement(ListElement le) {
le.setNext(head);
if(head != null) {
head.setPrevious(le);
}
head = le;
}
public ListElement getElement(int index) {
if(head == null || index < 0)
return null;
int count = 0;
ListElement temp = head;
while(temp != null && index <= count) {
if(index == count)
return temp;
count++;
temp =temp.getNext();
}
return null; // index is greater than totla element
}
public ListElement deleteElement(int index) {
if(head == null || index < 0)
return null;
if(index == 0) {
ListElement t = head;
head = head.getNext();
head.setPrevious(null);
return t;
}
int count = 0;
ListElement temp = head;
while(temp != null && count < index) {
count++;
temp =temp.getNext();
}
if(temp == null)// index is greater than totla element
return null;
temp.getPrevious().setNext(temp.getNext()); // setting next of previous of temp to next of temp
// setting previous of next of temp to previous of temp
if(temp.getNext() != null) {
temp.getNext().setPrevious(temp.getPrevious());
}
return temp;
}
public void printLinkedListTail() {
if(head == null)
return;
ListElement t = head;
// reaching to last element
while(t.getNext() != null)
t = t.getNext();
// printing from last
while(t != null) {
System.out.print(t.getData()+" ");
t = t.getPrevious();
}
System.out.println();
}
public void printLinkedListHead() {
if(head == null)
return;
ListElement t = head;
// reaching to last element
while(t != null){
System.out.print(t.getData()+" ");
t = t.getNext();
}
System.out.println();
}
}
#########
public class TestDLList {
public static void main(String[] args) {
DLList list = new DLList();
ListElement l1 = new ListElement();
l1.setData(4);
list.addElement(l1);
list.printLinkedListHead();
list.printLinkedListTail();
ListElement l2 = new ListElement();
l2.setData(6);
list.addElement(l2);
list.printLinkedListHead();
list.printLinkedListTail();
list.deleteElement(1);
list.printLinkedListHead();
list.printLinkedListTail();
}
}
/*
Sample run:
4
4
6 4
4 6
6
6
*/