Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class...

60.1K

Verified Solution

Question

Programming

Step 1: Create a new Java project calledLab5.5.

Step 2: Now create a new class calledaDLLNode.
class aDLLNode {
aDLLNode prev;   
char data;   
aDLLNode next;

aDLLNode(char mydata) { // Constructor
data = mydata;
next = null;   
prev = null;   
}
};

Step 3: In the main() function of the driver class(Lab5.5), instantiate an object of type aDLLNode and print thecontent of its class
public static void main(String[] args) {System.out.println(\"-----------------------------------------\");  
System.out.println(\"--------Create a new DLLNode-------\");  
aDLLNode myDLLNode = new aDLLNode('A');   
System.out.println(myDLLNode.data);
System.out.println(myDLLNode.next);
System.out.println(myDLLNode.prev);
}
The above is an example of dynamically allocating an object of type“aDLLNode”. Once the object is created, we can use the “dot”notation to access and display its properties.

Step 4: Compile and run the program to make sure theabove steps are working properly.

Step 5: Now create a new class calleddoubleLinkedList
class doubleLinkedList {
aDLLNode head, tail; //keep track of head, tail nodes
int size;
doubleLinkedList() { // Constructor
head = tail = null;
size = 0;
}
//-----------------------------------------------------
public void insert_at_beginning(char value) {

System.out.println(\"Attempting to Insert \" + value + \" atbeginning \");
aDLLNode newNode = new aDLLNode(value); // create aNew node
if (size == 0) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
size++;
}

//-----------------------------------------------------
// Delete the first node with the value
// Five cases must be resolved:
// 1) The linked list is empty (head and tail = null)
// 2) The Linked list has one node and it is being deleted (head =tail)
// 3) The node pointed to by the Head is being deleted.
// 4) The node pointed to by the Tail is being deleted.
// 5) Some node in the middle (not the head or tail) is beingdeleted.
public void delete(char deleteValue) {
System.out.println(\"Attempting to Delete: \" + deleteValue);
if (isEmpty()) { // Case 1
System.out.println(\"Linked List is empty, nothing todelete\");
} else {
if (head == tail) { // Case 2, only one node (head and tailpointing to it)
if (head.data == deleteValue) {
head = tail = null;
size--;
}
} else {
if (head.data == deleteValue) { // Case 3, Head is beingdeleted.
head = head.next;
size--;
} else {
// Case 4 and 5, find the node to be deleted middle or end
boolean found = false;
aDLLNode deletePtr = head; // create a refe
if (deletePtr.data == deleteValue) {
found = true;
if (tail == deletePtr) { // Case 4
deletePtr.prev.next = deletePtr.next;
tail = deletePtr.prev;
} else { // Case 5
deletePtr.prev.next = deletePtr.next;
deletePtr.next.prev = deletePtr.prev;
}
deletePtr = null; // make deletePtr availabe to garbagecollection
size--;
} else {
deletePtr = deletePtr.next;
}
}
if (found == false) {
System.out.println(\"Not able to find/delete \" + deleteValue + \" inthe Doubly Linked List\");
}
}
}
}
}
//-----------------------------------------------------
public boolean isEmpty() {
if (head == null) {
return (true);
} else {
return (false);
}
}
//-----------------------------------------------------
public void print_from_beginning() {
aDLLNode ptr;
ptr = head;
System.out.print(\"Printing: Head--> \");
while (ptr != null) {
System.out.print(ptr.data + \" --> \");
ptr = ptr.next;
}
System.out.println(\"NULL / <--Tail \" + \"(Size = \" + size +\")\");
}
//-----------------------------------------------------
public int getSize() {
return (size);
}
//-----------------------------------------------------
public void freeAll() {
System.out.println(\"Freeing Doubly Linked List........ \");
aDLLNode freePtr = head;
while (head != null) {
head = head.next;
// the next two lines are unnecessary, but are included for
// illustration of how memory is freed up
//
freePtr = null; // make the node available for garbagecollector
freePtr = head; // now let the freePtr to the new head
}
head = null;
size = 0;
}
}

Step 6: Compile and run the program to make sure theabove steps are still working properly. The result should be stillthe same as Step 4.

Step 7: Now add the following code after the code instep 3 (in the main() function). The code below instantiates adoubly linked list
called “myDLL”, and then proceeds to call it’s methodinsert_at_beginning() several times.
doubleLinkedList myDLL = new doubleLinkedList();
System.out.println(\"-----------------------------------------\");
System.out.println(\"--------TestingInsert_at_Begining-------\");
myDLL.insert_at_beginning('A');
myDLL.insert_at_beginning('b');
myDLL.insert_at_beginning('C');
myDLL.insert_at_beginning('d');
myDLL.print_from_beginning();

Step 8: Compile and run the program.

Step 9: Now, let try running the insert_after() methodof the doubleLinkedList class. Copy the following code and add itto the bottom of your main() function.
System.out.println(\"-----------------------------------------\");
System.out.println(\"-----------TestingInsert_After----------\");
myDLL.insert_after('8', 'C');
myDLL.print_from_beginning();
myDLL.insert_after('9', 'D');
myDLL.print_from_beginning();

Step 10: Compile and run the program.

Step 11: Now, let try running the delete() method of thedoubleLinkedList class. Copy the following code and add it to thebottom of your main() function.
System.out.println(\"-----------------------------------------\");
System.out.println(\"--------------TestingDelete-------------\");
myDLL.print_from_beginning();
myDLL.delete('d');
myDLL.print_from_beginning();
myDLL.delete('A');
myDLL.print_from_beginning();
myDLL.delete('7');
myDLL.print_from_beginning();
myDLL.freeAll();
myDLL.delete('8');
myDLL.print_from_beginning();
myDLL.insert_at_beginning('8');
myDLL.insert_at_beginning('9');
myDLL.print_from_beginning();
myDLL.delete('8');
myDLL.print_from_beginning();

Step 12: Compile and run the program.

What to submit
The final source code after step 12.

Answer & Explanation Solved by verified expert
3.8 Ratings (488 Votes)
So To be clear I wrote a cripsy code of double linked list all operation in as menu dashboard all the stuffs is easily understandable Java Program to Implement Doubly Linked List import javautilScanner Class Node class Node protected int data protected Node next prev Constructor public Node next null prev null data 0 Constructor public Nodeint d Node n Node p data d next n prev p Function to set link to next node public    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students