Question

In: Computer Science

Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...

Java Language

Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data.

Code:

class Node {

int value;

Node nextNode;

Node(int v, Node n)

{

value = v;

nextNode = n;

}

Node (int v)

{

this(v,null);

}

}

class LinkedList

{

Node head; //head = null;

LinkedList()

{

}

int length()

{

Node tempPtr;

int result = 0;

tempPtr = head;

while (tempPtr != null)

{

tempPtr = tempPtr.nextNode;

result = result + 1;

}

return(result);

}

void insertAt(int v, int position)

{

Node newNode = new Node(v,null);

Node tempPtr;

int tempPosition = 0;

if((head == null) || (position ==0))

{

newNode.nextNode = head;

head = newNode;

}

else {

tempPtr = head;

while((tempPtr.nextNode != null)&&(tempPosition < position -1))

{

tempPtr = tempPtr.nextNode;

tempPosition = tempPosition + 1;

}

if (tempPosition == (position - 1))

{

newNode.nextNode = tempPtr.nextNode;

tempPtr.nextNode = newNode;

}

}

}

public String toString()

{

Node tempPtr;

tempPtr = head;

String result = "";

while(tempPtr != null)

{

result = result + "[" + tempPtr.value + "| ]-->";

tempPtr = tempPtr.nextNode;

}

result = result + "null";

return result;

}

}

public class LinkedListDemoInsDel

{

public static void main(String[] args)

{

LinkedList aLinkedList = new LinkedList();

aLinkedList.insertAt(1,0);

aLinkedList.insertAt(9,1);

aLinkedList.insertAt(13,2);

aLinkedList.insertAt(8,1);

aLinkedList.insertAt(3,2);

System.out.println(aLinkedList);

System.out.println("Largo de lista: " + aLinkedList.length());

}

}

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------LinkedListDemoInsDel.java----------------

class Node {
   int value;
   Node nextNode;
   Node(int v, Node n)
   {
       value = v;
       nextNode = n;
   }

   Node(int v)
   {
       this(v, null);
   }
}

class LinkedList
{
   Node head; //head = null;

   LinkedList()
   {
   }

   int length()
   {
       Node tempPtr;
       int result = 0;
       tempPtr = head;
       while (tempPtr != null)
       {
           tempPtr = tempPtr.nextNode;
           result = result + 1;
       }
       return (result);
   }

   void insertAt(int v, int position)
   {
       Node newNode = new Node(v, null);
       Node tempPtr;
       int tempPosition = 0;
       if ((head == null) || (position == 0))
       {
           newNode.nextNode = head;
           head = newNode;
       } else {
           tempPtr = head;
           while ((tempPtr.nextNode != null) && (tempPosition<position - 1))
           {
               tempPtr = tempPtr.nextNode;
               tempPosition = tempPosition + 1;
           }
           if (tempPosition == (position - 1))
           {
               newNode.nextNode = tempPtr.nextNode;
               tempPtr.nextNode = newNode;
           }
       }
   }

   void deleteGreater(){                   //this function deletes the node with highest value
       if(head == null)                   //if list is empty, do nothing
           return;

       int max_value = head.value;           //assume head has max value
       Node tempPtr;
       tempPtr = head;              
       while (tempPtr != null){           //loop through the list
           if(tempPtr.value > max_value){   //find the node with the maximum value
               max_value = tempPtr.value;
           }
           tempPtr = tempPtr.nextNode;
       }
       System.out.println("\nDeleting node with value: " + max_value);

       if(head.value == max_value){       //if head has maximum value, then update the head to next node
           head = head.nextNode;   
           return;
       }

       tempPtr = head;                       //loop until you reach one node before the max value node
       while (tempPtr != null && tempPtr.nextNode.value != max_value){
           tempPtr = tempPtr.nextNode;      
       }
       tempPtr.nextNode = tempPtr.nextNode.nextNode;   //set the link of prev node to max node's next
   }

   public String toString()
   {
       Node tempPtr;
       tempPtr = head;
       String result = "";
       while (tempPtr != null)
       {
           result = result + "[" + tempPtr.value + "| ]-->";
           tempPtr = tempPtr.nextNode;
       }
       result = result + "null";
       return result;
   }
}

public class LinkedListDemoInsDel
{
   public static void main(String[] args)
   {
       LinkedList aLinkedList = new LinkedList();
       aLinkedList.insertAt(1, 0);
       aLinkedList.insertAt(9, 1);
       aLinkedList.insertAt(13, 2);
       aLinkedList.insertAt(8, 1);
       aLinkedList.insertAt(3, 2);
       System.out.println(aLinkedList);
       System.out.println("Largo de lista: " + aLinkedList.length());

       //Testing
       aLinkedList.deleteGreater();
       System.out.println(aLinkedList);
      
       aLinkedList.deleteGreater();
       System.out.println(aLinkedList);
      
       aLinkedList.deleteGreater();
       System.out.println(aLinkedList);
  
       aLinkedList.deleteGreater();
       System.out.println(aLinkedList);
      
       aLinkedList.deleteGreater();
       System.out.println(aLinkedList);
      
       aLinkedList.deleteGreater();
       System.out.println(aLinkedList);
   }
}

--------------Screenshots-------------------

------------Output----------------------

------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the...
Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the LinkedList. 2. (2 pts) Create the LinkedList using the ListNode class. 3. (4 pts) Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. 4. (2 pts) Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy...
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy node”) that connects the end of the list with the beginning, transforming the list to a circular list Code in c++ The Josephus problem is named after the historian Flavius Josephus, who lived between the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against the Roman Empire. When it appeared that Josephus and his band were to be...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Java Language Add a recursive method to the program shown in the previous section that states...
Java Language Add a recursive method to the program shown in the previous section that states how many nodes does the stack have. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT