Question

In: Computer Science

Add your own method (or use one or more of the existing methods) to insert the...

Add your own method (or use one or more of the existing methods) to insert the following set of numbers (1, 5, 19, 7, 23, 17, 2) in a linked list (use one function-call per number, and preserve the order of insertion). Once inserted print the linked list such that the output displays the numbers in reverse order. (2, 17, 23, 7, 19, 5, 1)

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

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

LinkListTest.java

package c12;

class aNode {

   int data;
   aNode next;

   aNode(int 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(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 void printReverse() {
       System.out.print("Tail --> ");
       aNode ptr;
       ptr = head;
       printReverseHelper(ptr);
       System.out.print("Head");
   }

   public void printReverseHelper(aNode ptr2) {
       if(ptr2==null) {
           return ;
       }else {
           printReverseHelper(ptr2.next);
           System.out.print(ptr2.data + " --> ");
       }
   }

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

public class LinkListTest{
   public static void main(String[] args) {
       linkedList list = new linkedList();
       list.insert_at_end(1);
       list.insert_at_end(5);
       list.insert_at_end(19);
       list.insert_at_end(7);
       list.insert_at_end(23);
       list.insert_at_end(17);
       list.insert_at_end(2);
       System.out.println("Link list before : ");
       list.print();

       System.out.println("Link list in reverse order : ");
       list.printReverse();
   }
}


Related Solutions

code the following methods: one returning the average weekly hours and a method to add a...
code the following methods: one returning the average weekly hours and a method to add a weeks hours to the array of hours (this means creating a larger array).
ON PYTHON Exercise 4. Insert one or more conditional statements at the end of the source...
ON PYTHON Exercise 4. Insert one or more conditional statements at the end of the source code listed in exercise4.py that prints the messages ‘You entered a negative number’, ‘You entered a zero’, or ‘You entered a positive number’ based on the value entered by the user. value = int( input( 'Enter the value: ') ) # insert the additional statement(s) here Exercise 5. Insert one or more conditional statements at the end of the source code listed in exercise5.py...
List and describe the four methods of surveying sample population, use your own definition, and provide...
List and describe the four methods of surveying sample population, use your own definition, and provide an example for each.
Use the scientific method to come up with, and create your own testable experiment. The experiment...
Use the scientific method to come up with, and create your own testable experiment. The experiment must involve something dealing with cancer, and it must be testable in a lab setting. Remember to include illustrations on how the experiment can be tester. Be sure to include and label your "Observation", "Hypothesis","Experiment", Data & Analyis", & "Conclusion".
a) In your own words, explain the concept of variable scope. Include one or more code...
a) In your own words, explain the concept of variable scope. Include one or more code fragments that you write for this discussion post to help illustrate your case. Clearly mark as appropriate any code fragments as either "script" code or "function" code, and use comments in your code to note the locality of each variable at least the first time the variable appears in the script or function. If a variable is local in scope, be sure to notate...
in 300 hundred words or more, please write, in your own words, about one of the...
in 300 hundred words or more, please write, in your own words, about one of the entities that establish the rules accountants must follow when preparing financial reports. they include, but are not limited to, the Financial Accounting Standards Board (FASB), International Accounting Standards Board (IASB), and the Security Exchange Commission (SEC). please also state your opinion wether the mission of the organization you are writing on helps to provide guidance to aacountants or is repetitive of other standard-making bodies....
DIFFERENTIAL EQUATIONS Situation Describe in your own words the method you would use to find the...
DIFFERENTIAL EQUATIONS Situation Describe in your own words the method you would use to find the Laplace transform of the first derivative, that is,. Give 2 examples. Situacion: Describa en sus propias palabras el método que usted utilizaría para hallar la transformada de Laplace de la primera derivada, o sea, . De 2 ejemplos.
Chapter 9 project 1 & 2(these are combined into one project) : Add methods to the...
Chapter 9 project 1 & 2(these are combined into one project) : Add methods to the Student class that compare two Student objects. One method should test for equality. The other methods should support the other possible comparisons. In each case, the method returns the result of the comparison of the two students' names. Place several Student objects into a list and shuffle it. Then run the sort method with this list and display all of the students' information. I...
1. Make an assertion of your own, add a colon, and follow it with a quotation...
1. Make an assertion of your own, add a colon, and follow it with a quotation in a complete sentence.    2. Create an introductory or closing phrase or clause, and attach a quotation to it with a comma.    3. Work some brief excerpts of quoted material into an assertion of your own. Instructions: Bearing these three principles in mind, read the ten examples of quotations listed below, all of which are full of various mistakes. Identify the mistakes...
On your own words, talk briefly about two of the inventory costing methods, give one example...
On your own words, talk briefly about two of the inventory costing methods, give one example of each method.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT