Page 1 of 1

Specification of your program 1. Your program should be named as: Merge.java The merging process described in this assig

Posted: Sun May 15, 2022 8:22 am
by answerhappygod
Specification of your program
1. Your program should be named as: Merge.java
The merging process described in this assignment is trivial and
slow. There exists an order of magnitudes faster method for
this merge process by using the priority queue data
structure.
2. The input and output of your program.
The input are a number of files, each of which contains a sequence
of integers, one integer per line,
in ascending order. Your program should be working for any number
(≥ 2) of input files. The
number of command line parameters is available as
args.length.
The output is the screen print of the sorted (in ascending order)
entire data set obtained from merging
all the integers in the input files.
An example
Suppose we supply to your program with three files named data1.txt,
data2.txt, and data3.txt,
with the following content:
4 1 2
6 3 5
7 5 9
9 6
8
data1.txt data2.txt data3.txt
Then, the command line you should type would be:
$java Merge data1.txt data2.txt data3.txt
Then your program will merge all the integers from the three sorted
files and print them on the screen:
1
2
3
4
5
5
6
6
7
8
9
9
The merge process and its implementation
Suppose we are given k files F1, F2, . . . , Fk, for some k ≥ 2,
i.e., k = args.length. Each contains a
sorted sequence of integers, one per line in the ascending order.
The number of integers in all these files do
NOT have to be the same. We want to merge the integers from all
these files, so that all the integers after
merging are in ascending order. The following pseudocode shows a
trivial way for doing this merge process
and you are required to implement it.
Note: The capacity of the array-based FIFO queue in the
code below must be set to be 10 in order to give
yourself the chance to play with the enqueue and dequeue operations
alternatively during the run of your
program.
Note: Please implement the Queue data structure. The
linked list should be able to implement the enqueue method to add a
node at tail position, and the dequeue method should remove a node
at the head position.
The code I have so far:
public class Queue {
private class Node {
private Object data;
private Node next;
public Node (Object elem) {
this.data = elem;
this.next = null;
}
private Node head, tail;
private int size;
public Queue() {
this.head = null;
this.tail = null;
this.size = 0;
}
public void enque(Object elem) {
Node nn = new Node(elem);
if(this.size == 0) {
head = nn;
tail = nn;
}
else {
tail.next = nn;
tail = nn;
}
this.size ++;
}
public Object deque() throws Exception {
if(size == 0)
throw new Exception("Queue is empty");
Object temp = head.data;
this.head = this.head.next;
this.size --;
if(this.size == 0)
this.tail = null;
return temp;
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.io.FileNotFoundException;
public class Merge {
public static void main(String[] args) {
int length = args.length;
for(int i = 0; i < length; i++){
File file = new File(args);
if(!file.exists()) {
System.out.println("The file you entered is missing or not
there.");
}
LinkedList i = new LinkedList();

try {
String f;
BufferedReader br = new BufferedReader(new FileReader(file));
while((f = data.readLine()) != null) {
Integer.parseInt(f);
args.enque(f);
}
}
catch(IOException ex) {
System.out.print(ex);
}
catch(FileNotFoundException ex) {
System.out.print(ex);
}
}
public class LinkedList {
private class Node {
private Object data;
private Node next, prev;
private Node(Object data, Node pref, Node next)
{
this.data = data;
this.prev = pref;
this.next = next;
}
}
private Node head;
private Node Tail;
private int size;
public LinkedList() {
this.head = new Node(null, null, null );
this.head.next = this.head;
this.head.prev = this.head;
this.size = 0;
}
public void isEmpty() {
return (this.size == 0);
}

public void addFirst(Object data) {
Node nn = new Node(data, this.head, this.head.next);
this.head.next.prev = nn;
this.head.next = nn;
this.size ++;
}
public int MergeSort() {
Queue q = new Queue();
for(Node D = this.head; D != null; D = D.next) {
LinkedList newList = new LinkedList();
newList.addFirst(D);
q.enque(newList);
this.size++;
}
LinkedList subList1 = new LinkedList();
LinkedList subList2 = new LinkedList();
while(!q.isEmpty) {
subList1 = q.deque();
subList2 = q.deque();
this.size--;
}
LinkedList tempList = merge(subList1, subList2);
q.enque(tempList);
return tempList;
}
public void sizeOf(LinkedList A) {
return A.length;
}


@Override
public String toString() {
String result = "{";
for (Node node = this.head.next; node != this.head; node =
node.next) {
if(node.next != this.head)
result += node.data + "->";
else
result += node.data;
}
return result + "}";
}
}