In: Computer Science
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());
}
}
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