Question

In: Computer Science

Add a new method called insert_in_order() which accepts a number as its parameter and inserts that...

Add a new method called insert_in_order() which accepts a number as its parameter and inserts that number in the linked list in an ascending (sorted) order. Note that this method should be called for all the numbers (1, 5, 19, 7, 23, 17, 2) and the resulting linked list should come out sorted. (1, 2, 5, 7, 17, 19, 23)

JAVA CODE BELOW

class aNode {

char data;
aNode next;

aNode(char mydata) { // Constructor
data = mydata;
next = null;
}
};
//-----------------------------------------------------

class linkedList {

aNode head; // Head of the linked list
int size;

linkedList() { // Constructor
head = null;
size = 0;
}
//-----------------------------------------------------

public void insert_at_beginning(char value) {
aNode newNode = new aNode(value); // create aNew node
newNode.next = head;
head = newNode;
size++;
}
//-----------------------------------------------------

public void insert_at_end(char value) {
aNode newNode = new aNode(value); // create aNew node
if (isEmpty()) {
newNode.next = head;
head = newNode;
size++;
} else {
//find the last node
aNode ptr;
ptr = head;
while (ptr.next != null) {
ptr = ptr.next;
}
ptr.next = newNode; //add the node to the end
size++;
}
}
//-----------------------------------------------------

public void insert_after(char value, char searchValue) {
if (isEmpty()) {
System.out.println("Linked List is empty, no way to insert " + value + " after " + searchValue);
} else {
//find the node with searchValue
aNode ptr;
boolean found = false;
ptr = head;
while (ptr != null && found == false) {
if (ptr.data == searchValue) {
found = true;
} else {
ptr = ptr.next;
}
}
if (ptr == null) {
System.out.println("Did not find " + searchValue + "Nothing Inserted");
} else {
aNode newNode = new aNode(value); // create aNew node
newNode.next = ptr.next;
ptr.next = newNode; //add the node after the searchValue
size++;
}
}
}
//-----------------------------------------------------
// Delete the first node with the value

public void delete(char deleteValue) {
if (isEmpty()) {
System.out.println("Linked List is empty, nothing to delete");
} else {
aNode deletePtr = head; // create a reference to head
if (head.data == deleteValue) {
head = head.next; // remove the head
deletePtr = null; // make the node available for garbage collection.
size--;
} else {
aNode prevPtr;
deletePtr = prevPtr = head;
boolean found = false;
//find the value to be deleted
while (deletePtr != null && found == false) {
if (deletePtr.data == deleteValue) { // Read about the difference between == and .equals()
found = true;
prevPtr.next = deletePtr.next;
deletePtr = null; // make deletePtr available to garbage collection
size--;
} else {
prevPtr = deletePtr;
deletePtr = deletePtr.next;
}
}
if (found == false) {
System.out.println("Not able to find/delete " + deleteValue + " in the Linked List");
}
}
}
}
//-----------------------------------------------------

public boolean isEmpty() {
if (head == null) {
return (true);
} else {
return (false);
}
}
//-----------------------------------------------------

public void print() {
aNode ptr;
ptr = head;
System.out.print("Head--> ");
while (ptr != null) {
System.out.print(ptr.data + " --> ");
ptr = ptr.next;
}
System.out.println("NULL");
}
//-----------------------------------------------------

public int getSize() {
return (size);
}
//-----------------------------------------------------

public void freeAll() {

aNode 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;

Solutions

Expert Solution

Code After Modification:

Node Class Code:

class aNode {

        int data;
        aNode next;

        aNode(int mydata) { // Constructor
                data = mydata;
                next = null;
        }
};

linkedList Class Code:


//-----------------------------------------------------

class linkedList {

        aNode head; // Head of the linked list
        int size;

        linkedList() { // Constructor
                head = null;
                size = 0;
        }
        //-----------------------------------------------------

        public void insert_at_beginning(int value) {
                aNode newNode = new aNode(value); // create aNew node
                newNode.next = head;
                head = newNode;
                size++;
        }
        //-----------------------------------------------------

        public void insert_at_end(int value) {
                aNode newNode = new aNode(value); // create aNew node
                if (isEmpty()) {
                        newNode.next = head;
                        head = newNode;
                        size++;
                } else {
                        //find the last node
                        aNode ptr;
                        ptr = head;
                        while (ptr.next != null) {
                                ptr = ptr.next;
                        }
                        ptr.next = newNode; // add the node to the end
                        size++;
                }
        }
//-----------------------------------------------------

        public void insert_after(int value, int searchValue) {
                if (isEmpty()) {
                        System.out.println("Linked List is empty, no way to insert " + value + " after " + searchValue);
                } else {
//find the node with searchValue
                        aNode ptr;
                        boolean found = false;
                        ptr = head;
                        while (ptr != null && found == false) {
                                if (ptr.data == searchValue) {
                                        found = true;
                                } else {
                                        ptr = ptr.next;
                                }
                        }
                        if (ptr == null) {
                                System.out.println("Did not find " + searchValue + "Nothing Inserted");
                        } else {
                                aNode newNode = new aNode(value); // create aNew node
                                newNode.next = ptr.next;
                                ptr.next = newNode; // add the node after the searchValue
                                size++;
                        }
                }
        }
        //-----------------------------------------------------
        // Delete the first node with the value

        public void delete(int deleteValue) {
                if (isEmpty()) {
                        System.out.println("Linked List is empty, nothing to delete");
                } else {
                        aNode deletePtr = head; // create a reference to head
                        if (head.data == deleteValue) {
                                head = head.next; // remove the head
                                deletePtr = null; // make the node available for garbage collection.
                                size--;
                        } else {
                                aNode prevPtr;
                                deletePtr = prevPtr = head;
                                boolean found = false;
                                //find the value to be deleted
                                while (deletePtr != null && found == false) {
                                        if (deletePtr.data == deleteValue) { // Read about the difference between == and .equals()
                                                found = true;
                                                prevPtr.next = deletePtr.next;
                                                deletePtr = null; // make deletePtr available to garbage collection
                                                size--;
                                        } else {
                                                prevPtr = deletePtr;
                                                deletePtr = deletePtr.next;
                                        }
                                }
                                if (found == false) {
                                        System.out.println("Not able to find/delete " + deleteValue + " in the Linked List");
                                }
                        }
                }
        }
        //-----------------------------------------------------

        public boolean isEmpty() {
                if (head == null) {
                        return (true);
                } else {
                        return (false);
                }
        }
        //-----------------------------------------------------

        public void print() {
                aNode ptr;
                ptr = head;
                System.out.print("Head--> ");
                while (ptr != null) {
                        System.out.print(ptr.data + " --> ");
                        ptr = ptr.next;
                }
                System.out.println("NULL");
        }
        //-----------------------------------------------------

        public int getSize() {
                return (size);
        }
        //-----------------------------------------------------

        public void freeAll() {

                aNode 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;
        }
        
        /*
         *  insert_in_order() which accepts a number as its parameter and 
         *  inserts that number in the linked list in an ascending (sorted) order
         */
        
        //NOTE:To insert numbers into list we need to change type of data from char to int in class aNode
        public void insert_in_order(int n)
        {
                aNode newNode=new aNode(n);
                /* Special case for head node */
                if(head==null ||  head.data>=newNode.data)
                {
                        newNode.next=head;
                        head=newNode;
                        size++;
                        return;
                }
                
                /* Locate the node before point of insertion. */
                aNode current=head;
                while(current.next!=null && current.next.data<newNode.data)
                {
                        current=current.next;
                }
                newNode.next=current.next;
                current.next=newNode;
                size++;
        }
        
        
        //Driver(Main method)
        public static void main(String[] args) {
                
                //create linkedList Object
                linkedList list=new linkedList();
                //values to be inserted into list
                int values[]= {1, 5, 19, 7, 23, 17, 2};
                
                //insert all values into list using insert_in_order method
                for(int i=0;i<values.length;i++)
                {
                        list.insert_in_order(values[i]);
                }
                
                //print the list
                list.print();
        }
}

Output Of Code:

Head--> 1 --> 2 --> 5 --> 7 --> 17 --> 19 --> 23 --> NULL

Useful Image Of Code:


Related Solutions

Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT