Question

JAVA ANSWER IN JAVA PLEASE.. ReadFile Create a linked list from an input file (input.txt) that...

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)  

           

Homework Answers

Answer #1

// 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());

    }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
def array_to_list(data): ''' Converts an array to linked list with the same order as in array...
def array_to_list(data): ''' Converts an array to linked list with the same order as in array returns None if array is empty ''' head = None # for each value in array for val in data: # if list is empty then create a new head node if head is None: head = list_node.ListNode(val) temp = head # else create a new node and add it to the list else: temp.next = list_node.ListNode(val) temp = temp.next return head def pair(list1,...
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...
I am having some trouble trying to create a linked list from a text file input...
I am having some trouble trying to create a linked list from a text file input from user The three lines respectively represent a long integer ID of a person, the name of the person, and an integer ranging from 0 to 5 indicating the threat level posed by a person. 389114 Paul Bunion 5 399012 John Doe 0 685015 Johnny Appleseed 3 179318 Tom Sawyer 2 284139 Ebenezer Scrooge 5 The full connection is: Paul Bunion -> John Doe...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
Adding large numbers with linked list Requirement - in C++ - use file for the input...
Adding large numbers with linked list Requirement - in C++ - use file for the input (nums.txt) - (recommended) use only one linked list to hold intermediate answer and final answer. You may use another one to reverse the answer. - store the num reversely in the linked list. For example, the num 123 is stored as 3 (at first node), 2 (at second node) and 1 (at third node) in the linked list. - write a function that performs...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT