Question

In: Computer Science

pls use deep clone method to make a deep copy of each elements in a singly...

pls use deep clone method to make a deep copy of each elements in a singly linked list.

(show detailed code and explanation in java. thanks!)

Solutions

Expert Solution

Program

//Node class
class Node
{
   //data members
   private int data;
   private Node link;
   //constructor
   public Node(int data, Node link){
      this.data = data;
   this.link = link;
   }
   //return the data
   public int getData(){
   return data;
   }
   //return the link
   public Node getLink(){
      return link;
   }
   //modified the data
   public void setData(int data){
   this.data = data;
   }
   //modified the link
   public void setLink(Node link){
   this.link = link;
   }
}

//singly linked list
class SLinkedList
{
   //head node
   private Node head;
  
   //constructor
   public SLinkedList(){
       head = null;
   }
  
   //method to insert an item to end of the list
   public void insert(int item)
   {
       //create a new node
       Node newnode = new Node(item, null);
       //check list is empty
       if(head==null)
       {
           head = newnode;
       }
       else
       {
           //traverse the list
           Node tmp = head;
           while(tmp.getLink()!=null)
           {
               tmp = tmp.getLink();
           }
           //insert an item to end of the list
           tmp.setLink(newnode);
       }
   }
  
   //deep copy of each elements in a singly linked list
   public static SLinkedList deepClone(SLinkedList list1)
   {
       //create a new singly linked list
       SLinkedList list2 = new SLinkedList();
       //head1 refer to the head of the original list
       Node head1 = list1.head;
       //create the head of the newly list and head2 refer to the head
       Node head2 = list2.head = new Node(head1.getData(), head1.getLink());
       //head1 refer to the next node of the original list
       head1 = head1.getLink();
      
       //processing until end of the list
       while(head1 != null)
       {
           //create a new node
           Node newnode = new Node(head1.getData(), head1.getLink());
           //advance one node of the newly list
           head2 = head2.getLink();
           //advance one node of the original list
           head1 = head1.getLink();
       }
       //return the newly list
       return list2;
   }
  
   //method to print the list
   public void printList()
   {
       if(head==null)
       {
           System.out.println ("List is empty");
           return;
       }
       System.out.print("List: ");
       Node temp = head;
       while(temp!= null)
       {
           System.out.print (temp.getData() + " ");
           temp = temp.getLink();
       }
       System.out.println ();
   }
}

//LinkedListDemo class
class LinkedListDemo
{
   //main method
   public static void main (String[] args)
   {
       //create object of SLinkedList
       SLinkedList list = new SLinkedList();
      
       //Insert some values to the list
       for(int i=1; i<=10; i++)
       {
           list.insert(i);
       }
  
       //print the original list
       list.printList();
      
       //deep copy from original list to newly cloned list
       SLinkedList list2 = SLinkedList.deepClone(list);
      
       //print the newly cloned list
       list2.printList();
   }
}

Output:

List: 1 2 3 4 5 6 7 8 9 10
List: 1 2 3 4 5 6 7 8 9 10

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you.
Thank you.


Related Solutions

