subject

The Module 13 Assignment is to convert a non-generic Java class into a generic Java class and demonstrate functionality by instantiating with several different types The source code for the non-generic class came from GeeksForGeeks A Computer Science Portal for Geeks GeeksForGeeks
https://www. geeksforgeeks. org/doubly-linked-list/
The source code for the Doubly Linked List example in Java was copied, modified to move the attributes to the bottoms of classes, make the attributes private to support information hiding, and comments changed from C to Java style and then saved in this Java source file
The DLL class is written as a Doubly Linked List of ints. Modify the class to be generic and demonstrate with the following elements of the three types
Integer: 1, 5, 10
Double: 2.0, 6.0, 12.0
String: "Dog", "Cat", "Horse"
Submit Requirements, Design, Source Code, and Program Execution Output
package LinkedList;
//A complete working Java program to demonstrate all
// Doubly Linked list Node
class Node {
// Constructor to create a new node
// next and prev is by default initialized as null
Node(int data) {
this. data = data;
}
public int getData() {
return this. data;
}
public void setData(int data) {
this. data = data;
}
public Node getPrev() {
return this. prev;
}
public void setPrev(Node prev) {
this. prev = prev;
}
public Node getNext() {
return this. next;
}
public void setNext(Node next) {
this. next = next;
}
private int data;
private Node prev;
private Node next;
}
//Class for Doubly Linked List
public class DLL {
// Adding a node at the front of the list
public void push(int new_data) {
// 1. allocate node
// 2. put in the data
Node new_Node = new Node(new_data);
// 3. Make next of new node as head and previous as NULL
new_Node. setNext(head);
new_Node. setPrev(null);
// 4. change prev of head node to new node
if (head != null) {
head. setPrev(new_Node);
}
// 5. move the head to point to the new node
head = new_Node;
}
// Given a node as prev_node, insert a new node after the given node
public void InsertAfter(Node prev_Node, int new_data) {
// 1. check if the given prev_node is NULL
if (prev_Node == null) {
System. out. println("The given previous node cannot be NULL ");
return;
}
// 2. allocate node
// 3. put in the data
Node new_node = new Node(new_data);
// 4. Make next of new node as next of prev_node
new_node. setNext(prev_Node. getNext());
// 5. Make the next of prev_node as new_node
prev_Node. setNext(new_node);
// 6. Make prev_node as previous of new_node
new_node. setPrev(prev_Node);
// 7. Change previous of new_node's next node
if (new_node. getNext() != null) {
new_node. getNext().setPrev(new_node);
}
}
// Add a node at the end of the list
void append(int new_data) {
// 1. allocate node
// 2. put in the data
Node new_node = new Node(new_data);
Node last = head; // used in step 5
// 3. This new node is going to be the last node, so make next of it as NULL
new_node. setNext(null);
// 4. If the Linked List is empty, then make the new node as head
if (head == null) {
new_node. setPrev(null);
head = new_node;
return;
}
// 5. Else traverse till the last node
while (last. getNext() != null) {
last = last. getNext();
}
// 6. Change the next of last node
last. setNext(new_node);
// 7. Make last node as previous of new node
new_node. setPrev(last);
}
// This function prints contents of linked list starting from the given node
public void printlist(Node node) {
Node last = null;
System. out. println("Traversal in forward Direction");
while (node != null) {
System. out. print(node. getData() + " ");
last = node;
node = node. getNext();
}
System. out. println();
System. out. println("Traversal in reverse direction");
while (last != null) {
System. out. print(last. getData() + " ");
last = last. getPrev();
}
}

/* Drier program to test above functions */
public static void main(String[] args) {
/* Start with the empty list */
DLL dll = new DLL();
// Insert 6. So linked list becomes 6->NULL
dll. append(6);
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
dll. push(7);
// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL
dll. push(1);
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
dll. append(4);
// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL< br /> dll. InsertAfter(dll. head. getNext(), 8);
System. out. println("Created DLL is: ");
dll. printlist(dll. head);
}
private Node head; // head of list
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 11:00
Technician a says that the radiator usually cools better if the front air dam is removed. technician b says that when a condenser has a leak it can be repaired easily with epoxy. who is correct?
Answers: 1
question
Computers and Technology, 24.06.2019 22:00
What is a number system, and what is the total number of digits used in this system called? a number system is a system that uses different (options: a) numbers b) symbols c) codes d) digits e) alphabets) to represent different numbers. the total number of digits used in a number system is known as its (options: 1) processor 2) converter 3) radix 4) least significant digit 5) most significant digit)
Answers: 1
question
Computers and Technology, 25.06.2019 00:00
He computer component that disperses heat from the microprocessor to the cooling fan is a cooler thermometer heat sink
Answers: 1
question
Computers and Technology, 25.06.2019 04:30
What should be used when performing vehicle maneuvers?
Answers: 1
You know the right answer?
The Module 13 Assignment is to convert a non-generic Java class into a generic Java class and demons...
Questions
question
Mathematics, 02.11.2020 14:00
question
Mathematics, 02.11.2020 14:00
question
Mathematics, 02.11.2020 14:00
question
History, 02.11.2020 14:00
question
Mathematics, 02.11.2020 14:00
Questions on the website: 13722363