In Java - The code is fine. Need to output in decreasing order import java.util.
ID: 3835607 • Letter: I
Question
In Java - The code is fine. Need to output in decreasing order
import java.util.*;
//Create the class
public class SortedLinkedList<K,V>
{
//Create a private class
private class Entry
{
int key;
V value;
Entry link;
//Constructor
public Entry( int key, V value)
{
this.key = key;
this.value = value;
}
//Method to return the key and value of the elements
public String toString()
{
return "(Key:" + key + " Value:" + value +") ";
}
}
// instance variables for PqList
LinkedList<Entry> list = new LinkedList<Entry>();
Entry head;
int min = 0;
int temp = 0;
int num = 0;
//Provide the definition for insert method
public Entry insert( int key, V value )
{
Entry n = new Entry( key, value);
list.addFirst(n);
num++;
return list.getFirst();
}
//Method to find the minimum key from the list
public Entry min()
{
int i = 0;
min = list.get(0).key;
for(int j=1;j< list.size()-1;j++)
{
temp = list.get(j).key;
if(min < temp)
i = min;
else
i = temp;
}
return list.remove(i);
}
//Remove method to get the element which key value is minimum
public Entry removeMin()
{
if( list.size() == 0 )
System.out.println("The list is empty.");
Entry z = min();
num--;
return z;
}
public int size()
{
return num;
}
public boolean isEmpty()
{
return (num == 0);
}
}
//PqListTest.java:
//package SortedLinkedList;
//Create the class
class PqListTest
{
//Start the main method of the program
public static void main(String[] args)
{
//Create an object of the SortedLinkedList class
SortedLinkedList<Integer,Integer> list = new SortedLinkedList<Integer,Integer>();
//Insert the elements in the list.
list.insert(3, 23);
list.insert(1, 9);
list.insert(0, 34);
list.insert(2, 36);
System.out.println("Sorted Linked List: ");
//Print the list
while(!list.isEmpty())
{
System.out.print(list.removeMin());
}
}
}
Explanation / Answer
As you said logic is correct I made changes in PqListTest only (though I fixed one bug in min() method)
import java.util.*;
//Create the class
public class SortedLinkedList<K, V>
{
// Create a private class
private class Entry
{
int key;
V value;
Entry link;
// Constructor
public Entry(int key, V value)
{
this.key = key;
this.value = value;
}
// Method to return the key and value of the elements
public String toString()
{
return "(Key:" + key + " Value:" + value + ") ";
}
}
// instance variables for PqList
LinkedList<Entry> list = new LinkedList<Entry>();
Entry head;
int min = 0;
int temp = 0;
int num = 0;
// Provide the definition for insert method
public Entry insert(int key, V value)
{
Entry n = new Entry(key, value);
list.addFirst(n);
num++;
return list.getFirst();
}
// Method to find the minimum key from the list
public Entry min()
{
int i = 0;
min = list.get(0).key;
for (int j = 1; j < list.size() - 1; j++)
{
temp = list.get(j).key;
if (min > temp)
{
i = j;
min = temp;
}
}
return list.remove(i);
}
// Remove method to get the element which key value is minimum
public Entry removeMin()
{
if (list.size() == 0)
System.out.println("The list is empty.");
Entry z = min();
num--;
return z;
}
public int size()
{
return num;
}
public boolean isEmpty()
{
return (num == 0);
}
}
// PqListTest.java:
// package SortedLinkedList;
// Create the class
class PqListTest
{
// Start the main method of the program
public static void main(String[] args)
{
// Create an object of the SortedLinkedList class
SortedLinkedList<Integer, Integer> list = new SortedLinkedList<Integer, Integer>();
// Insert the elements in the list.
list.insert(3, 23);
list.insert(1, 9);
list.insert(0, 34);
list.insert(2, 36);
System.out.println("Sorted Linked List: ");
LinkedList decreasinglist = new LinkedList<>();
// Print the list
while (!list.isEmpty())
{
decreasinglist.add(list.removeMin());
}
for(int i = decreasinglist.size()-1; i >=0; i--)
{
System.out.print(decreasinglist.get(i));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.