Urgent! pls show all ways of how to make a deep copy of each elements in...
Urgent! pls show all ways of how to make a deep copy of each elements in a singly linked list (linear time) (pls write code with explanation in java! Thanks in advance! )
Add a clone method to the BST program. This method returns a deep copy of a...
Add a clone method to the BST program. This method returns a deep copy of a tree. Coding Language C++ Use this main method to test your code: int main() { Tree T; T.insert(50); T.insert(20); T.insert(80); T.insert(60); T.insert(90); T.display(); Tree T1 = T; Tree T2 = T.clone(); cout << "\n\nT1 and T2 before insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); T.insert(999); cout << "\n\nT1 and T2 after insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); system("pause"); // may need to remove this on some systems return 0; } #include...
Using the bisection method:     Make a program to use this method using the following three...
Using the bisection method:     Make a program to use this method using the following three functions and use it to find the root of this function f (x) = x * x * x-8. a) A function so that the user between xlower and xupper that meets the value of the function has a different sign and if he does not ask for new values. b) A function to find the root and call it bisection and perform a...
DO NOT USE EXCEL PLS EXPLAIN EACH STEP Use AstroTurf Company's income statement below to answer...
DO NOT USE EXCEL PLS EXPLAIN EACH STEP Use AstroTurf Company's income statement below to answer the following two questions. a. Calculate AstroTurf’s EBITDA. b. Determine what level of sales would generate $3.3m in net income for the following year (Year 2), knowing that operating costs (excl. depreciation and amortization) will increase by 5%. The tax rate will not change. Year 1 Income Statement: Sales: $11,000,000 Operating costs (excl. depreciations & amortization): $4,500,000 Depreciation and amortization: $1,500,000 Interest: $700,000 Net...
Discussion GIARISM IS NOT ALLOWED, USE YOUR WORDS DON’T COPY AND PASTE. *(Pleas make it as...
Discussion GIARISM IS NOT ALLOWED, USE YOUR WORDS DON’T COPY AND PASTE. *(Pleas make it as a text not handwriting) if there any reference add it During the Covid-19 pandemic and based on its natural a great deal of businesses has been impacted by it. Also, due to its rapid outbreak working from home and the limitation of travel become vital and critical to limit the spread of Covid-19 as well as the safety of the people. Thus, digital transformation...
Please don't Copy an answer and paste it, explain use variables that make sense The length...
Please don't Copy an answer and paste it, explain use variables that make sense The length of time a potential customer visits a website is Exponentially distributed with a mean of             40 seconds.             If the potential customer stays at the website for 60 seconds or more, then the probability             that the customer will place an order is 0.90.             However, if the customer leaves the website within 60 seconds, then the probability they place an order is...
Use Minitab to answer the questions. Make sure to copy all output from the Minitab: The...
Use Minitab to answer the questions. Make sure to copy all output from the Minitab: The U.S. Bureau of Labor Statistics publishes a variety of unemployment statistics, including the number of individuals who are unemployed and the mean length of time the individuals have been unemployed. For November 1998, the Bureau of Labor Statistics reported that the national mean length of time of unemployment was 14.5 weeks. The mayor of Chicago has requested the study on the status of unemployment...
To the TwoDArray class, add a method called sumCols() that sums the elements in each column...
To the TwoDArray class, add a method called sumCols() that sums the elements in each column and displays the sum for each column. Add appropriate code in main() of the TwoDArrayApp class to execute the sumCols() method. /** * TwoDArray.java */ public class TwoDArray { private int a[][]; private int nRows; public TwoDArray(int maxRows, int maxCols) //constructor { a = new int[maxRows][maxCols]; nRows = 0; } //****************************************************************** public void insert (int[] row) { a[nRows] = row; nRows++; } //****************************************************************** public...
Use the rules (in order) to assign oxidation numbers to each of the elements in the...
Use the rules (in order) to assign oxidation numbers to each of the elements in the compounds below. hydroxylamine H N O NH2OH ___-7-6-5-4-3-2-10+1+2+3+4+5+6+7 ___-7-6-5-4-3-2-10+1+2+3+4+5+6+7 ___-7-6-5-4-3-2-10+1+2+3+4+5+6+7 magnesium bromide Br Mg MgBr2 ___-7-6-5-4-3-2-10+1+2+3+4+5+6+7 ___-7-6-5-4-3-2-10+1+2+3+4+5+6+7 boric acid H B O H3BO3
Use Minitab to answer the questions. Make sure to copy all output from the Minitab: Followings...
Use Minitab to answer the questions. Make sure to copy all output from the Minitab: Followings Tables shows previous 11 months stock market returns. Date Monthly SP500 Return Monthly DJIA Return 12/7/2007 -0.8628 -0.7994 1/8/2008 -6.1163 -4.6323 2/8/2008 -3.4761 -3.0352 3/8/2008 -0.5960 -0.0285 4/8/2008 4.7547 4.5441 5/8/2008 1.0674 -1.4182 6/8/2008 -8.5962 -10.1937 7/8/2008 -0.9859 0.2468 8/8/2008 1.2191 1.4548 9/8/2008 -9.2054 -6.0024 10/8/2008 -16.8269 -4.8410 1. Let consider we know the variance of monthly return for all stock were 25 percent...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT