Question

Help in JAVA please Write a program that reads a list of students (first names only)...

Help in JAVA please

Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in unsorted order in the file but they have to be placed in sorted order within the linked list.

The program should use a doubly linked list.

Each node in the doubly linked list should have the student’s name, a pointer to the next student, and a pointer to the previous student. Here is a sample visual. The head points to the beginning of the list. The tail points to the end of the list.

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

}

When deleting a student consider all the following conditions:

student may be at the head, the tail or in the middle

Below, you will find a sample of what the file looks like. Notice the names are in unsorted order but the information placed in the linked list (above visual) is in sorted order. The name of the file should be “input.txt”.

In the text file, the word delete followed by a name, should delete the node with that specific student’s name from the doubly linked list. If the name is not found, then nothing is deleted.

(NOTE: The above visual represents only the first three lines from the text file below.)

Jim

jill   

John

delete jill

Bob

Jack   

delete jim

At the end of the program, traverse through the contents of the linked list in both ascending and descending order, using the doubly linked list, and write the contents into the file output.txt. For example, given the above list, here is the sample display:

Bob

Jack

John

=============

John

Jack   

Bob   
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// DoublyLinkedList.java

public class DoublyLinkedList {

       // inner class DNode to represent a node in doubly linked list

       class DNode

       {

             private String name; // data

             private DNode prev; // pointer to previous node

             private DNode next; // pointer to next node

            

             // constructor that create a node with given name and pointers to prev and next is null

             public DNode(String name)

             {

                    this.name = name;

             }

            

       }

      

       private DNode head; // pointing to start of the list

       private DNode tail; // pointing to end of the list

      

       // constructor that creates an empty list

       public DoublyLinkedList()

       {

             head = null;

             tail = null;

       }

      

       // method to insert name in list in sorted order

       public void insert(String name)

       {

             DNode node = new DNode(name);

             if(head == null) // if empty list, insert as head node

             {

                    head = node;

                    tail = node;

             }else if(head.name.toLowerCase().compareTo(name.toLowerCase()) > 0) // if data in head > name then update head

             {

                    node.next = head;

                    head.prev = node;

                    head = node;

             }else if(name.toLowerCase().compareTo(tail.name.toLowerCase()) > 0) // if data in tail < name then update tail

             {

                    tail.next = node;

                    node.prev = tail;

                    tail = node;

             }else

             {

                    // insert in middle

                    DNode curr = head;

                    // loop to insert the node in the middle of the list

                    while(curr != null)

                    {

                           if(curr.name.toLowerCase().compareTo(name.toLowerCase()) > 0)

                           {

                                 curr.prev.next = node;

                                 node.prev = curr.prev;

                                 node.next = curr;

                                 curr.prev = node;

                                 return ;

                           }

                          

                           curr = curr.next;

                    }

             }

            

       }

      

       // method to remove a name from the list

       public void remove(String name)

       {

             // if list is empty, do nothing

             if(head != null)

             {

                    if(head.name.toLowerCase().compareTo(name.toLowerCase()) == 0) // if head is the node to be deleted update head

                    {

                           head = head.next;

                           if(head == null)

                                 tail = null;

                           else

                                 head.prev = null;

                          

                    }else

                    {

                           // delete the node in the middle or end

                           DNode curr = head;

                           while(curr != null)

                           {

                                  if(curr.name.toLowerCase().compareTo(name.toLowerCase()) == 0)

                                 {

                                        curr.prev.next = curr.next;

                                        if(curr.next != null)

                                        {

                                               curr.next.prev = curr.prev;

                                        }else

                                        {

                                               tail = curr.prev;

                                        }

                                       

                                        return;

                                 }

                                

                                 curr = curr.next;

                           }

                    }

             }

       }

      

       // method to display the list in ascending order

       public void display()

       {

             if(head == null)

                    System.out.println("List is empty");

             else

             {

                    DNode curr = head;

                   

                    while(curr != null)

                    {

                           System.out.println(curr.name);

                           curr = curr.next;

                    }

             }

       }

      

       // method to display the list in descending order

       public void displayReverse()

       {

             if(head == null)

                    System.out.println("List is empty");

             else

             {

                    DNode curr = tail;

                   

                    while(curr != null)

                    {

                           System.out.println(curr.name);

                           curr = curr.prev;

                    }

             }

       }

      

}

//end of DoublyLinkedList.java

// DoublyLinkedListDriver.java : Java program to test the DoublyLinkedList class

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class DoublyLinkedListDriver {

       public static void main(String[] args) throws FileNotFoundException {

             // test the class

             Scanner scan = new Scanner(new File("input.txt")); // open the file input.txt

            

             DoublyLinkedList list = new DoublyLinkedList();

            

             String name;

             while(scan.hasNext())

             {

                    name = scan.next();

                   

                    if(name.equalsIgnoreCase("delete"))

                    {

                           name = scan.next();

                           list.remove(name);

                    }else

                           list.insert(name);

             }

            

             scan.close();

             System.out.println("List in ascending order : ");

             list.display();

             System.out.println("List in descending order : ");

             list.displayReverse();

       }

}

//end of DoublyLinkedListDriver.java

Output:

Input file:

Output:

Add a comment
Know the answer?
Add Answer to:
Help in JAVA please Write a program that reads a list of students (first names only)...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Write a c/c++ program to read a list of students from a file and create a...

    Write a c/c++ program to read a list of students from a file and create a list. The program should use a linked list for implementation. Each node in the linked list should have the student’s name, a pointer to the next student, and a pointer to a linked list of scores. There may be up to four scores for each student.

  • C++ Data Structure Write a program to read a list of students from a file and...

    C++ Data Structure Write a program to read a list of students from a file and create a list. The program should use a linked list for implementation. Each node in the linked list should have the student’s name, a pointer to the next student, and a pointer to a linked list of scores. There may be up to four scores for each student.

  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions.    Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...

  • C++ Program 1. Read data for names and weights for 15 people. 2. Your program will...

    C++ Program 1. Read data for names and weights for 15 people. 2. Your program will build a list for the data maintained in ascending order based on both name and weight via a doubly linked list. 3. This dll will use one pointer to keep weights in sorted order, and use the other link to keep names on sorted order. 4. You need to build the list as you go maintaining this ordering, so at any time a print...

  • C++ Program 1. Read data for names and weights for 15 people. 2. Your program will...

    C++ Program 1. Read data for names and weights for 15 people. 2. Your program will build a list for the data maintained in ascending order based on both name and weight via a doubly linked list. 3. This dll will use one pointer to keep weights in sorted order, and use the other link to keep names on sorted order. 4. You need to build the list as you go maintaining this ordering, so at any time a print...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT