Question

In: Computer Science

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

Step 1: Create a new Java project called Lab5.5.

Step 2: Now create a new class called aDLLNode.
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 the content 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 the above steps are working properly.

Step 5: Now create a new class called doubleLinkedList
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 + " at beginning ");
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 being deleted.
public void delete(char deleteValue) {
System.out.println("Attempting to Delete: " + deleteValue);
if (isEmpty()) { // Case 1
System.out.println("Linked List is empty, nothing to delete");
} else {
if (head == tail) { // Case 2, only one node (head and tail pointing to it)
if (head.data == deleteValue) {
head = tail = null;
size--;
}
} else {
if (head.data == deleteValue) { // Case 3, Head is being deleted.
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 garbage collection
size--;
} else {
deletePtr = deletePtr.next;
}
}
if (found == false) {
System.out.println("Not able to find/delete " + deleteValue + " in the 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 garbage collector
freePtr = head; // now let the freePtr to the new head
}
head = null;
size = 0;
}
}

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

Step 7: Now add the following code after the code in step 3 (in the main() function). The code below instantiates a doubly linked list
called “myDLL”, and then proceeds to call it’s method insert_at_beginning() several times.
doubleLinkedList myDLL = new doubleLinkedList();
System.out.println("-----------------------------------------");
System.out.println("--------Testing Insert_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() method of the doubleLinkedList class. Copy the following code and add it to the bottom of your main() function.
System.out.println("-----------------------------------------");
System.out.println("-----------Testing Insert_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 the doubleLinkedList class. Copy the following code and add it to the bottom of your main() function.
System.out.println("-----------------------------------------");
System.out.println("--------------Testing Delete-------------");
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.

Solutions

Expert Solution

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.

  1. /*
  2.  *  Java Program to Implement Doubly Linked List
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. /*  Class Node  */
  8. class Node
  9. {
  10.     protected int data;
  11.     protected Node next, prev;
  12.  
  13.     /* Constructor */
  14.     public Node()
  15.     {
  16.         next = null;
  17.         prev = null;
  18.         data = 0;
  19.     }
  20.     /* Constructor */
  21.     public Node(int d, Node n, Node p)
  22.     {
  23.         data = d;
  24.         next = n;
  25.         prev = p;
  26.     }
  27.     /* Function to set link to next node */
  28.     public void setLinkNext(Node n)
  29.     {
  30.         next = n;
  31.     }
  32.     /* Function to set link to previous node */
  33.     public void setLinkPrev(Node p)
  34.     {
  35.         prev = p;
  36.     }    
  37.     /* Funtion to get link to next node */
  38.     public Node getLinkNext()
  39.     {
  40.         return next;
  41.     }
  42.     /* Function to get link to previous node */
  43.     public Node getLinkPrev()
  44.     {
  45.         return prev;
  46.     }
  47.     /* Function to set data to node */
  48.     public void setData(int d)
  49.     {
  50.         data = d;
  51.     }
  52.     /* Function to get data from node */
  53.     public int getData()
  54.     {
  55.         return data;
  56.     }
  57. }
  58.  
  59. /* Class linkedList */
  60. class linkedList
  61. {
  62.     protected Node start;
  63.     protected Node end ;
  64.     public int size;
  65.  
  66.     /* Constructor */
  67.     public linkedList()
  68.     {
  69.         start = null;
  70.         end = null;
  71.         size = 0;
  72.     }
  73.     /* Function to check if list is empty */
  74.     public boolean isEmpty()
  75.     {
  76.         return start == null;
  77.     }
  78.     /* Function to get size of list */
  79.     public int getSize()
  80.     {
  81.         return size;
  82.     }
  83.     /* Function to insert element at begining */
  84.     public void insertAtStart(int val)
  85.     {
  86.         Node nptr = new Node(val, null, null);        
  87.         if(start == null)
  88.         {
  89.             start = nptr;
  90.             end = start;
  91.         }
  92.         else
  93.         {
  94.             start.setLinkPrev(nptr);
  95.             nptr.setLinkNext(start);
  96.             start = nptr;
  97.         }
  98.         size++;
  99.     }
  100.     /* Function to insert element at end */
  101.     public void insertAtEnd(int val)
  102.     {
  103.         Node nptr = new Node(val, null, null);        
  104.         if(start == null)
  105.         {
  106.             start = nptr;
  107.             end = start;
  108.         }
  109.         else
  110.         {
  111.             nptr.setLinkPrev(end);
  112.             end.setLinkNext(nptr);
  113.             end = nptr;
  114.         }
  115.         size++;
  116.     }
  117.     /* Function to insert element at position */
  118.     public void insertAtPos(int val , int pos)
  119.     {
  120.         Node nptr = new Node(val, null, null);    
  121.         if (pos == 1)
  122.         {
  123.             insertAtStart(val);
  124.             return;
  125.         }            
  126.         Node ptr = start;
  127.         for (int i = 2; i <= size; i++)
  128.         {
  129.             if (i == pos)
  130.             {
  131.                 Node tmp = ptr.getLinkNext();
  132.                 ptr.setLinkNext(nptr);
  133.                 nptr.setLinkPrev(ptr);
  134.                 nptr.setLinkNext(tmp);
  135.                 tmp.setLinkPrev(nptr);
  136.             }
  137.             ptr = ptr.getLinkNext();            
  138.         }
  139.         size++ ;
  140.     }
  141.     /* Function to delete node at position */
  142.     public void deleteAtPos(int pos)
  143.     {        
  144.         if (pos == 1) 
  145.         {
  146.             if (size == 1)
  147.             {
  148.                 start = null;
  149.                 end = null;
  150.                 size = 0;
  151.                 return; 
  152.             }
  153.             start = start.getLinkNext();
  154.             start.setLinkPrev(null);
  155.             size--; 
  156.             return ;
  157.         }
  158.         if (pos == size)
  159.         {
  160.             end = end.getLinkPrev();
  161.             end.setLinkNext(null);
  162.             size-- ;
  163.         }
  164.         Node ptr = start.getLinkNext();
  165.         for (int i = 2; i <= size; i++)
  166.         {
  167.             if (i == pos)
  168.             {
  169.                 Node p = ptr.getLinkPrev();
  170.                 Node n = ptr.getLinkNext();
  171.  
  172.                 p.setLinkNext(n);
  173.                 n.setLinkPrev(p);
  174.                 size-- ;
  175.                 return;
  176.             }
  177.             ptr = ptr.getLinkNext();
  178.         }        
  179.     }    
  180.     /* Function to display status of list */
  181.     public void display()
  182.     {
  183.         System.out.print("\nDoubly Linked List = ");
  184.         if (size == 0) 
  185.         {
  186.             System.out.print("empty\n");
  187.             return;
  188.         }
  189.         if (start.getLinkNext() == null) 
  190.         {
  191.             System.out.println(start.getData() );
  192.             return;
  193.         }
  194.         Node ptr = start;
  195.         System.out.print(start.getData()+ " <-> ");
  196.         ptr = start.getLinkNext();
  197.         while (ptr.getLinkNext() != null)
  198.         {
  199.             System.out.print(ptr.getData()+ " <-> ");
  200.             ptr = ptr.getLinkNext();
  201.         }
  202.         System.out.print(ptr.getData()+ "\n");
  203.     }
  204. }
  205.  
  206. /* Class DoublyLinkedList */
  207. public class DoublyLinkedList
  208. {    
  209.     public static void main(String[] args)
  210.     {            
  211.         Scanner scan = new Scanner(System.in);
  212.         /* Creating object of linkedList */
  213.         linkedList list = new linkedList(); 
  214.         System.out.println("Doubly Linked List Test\n");          
  215.         char ch;
  216.         /*  Perform list operations  */
  217.         do
  218.         {
  219.             System.out.println("\nDoubly Linked List Operations\n");
  220.             System.out.println("1. insert at begining");
  221.             System.out.println("2. insert at end");
  222.             System.out.println("3. insert at position");
  223.             System.out.println("4. delete at position");
  224.             System.out.println("5. check empty");
  225.             System.out.println("6. get size");
  226.  
  227.             int choice = scan.nextInt();            
  228.             switch (choice)
  229.             {
  230.             case 1 : 
  231.                 System.out.println("Enter integer element to insert");
  232.                 list.insertAtStart( scan.nextInt() );                     
  233.                 break;                          
  234.             case 2 : 
  235.                 System.out.println("Enter integer element to insert");
  236.                 list.insertAtEnd( scan.nextInt() );                     
  237.                 break;                         
  238.             case 3 : 
  239.                 System.out.println("Enter integer element to insert");
  240.                 int num = scan.nextInt() ;
  241.                 System.out.println("Enter position");
  242.                 int pos = scan.nextInt() ;
  243.                 if (pos < 1 || pos > list.getSize() )
  244.                     System.out.println("Invalid position\n");
  245.                 else
  246.                     list.insertAtPos(num, pos);
  247.                 break;                                          
  248.             case 4 : 
  249.                 System.out.println("Enter position");
  250.                 int p = scan.nextInt() ;
  251.                 if (p < 1 || p > list.getSize() )
  252.                     System.out.println("Invalid position\n");
  253.                 else
  254.                     list.deleteAtPos(p);
  255.                 break;     
  256.             case 5 : 
  257.                 System.out.println("Empty status = "+ list.isEmpty());
  258.                 break;            
  259.             case 6 : 
  260.                 System.out.println("Size = "+ list.getSize() +" \n");
  261.                 break;                         
  262.             default : 
  263.                 System.out.println("Wrong Entry \n ");
  264.                 break;   
  265.             }    
  266.             /*  Display List  */ 
  267.             list.display();
  268.             System.out.println("\nDo you want to continue (Type y or n) \n");
  269.             ch = scan.next().charAt(0);    
  270.  
  271.         } while (ch == 'Y'|| ch == 'y');               
  272.     }
  273. }

Related Solutions

1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data types to store the following information: The names of the bride and groom The total number of guests at the wedding The square footage of the location (must be accurate to 0.1 square feet). The names of each song in the DJ's playlist. You should use an ArrayList of Strings to store this, and the user should be able to enter as many song...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
JAVA PROJECT Step 1: Create an application named YourInitials_Project 3 that uses a class to convert...
JAVA PROJECT Step 1: Create an application named YourInitials_Project 3 that uses a class to convert number grades to letter grades and another class for data validation. Make comments including your name, date, and project name as well as appropriate comments throughout your application that include the step number. Step 2: Create code to print Titles Step 3: Create a variable to hold user’s choice. Remember that all variables must begin with your initials and should be descriptive. Step 4:...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT