package csci3230.hw4; import java.io.File; import java.io.IOException; import ja
ID: 3848418 • Letter: P
Question
package csci3230.hw4;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
public class PQList extends LinkedList<Integer>{
private static final long serialVersionUID = 1L;
LinkedList<Integer> list = new LinkedList<Integer>();
public void insert(Integer key){
// your code goes here
}
public Integer removeMin() {
if(!list.isEmpty()){
return list.removeFirst();
}else{
return null;
}
// your code goes here
}
public String toString() {
StringBuilder s = new StringBuilder();
for (Integer item : list)
s.append(item + " ");
return s.toString();
}
public static void main(String[] args) throws IOException {
int n, seed;
File outputFile;
File dir = new File(".");
if (args.length > 0) {
n = Integer.parseInt(args[0]);
seed = Integer.parseInt(args[1]);
}else {
n = 10;
seed = 3;
}
if (args.length == 3) {
outputFile = new File(args[2]);
} else {
outputFile = new File(dir.getCanonicalPath() + File.separator + "Files/testOut_PQ");
}
OutputWriter out = new OutputWriter(outputFile);
Random r = new Random(seed);
PQList pq = new PQList();
for (int i = 0; i < n; i++) {
pq.insert(r.nextInt(100));
}
out.writeOutput("Number of Integer items as input: "+ n);
out.writeOutput(pq.toString());
out.writeOutput("");
out.writeOutput("Sorted output using Selection sort:");
for (int i = 0; i < n; i++) {
Integer min = pq.removeMin();
out.writeOutput(min.toString());
}
}
}
Output:
Number of Integer items as input: 10
34 60 10 81 28 2 49 64 59 61
Sorted output using Selection sort:
34
60
10
81
28
2
49
64
59
61
Im having trouble writing my insert() method. Can you help me with with my insert method and can you check my removeMin() method. The methods are suppose to be implemented such that it implements selection sort (ascending)in the main method.
Explanation / Answer
Hi, I have implemented. Plesae let me know in case of any issue.
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
public class PQList extends LinkedList<Integer>{
private static final long serialVersionUID = 1L;
LinkedList<Integer> list = new LinkedList<Integer>();
public void insert(Integer key){
// searching key in list
int pos = Collections.binarySearch(this, key);
// if it is not in list
if (pos < 0) {
add(-pos-1, key);
}else{ // if it is in list
add(pos, key);
}
}
public Integer removeMin() {
if(!list.isEmpty()){
return list.removeFirst();
}else{
return null;
}
// your code goes here
}
public String toString() {
StringBuilder s = new StringBuilder();
for (Integer item : list)
s.append(item + " ");
return s.toString();
}
public static void main(String[] args) throws IOException {
int n, seed;
File outputFile;
File dir = new File(".");
if (args.length > 0) {
n = Integer.parseInt(args[0]);
seed = Integer.parseInt(args[1]);
}else {
n = 10;
seed = 3;
}
if (args.length == 3) {
outputFile = new File(args[2]);
} else {
outputFile = new File(dir.getCanonicalPath() + File.separator + "Files/testOut_PQ");
}
OutputWriter out = new OutputWriter(outputFile);
Random r = new Random(seed);
PQList pq = new PQList();
for (int i = 0; i < n; i++) {
pq.insert(r.nextInt(100));
}
out.writeOutput("Number of Integer items as input: "+ n);
out.writeOutput(pq.toString());
out.writeOutput("");
out.writeOutput("Sorted output using Selection sort:");
for (int i = 0; i < n; i++) {
Integer min = pq.removeMin();
out.writeOutput(min.toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.