Question

In: Computer Science

Add a sort instance method to the IntList class, so that x.sort() returns an IntList that...

Add a sort instance method to the IntList class, so that x.sort() returns an IntList that is a version of the IntList x, sorted in non-decreasing order. You may use any sorting algorithm you like. There should be no side effect on x.

//IntList Class

public class IntList {

private ConsCell start;

public IntList(ConsCell s) {

start = s;

}

public IntList cons (int h) {

return new IntList(new ConsCell(h,start));

}

public int length() {

int len = 0;

ConsCell cell = start;

while (cell != null) {

len++;

cell = cell.getTail();

}

return len;

}

public void print() {

System.out.print("[");

ConsCell a = start;

while (a != null){

System.out.print(a.getHead());

a = a.getTail();

if (a != null) System.out.print(",");

}

System.out.println("]");

}

public static void sort(int[] ConsCell) {

}

}

// ConsCell Class

/**

* A ConsCell is an element in a linked list of

* ints.

*/

public class ConsCell {

private int head;

private ConsCell tail;

public ConsCell(int h, ConsCell t) {

head = h;

tail = t;

}

public int getHead() {

return head;

}

public ConsCell getTail() {

return tail;

}

public void setTail(ConsCell t) {

tail = t;

}

}

//Main Class

public class Main {

public static void main(String args[]){

IntList a = new IntList(null);

IntList b = a.cons(2);

IntList c = b.cons(1);

IntList d = c.cons(3);

int x = a.length() + b.length() + c.length() + d.length();

a.print();

b.print();

c.print();

d.print();

System.out.println(x);

}

}

Solutions

Expert Solution

IntList class-

public class IntList {

    private ConsCell start;
    
    public IntList(ConsCell s) {
    
    start = s;
    
    }
    
    public IntList cons (int h) {
    
    return new IntList(new ConsCell(h,start));
    
    }
    
    public int length() {
    
    int len = 0;
    
    ConsCell cell = start;
    
    while (cell != null) {
    
    len++;
    
    cell = cell.getTail();
    
    }
    
    return len;
    
    }
    
    public void print() {
    
    System.out.print("[");
    
    ConsCell a = start;
    
    while (a != null){
    
    System.out.print(a.getHead());
    
    a = a.getTail();
    
    if (a != null) System.out.print(",");
    
    }
    
    System.out.println("]");
    
    }
    
    public static void sort(int[] ConsCell) {
    
    }

    public IntList sort(){

        // Make a duplicate copy of list which will be returned after sorting.
        ConsCell newStart = new ConsCell(start.getHead(), null);
        // temp element to iterate over list.
        ConsCell temp = start.getTail();
        // temp element at end of duplicate list.
        ConsCell temp2 = newStart;

        while(temp != null){
            ConsCell current = new ConsCell(temp.getHead(), null);
            temp2.setTail(current);

            temp2 = temp2.getTail();
            temp = temp.getTail();
            
        }

        IntList list = new IntList(newStart);
        int n = list.length();
        
        
        
        for(int i=0 ; i < n - 1 ; i++){
            ConsCell tempStart = newStart;
            for(int j = 0; j < n - i - 1 ; j++){
                ConsCell tempNext = tempStart.getTail();
                if(tempStart.getHead() > tempNext.getHead()){
                    int tempVal = tempStart.getHead();
                    tempStart.setHead(tempNext.getHead());
                    tempNext.setHead(tempVal);
                }
                tempStart = tempStart.getTail();
            }
        }
        
        return list;
        
    }
    
}
    
    

ConsCell class-

public class ConsCell {
    
    private int head;
    
    private ConsCell tail;
    
    public ConsCell(int h, ConsCell t) {
    
    head = h;
    
    tail = t;
    
    }
    
    public int getHead() {
    
    return head;
    
    }
    
    public ConsCell getTail() {
    
    return tail;
    
    }
    
    public void setTail(ConsCell t) {
    
    tail = t;
    
    }

    public void setHead(int h){
        head = h;
    }
    
    }

Main class -

import java.util.List;

public class IntList {

    private ConsCell start;
    
    public IntList(ConsCell s) {
    
    start = s;
    
    }
    
    public IntList cons (int h) {
    
    return new IntList(new ConsCell(h,start));
    
    }
    
    public int length() {
    
    int len = 0;
    
    ConsCell cell = start;
    
    while (cell != null) {
    
    len++;
    
    cell = cell.getTail();
    
    }
    
    return len;
    
    }
    
    public void print() {
    
    System.out.print("[");
    
    ConsCell a = start;
    
    while (a != null){
    
    System.out.print(a.getHead());
    
    a = a.getTail();
    
    if (a != null) System.out.print(",");
    
    }
    
    System.out.println("]");
    
    }
    
    public static void sort(int[] ConsCell) {
    
    }

    public IntList sort(){

        // To sort, we iterate n times over the list, and at each step we find the number which is just greater than
        // last element the list which we are building.

        ConsCell newStart = null;

        int n = length();

        ConsCell temp = start;
        
        // Set the smallest element to be the start of the new list.
        int last = start.getHead();

        while(temp != null){
            last = Math.min(last, temp.getHead());
            temp = temp.getTail();
        }

        newStart = new ConsCell(last, null);

        ConsCell tempTail = newStart;
        // Now we do n-1 iterations, and in each iteration we find the smallest number
        // that is larger than the tail of our new list and add the number at the end of list.
        for(int i=1;i<n;i++){
            last = tempTail.getHead();
            temp = start;
            int ceil = 10000000;

            while(temp != null){
                if(temp.getHead() > last && temp.getHead() < ceil ){
                    ceil = temp.getHead();
                }
                temp = temp.getTail();
            }

            // We need to attach ceil at the end of our list.

            ConsCell tail = new ConsCell(ceil, null);
            tempTail.setTail(tail);
            tempTail = tempTail.getTail();

        }
        // Return the new IntList with newStart as its start.
        return new IntList(newStart);
    }
    
}
    
    

Main class -

//Main Class
    
public class Main {
    
    public static void main(String args[]){
    
    IntList a = new IntList(null);
    
    IntList b = a.cons(2);
    
    IntList c = b.cons(1);
    
    IntList d = c.cons(100);
    
    d = d.cons(5);
    d = d.cons(12);
    d = d.cons(3);
    
    int x = a.length() + b.length() + c.length() + d.length();
    
    a.print();
    
    b.print();
    
    c.print();
    
    d.print();
    
    IntList sorted = d.sort();
    
    sorted.print();
    
    }
    
    }

The output is as follows -

The code has been commented for better readability. Please consider dropping an upvote to help out a struggling college kid :)

Happy Coding!!


Related Solutions

Add a method to OurQueue class that dequeues the Nth item on a queue and returns...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue. Test it with the following code: Console.WriteLine("Testing DequeueNth"); OurQueue<string> ourQ = new OurQueue<string>(12); // Empty Q try { ourQ.DequeueNth(0); Console.WriteLine("\a Error on empty list"); } catch { Console.WriteLine("Empty Queue worked"); } for (int i = 0; i < 9; ++i) ourQ.Enqueue("a" + i); for (int i =...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns the value of the highest key in the array, or a -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. 2.2) Modify the method in Programming project 2.1 so that the item with the highest key is not only returned by the method but also removed from the array....
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
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...
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT