Question

In: Computer Science

3- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join...

3- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join them together to give one. So if the lists lst1= [1,3,7,4] and lst2=[2,4,5,8,6], the result of lst1.join(lst2) is lst1=[1,3,7,4,2,4,5,8,6] and lst2=[].
4- Use FirstLastList: Write method public void swap(). It swaps the first and last elements of a FirstLastList. So if lst1= [1,3,7,4], lst1.swap() = [4,3,7,1]. Display or throw an exception if the list contains less than two elements.

public class FirstLastList
{
private Link first; // ref to first link
private Link last; // ref to last link
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link

if( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}

public void join(FirstLastList otherList) {
Link current = otherList.first; // start at beginning
while (current != null) { // until end of list,
this.insertLast(current.dData);
current = current.next; // move to next link
}
otherList.first = null;
}

public void swap() throws Exception {
int count = 0;
Link current = first; // start at beginning
while (current != null) { // until end of list,
count++;
current = current.next; // move to next link
}
  
if (count < 2) {
throw new Exception("There is not enough element in the list.");
} else
{
long firstData = first.dData;
long lastData = last.dData;
first.dData = lastData;
last.dData = firstData;
}
}
}

class Link
{
public long dData; // data item
public Link next; // next link in list

public Link(long d) // constructor
{ dData = d; }

public void displayLink() // display this link
{ System.out.print(dData + " "); }
} // end class Link

public class FirstLastAppTest
{
public static void main(String[] args)
{
FirstLastList lst1 = new FirstLastList(); // Start a new FirstLastList called lst1

lst1.insertLast(1); // Add links with data to the last position
lst1.insertLast(3);
lst1.insertLast(7);
lst1.insertLast(4);
System.out.print("\nlst1: "); // print the description for the list
lst1.displayList(); // print the contents of the list

FirstLastList lst2 = new FirstLastList(); // Start a new FirstLastList called lst2

lst2.insertLast(2); // Add links with data to the last position
lst2.insertLast(4);
lst2.insertLast(5);
lst2.insertLast(8);
lst2.insertLast(6);
System.out.print("\nlst2: "); // print the description for the list
lst2.displayList(); // print the contents of the list

System.out.print("\nlst1.join(lst2): "); // print the action to take place: lst1.join(lst2)
lst1.join(lst2); // call the join method for lst1 to add lst2
System.out.print("\nlst1: "); // print the description for the list
lst1.displayList(); // print the contents of the list lst1; post join()
System.out.print("lst2: "); // print the description for the list
lst2.displayList(); // print the contents of the list lst2; post join()

System.out.print("\nlst1.swap(): "); // print the action to take place: lst1.swap()
lst1.swap(); // call the swap method for lst1
System.out.print("\nlst1: "); // print the description for the list
lst1.displayList(); // print the contents of the list lst1; post swap()
} // end main()
} // end class

I can't get FirstLastAppTest to work with my code. I can't post my FirstLastApp as I have limited space for 1 question.

PLEASE DO NOT MODIFY FirstLastAppTest!!! Its just for testing the code.

Solutions

Expert Solution

Below is corrected code. I have modified FirstLastList.java file and added comments where i modified the code. You can compare your code with this one to get better understanding. Let me know if you still have any problem or doubts. Thank you.

=====================================================================

FirstLastList.java

============================

public class FirstLastList
{
   private Link first; // ref to first link
   private Link last; // ref to last link
  
   // -------------------------------------------------------------
   public FirstLastList() // constructor
   {
       first = null; // no links on list yet
       last = null;
   }
  
   // -------------------------------------------------------------
   public boolean isEmpty() // true if no links
   { return first == null; }
  
   // -------------------------------------------------------------
   public void insertFirst(long dd) // insert at front of list
   {
       Link newLink = new Link(dd); // make new link
      
       if( isEmpty() ) // if empty list,
           first = last = newLink; // newLink <-- last
       else
       {
           newLink.next = first; // newLink --> old first
           first = newLink; // first --> newLink
       }
   }
  
   // -------------------------------------------------------------
   public void insertLast(long dd) // insert at end of list
   {
       Link newLink = new Link(dd); // make new link
       if( isEmpty() ) // if empty list,
           first = last = newLink; // first --> newLink
       else
       {
           last.next = newLink; // old last --> newLink
           last = newLink; // newLink <-- last
       }
   }
  
   // -------------------------------------------------------------
   public long deleteFirst() // delete first link
   { // (assumes non-empty list)
       long temp = first.dData;
       if(first.next == null) // if only one item
           last = null; // null <-- last
       first = first.next; // first --> old next
       return temp;
   }
  
