JAVA
ANSWER IN JAVA PLEASE..
ReadFile
Create a linked list from an input file (input.txt) that contains an even number of first names. The number of items in the file is unknown.
SplitMerge
Create a split function that divides the newly created linked list into two equal sublists: myList1 and myList2. For example, originally you would point to (John, Jack, Jill, Jim). After the split, myList1 would point to john and jack and myList2 would point to jill and Jim.
Traverse
Accepts a pointer and displays each of the lists (myList1 and myList2 on the screen) Note: the traverse function takes only one argument, so the function would have to be called twice, once for each of the lists.
Merge
Feed the pointer variables myList1 and myList2 into the Merge function. Create a single list. Return the list.
Traverse
Pass in the pointer variable and display the returned list from the Merge function
Summary:
You need to write at least 5 functions: main, readFile, splitMerge, merge and traverse. The inputs will come from one input files (input.txt). There is no output file. The program should be in a single .cpp file.
From within main call
ReadFile (Read the contents of file, create a linked list and return the list to main)
SplitAndMerge (pass list, splits into list 1 and list 2, void return)
From within splitMerge
call Traverse (Display contents of list1)
call Traverse (Display contents of list2)
call Merge (Pass list1 and list2, merge and return one list)
call Traverse (Display contents of list coming back from Merge)
// Java Program to illustrate reading from FileReader
// using BufferedReader
import java.io.*;
public class ReadFromFile2
{
public static void main(String[] args)throws Exception
{
// We need to provide file path as the parameter:
// double backquote is to avoid compiler interpret words
// like \test as \t (ie. as a escape sequence)
File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
System.out.println(st);
}
}
----------------
// Java program to merge two
// files into third file
import java.io.*;
public class FileMerge
{
public static void main(String[] args) throws IOException
{
// PrintWriter object for file3.txt
PrintWriter pw = new PrintWriter("file3.txt");
// BufferedReader object for file1.txt
BufferedReader br = new BufferedReader(new FileReader("file1.txt"));
String line = br.readLine();
// loop to copy each line of
// file1.txt to file3.txt
while (line != null)
{
pw.println(line);
line = br.readLine();
}
br = new BufferedReader(new FileReader("file2.txt"));
line = br.readLine();
// loop to copy each line of
// file2.txt to file3.txt
while(line != null)
{
pw.println(line);
line = br.readLine();
}
pw.flush();
// closing resources
br.close();
pw.close();
System.out.println("Merged file1.txt and file2.txt into file3.txt");
}
}
=================
// Java program to demonstrate iteration over -Traverse
// HashSet using an iterator
import java.util.*;
class IterationDemo {
public static void main(String[] args)
{
HashSet<String> h = new HashSet<String>();
// Adding elements into HashSet usind add()
h.add("Geeks");
h.add("for");
h.add("Geeks");
// Iterating over hash set items
Iterator<String> i = h.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
Get Answers For Free
Most questions answered within 1 hours.