Question
Create-a program that reads a file containing a list of students (first names only). The names must be put in sorted order within the linked
Create-a program that reads a file containing a list of students (first names only). The names must be put in sorted order within the linked list even if they may be in an unsorted order in the file.
A doubly linked list should be used by the code.
The name of the student, a pointer to the following student, and a pointer to the preceding student should all be present in each node of the doubly linked list. Here is an illustration. The list's starting point is indicated by the head. The list's conclusion is indicated by the tail.
When inserting consider all the following conditions: if(!head){ //no other nodes }else if (strcmp(data, head->name)<0){ //smaller than head }else if (strcmp(data, tail->name)>0){ //larger than tail }else{ //somewhere in the middle }
Consider each of the following situations before eliminating a student: Student might be in the middle, at the tail, or at the head.
You may see an example of the file's appearance below. Although the information in the linked list (above visual) is sorted, the names are shown in alphabetical order. The file should be called "input.txt." The term delete followed by a name in the text file should remove the node from the doubly linked list that contains the name of that particular student. Nothing is erased if the name cannot be located. (PLEASE NOTE: The image above only shows the first three lines of the text file below.)
Jim jill John delete jill Bob Jack delete jim
At the conclusion of the program, use the doubly linked list to iterate through the linked list's items in both ascending and descending order, and then save the results to the file output.txt. Here is an example display using the list above as a reference:
Bob Jack John ============= John Jack Bob
Import Please follow this code and complete the code
import java.util.*; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream;
public class Main{ public static void main(String[] args)throws FileNotFoundException { Scanner file = new Scanner(new File("input.txt")); Doubly list = new Doubly(); // if file has input while(file.hasNextLine()){ String name = file.next(); if(name.equals("delete")){ name = file.next(); list.delete(name); }else{ list.insert(name); } //weite to put PrintStream output = new PrintStream(new File("output.txt")); System.setOut(output); list.traverseAscending(); System.out.println("============="); list.traverseDescending(); System.setOut(output); } } }
class Node{ private String data; private Node next; private Node prev; //constructor for class public Node(String data){ this.data=data; } public String getData(){ } public void setData(String data){ } public Node getNext(){ } public void setNext(Node next){ } public Node getprev(){ } public void setPrrev(Node prev){ this.prev=prev; } }
class Doubly{ public void delete(String name){ } public void traverseDescending(){ } public void traverseAscending(){ } public void insert(String name){ } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started