   // -------------------------------------------------------------
   public void displayList()
   {
       System.out.print("List (first-->last): ");
       Link current = first; // start at beginning
       while(current != null) // until end of list,
       {
           current.displayLink(); // print data
           current = current.next; // move to next link
       }
       System.out.println("");
   }
  
   // -------------------------------------------------------------
   public void join(FirstLastList otherList) {
       /*
       * This implementation does not empty otherList it still there holding
       * its memory space better use delete method
       *
       Link current = otherList.first; // start at beginning
       while (current != null) { // until end of list,
           this.insertLast(current.dData);
           current = current.next; // move to next link
       }
       otherList.first = null;
       */
       while(!otherList.isEmpty())
       {
           // remove first element from other list and append it to current list
           this.insertLast(otherList.deleteFirst());
       }
   }
  
   // -------------------------------------------------------------
   public void swap() throws Exception {
       /*
       * No need to count elements we can simply check for
       * First and Last to be equal for 1 element
       * and non null to be sure for elements >= 2
       *
       int count = 0;
       Link current = first; // start at beginning
       while (current != null) { // until end of list,
       count++;
       current = current.next; // move to next link
       }
       */
       if (first == null || first == last)
           throw new Exception("There is not enough element in the list.");
       else
       {
           long firstData = first.dData;
           long lastData = last.dData;
           first.dData = lastData;
           last.dData = firstData;
       }
   }
}

// inner class to represent link for list
class Link
{
   public long dData; // data item
   public Link next; // next link in list
  
   public Link(long d) // constructor
   {
       dData = d;
       // initialize next
       next = null;
   }
  
   public void displayLink() // display this link
   {
       System.out.print(dData + " ");
   }
} // end class Link

============================

FirstLastAppTest.java

============================

public class FirstLastAppTest
{
   public static void main(String[] args)
   {
       FirstLastList lst1 = new FirstLastList(); // Start a new FirstLastList called lst1
      
       lst1.insertLast(1); // Add links with data to the last position
       lst1.insertLast(3);
       lst1.insertLast(7);
       lst1.insertLast(4);
       System.out.print("\nlst1: "); // print the description for the list
       lst1.displayList(); // print the contents of the list
      
       FirstLastList lst2 = new FirstLastList(); // Start a new FirstLastList called lst2
      
       lst2.insertLast(2); // Add links with data to the last position
       lst2.insertLast(4);
       lst2.insertLast(5);
       lst2.insertLast(8);
       lst2.insertLast(6);
       System.out.print("\nlst2: "); // print the description for the list
       lst2.displayList(); // print the contents of the list
      
       System.out.print("\nlst1.join(lst2): "); // print the action to take place: lst1.join(lst2)
       lst1.join(lst2); // call the join method for lst1 to add lst2
       System.out.print("\nlst1: "); // print the description for the list
       lst1.displayList(); // print the contents of the list lst1; post join()
       System.out.print("lst2: "); // print the description for the list
       lst2.displayList(); // print the contents of the list lst2; post join()
      
       System.out.print("\nlst1.swap(): "); // print the action to take place: lst1.swap()
       try { // swap throws exception must include in try catch block
           lst1.swap();
       } catch (Exception e) {
           e.printStackTrace();
       }
       System.out.print("\nlst1: "); // print the description for the list
       lst1.displayList(); // print the contents of the list lst1; post swap()
   } // end main()
} // end class

=====================================================================


Related Solutions

1- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join...
1- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join them together to give one. So if the lists lst1= [1,3,7,4] and lst2=[2,4,5,8,6], the result of lst1.join(lst2) is lst1=[1,3,7,4,2,4,5,8,6] and lst2=[]. 2- Use FirstLastList: Write method public void swap(). It swaps the first and last elements of a FirstLastList. So if lst1= [1,3,7,4], lst1.swap() = [4,3,7,1]. Display or throw an exception if the list contains less than two elements. Demonstrate by displaying the list...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
2 a. Write a new method for the Greeter class,    public void swapNames(Greeter other) {...}...
2 a. Write a new method for the Greeter class,    public void swapNames(Greeter other) {...} that swaps the names of this greeter and another instance. b. write a new method for the Greeter class:     public Greeter createQualifiedGreeter(String qualifier) { ..... } that returns a new Greeter object with its name being the qualifier string followed by " " and the executing greeter's name (i.e. this.name). For example:    Greeter g = new Greeter("world");    Greeter g2 = g.createQualifiedGreeter("beautiful");...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling if L and M store the same sequence of elements (but perhaps with different starting points). Please provide a main() function with it in Java to test it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT