Question

In: Computer Science

Change program linkedListClass to the linked list for student items (a student item contains id, name...

Change program linkedListClass to the linked list for student items (a student item contains id, name and score, where the id is used as key) and test it in the main method. You can define a student item using a class of student.

given the following codes;


import java.util.*;

public class linkedListClass
{
   public Node header;

   public linkedListClass()
   {
       header = null;
   }

   public final Node Search(int key)
   {
       Node current = header;
       while (current != null && current.item != key)
           current = current.link;
       if (current == null)
           System.out.println("There is no such item!");
       return current;
   }

   public final void Append(int newItem)
   {
       Node newNode = new Node(newItem);
       newNode.link = header;
       header = newNode;
   }

   public final Node Remove()
   {
       Node x = header;
       if ( x == null)
           System.out.println("It is empty!");
       else
           header = header.link;
       return x;
   }

   public final Node searchPrevious(int key)
   {   
       if (header == null)
return header;
       else
       {
       Node current = header;
      
       while (!(current.link == null) && (current.link.item != key))
           current = current.link;
       return current;
       }
   }


   public final void Delete(int key)
   {
       if (header == null)
           System.out.println("It is empty!");
       else
       {
           if (header.item == key) // The header is the one to be deleted.
               header = header.link;
           else
           {
               Node p = searchPrevious(key);
               if (p.link == null)
                   System.out.println("There is no such item!");
               else
                   p.link = p.link.link;
           }
       }
   }

   public final void PrintList()
   {
       if (header == null)
           System.out.println("It is empty!");
       else
       {
           Node current = header;
           System.out.println(current.item);
           while (!(current.link == null))
           {
               current = current.link;
               System.out.println(current.item);
           }
       }
   }
}

import java.util.*;

public final class linkedListTest
{
   public static void main(String args[])
   {
       int Key;
       int NewItem;

       /* create an empty linked list */
       linkedListClass LL = new linkedListClass();

       /* create a linked list of n nodes */
       System.out.println("Enter the number of items to append:");
       int n= Integer.parseInt(new Scanner(System.in).nextLine());
       System.out.printf("Enter %1$s items" + "\r\n", n);
       for (int i = 0; i < n; i++)
       {
           NewItem = Integer.parseInt(new Scanner(System.in).nextLine());
           LL.Append(NewItem);
       };
      
   System.out.println("Display all items from the header:");
       LL.PrintList();
      
       /* Test the operations of linked list */
       System.out.println("Enter 1 for search, 2 for deletion, 3 for append, 4 for remove");
       int s = Integer.parseInt(new Scanner(System.in).nextLine());
       while (s == 1 || s == 2 || s == 3 || s == 4)
       {
           if (s == 1)
           {
               System.out.println("Enter the key that you want to search:");
               Key = Integer.parseInt(new Scanner(System.in).nextLine());
               Node node = LL.Search(Key);
               if (node != null)
                   System.out.printf("The item is found: %1$s" + "\r\n", node.item);
           };
           if (s == 2)
           {
               System.out.println("Enter the key of the item that you want to delete:");
               Key = Integer.parseInt(new Scanner(System.in).nextLine());
               LL.Delete(Key);
               System.out.println("Display all items from the header:");
               LL.PrintList();
           };
           if (s == 3)
           {
               System.out.println("Enter the item that you want to append:");
               NewItem = Integer.parseInt(new Scanner(System.in).nextLine());
               LL.Append(NewItem);
               System.out.println("Display all items from the header:");
               LL.PrintList();
           };
           if (s == 4)
           {
               Node RemoveNode = LL.Remove();
               if (RemoveNode != null)
               {
                   System.out.printf("The removed item is: %1$s" + "\r\n", RemoveNode.item);
                   System.out.println("Display all items from the header:");
                   LL.PrintList();
               }
           };
           System.out.print("\r\n");
          
           System.out.println("Enter 1 for search, 2 for deletion, 3 for append, 4 for remove");
           s = Integer.parseInt(new Scanner(System.in).nextLine());
       }

   }

}

import java.util.*;

public class Node
{

   public int item;
   public Node link;

   public Node(int theItem)
   {
       item = theItem;
       link = null;
   }
}

import java.util.Scanner;

public class Student {
     
       public int id;
       public String name;
       public float score;
      
       Student(){
             
              System.out.println("Enter ID, Name and Score :");
           this.id = Integer.parseInt(new Scanner(System.in).nextLine());
               this.name = new Scanner(System.in).nextLine();
           this.score = Float.parseFloat(new Scanner(System.in).nextLine());

       }
         
       }

Solutions

Expert Solution

Here is the completed code for this problem. Tried to keep changes minimal as possible so that you can understand everything easily.

. Node class has been modified to use Student as type for item, instead of int

. Added a toString() method for Student class

. Updated linkedListClass class methods so that student objects are now used instead of integers. used Student object as parameter for Append method, and integer key as the student’s id for some other methods.

. Updated test program to deal with student linked list now.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// linkedListClass.java

public class linkedListClass {

      public Node header;

      public linkedListClass() {

            header = null;

      }

      // searches the node with student with id=key

      public final Node Search(int key) {

            Node current = header;

            while (current != null && current.item.id != key)

                  current = current.link;

            if (current == null)

                  System.out.println("There is no such item!");

            return current;

      }

      // appends a Student before the head node

      public final void Append(Student newItem) {

            Node newNode = new Node(newItem);

            newNode.link = header;

            header = newNode;

      }

      // removes and return the head node

      public final Node Remove() {

            Node x = header;

            if (x == null)

                  System.out.println("It is empty!");

            else

                  header = header.link;

            return x;

      }

      // returns the previous node of student with id=key

      public final Node searchPrevious(int key) {

            if (header == null)

                  return header;

            else {

                  Node current = header;

                  while (!(current.link == null) && (current.link.item.id != key))

                        current = current.link;

                  return current;

            }

      }

     

      //removes the student with id=key

      public final void Delete(int key) {

            if (header == null)

                  System.out.println("It is empty!");

            else {

                  if (header.item.id == key) // The header is the one to be deleted.

                        header = header.link;

                  else {

                        Node p = searchPrevious(key);

                        if (p.link == null)

                              System.out.println("There is no such item!");

                        else

                              p.link = p.link.link;

                  }

            }

      }

     

      //prints the list from head to tail

      public final void PrintList() {

            if (header == null)

                  System.out.println("It is empty!");

            else {

                  Node current = header;

                  System.out.println(current.item);

                  while (!(current.link == null)) {

                        current = current.link;

                        System.out.println(current.item);

                  }

            }

      }

}

// linkedListTest.java

import java.util.Scanner;

public final class linkedListTest {

      public static void main(String args[]) {

            int Key;

            /* create an empty linked list */

            linkedListClass LL = new linkedListClass();

            /* create a linked list of n nodes */

            System.out.println("Enter the number of students to add:");

            int n = Integer.parseInt(new Scanner(System.in).nextLine());

            System.out.printf("Enter %1$s students details" + "\r\n", n);

            for (int i = 0; i < n; i++) {

                  LL.Append(new Student());

            }

            System.out.println("Display all items from the header:");

            LL.PrintList();

            /* Test the operations of linked list */

            System.out

                        .println("Enter 1 for search, 2 for deletion, 3 for append, 4 for remove");

            int s = Integer.parseInt(new Scanner(System.in).nextLine());

            while (s == 1 || s == 2 || s == 3 || s == 4) {

                  if (s == 1) {

                        System.out.println("Enter the id of student that you want to search:");

                        Key = Integer.parseInt(new Scanner(System.in).nextLine());

                        Node node = LL.Search(Key);

                        if (node != null)

                              System.out.printf("The item is found: %1$s" + "\r\n",

                                           node.item);

                  }

                  if (s == 2) {

                        System.out

                                    .println("Enter the id of the student that you want to delete:");

                        Key = Integer.parseInt(new Scanner(System.in).nextLine());

                        LL.Delete(Key);

                        System.out.println("Display all items from the header:");

                        LL.PrintList();

                  }

                  if (s == 3) {

                        System.out.println("Enter the item that you want to append:");

                        // creating and adding new Student

                        LL.Append(new Student());

                        System.out.println("Display all items from the header:");

                        LL.PrintList();

                  }

                  if (s == 4) {

                        Node RemoveNode = LL.Remove();

                        if (RemoveNode != null) {

                              System.out.printf("The removed Student is: %1$s" + "\r\n",

                                          RemoveNode.item);

                              System.out.println("Display all items from the header:");

                              LL.PrintList();

                        }

                  }

                  ;

                  System.out.print("\r\n");

                  System.out

                              .println("Enter 1 for search, 2 for deletion, 3 for append, 4 for remove");

                  s = Integer.parseInt(new Scanner(System.in).nextLine());

            }

      }

}

// Node.java

public class Node {

      // using Student object as item

      public Student item;

      public Node link;

      // constructor taking Student object as item

      public Node(Student theItem) {

            item = theItem;

            link = null;

      }

}

// Student.java

import java.util.Scanner;

public class Student {

      public int id;

      public String name;

      public float score;

      Student() {

            System.out.println("Enter ID, Name and Score :");

            this.id = Integer.parseInt(new Scanner(System.in).nextLine());

            this.name = new Scanner(System.in).nextLine();

            this.score = Float.parseFloat(new Scanner(System.in).nextLine());

      }

      // returns a String containing student name, id and score

      public String toString() {

            return name + "(id=" + id + ", score=" + score + ")";

      }

}

/*OUTPUT*/

Enter the number of students to add:

3

Enter 3 students details

Enter ID, Name and Score :

101

Oliver

99

Enter ID, Name and Score :

105

John

88

Enter ID, Name and Score :

106

Barry

100

Display all items from the header:

Barry(id=106, score=100.0)

John(id=105, score=88.0)

Oliver(id=101, score=99.0)

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

1

Enter the id of student that you want to search:

105

The item is found: John(id=105, score=88.0)

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

2

Enter the id of the student that you want to delete:

101

Display all items from the header:

Barry(id=106, score=100.0)

John(id=105, score=88.0)

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

3

Enter the item that you want to append:

Enter ID, Name and Score :

108

James

76

Display all items from the header:

James(id=108, score=76.0)

Barry(id=106, score=100.0)

John(id=105, score=88.0)

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

4

The removed Student is: James(id=108, score=76.0)

Display all items from the header:

Barry(id=106, score=100.0)

John(id=105, score=88.0)

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

4

The removed Student is: Barry(id=106, score=100.0)

Display all items from the header:

John(id=105, score=88.0)

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

4

The removed Student is: John(id=105, score=88.0)

Display all items from the header:

It is empty!

Enter 1 for search, 2 for deletion, 3 for append, 4 for remove

5


Related Solutions

Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular. Code needed in Java.
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
Student Structure Assignment Create a Student Structure globally consisting of student ID, name, completed credits, and...
Student Structure Assignment Create a Student Structure globally consisting of student ID, name, completed credits, and GPA. Define one student in main() and initialize the student with data. Display all of the student’s data on one line separated by tabs. Create another student in main(). Assign values to your second student (do not get input from the user). Display the second student’s data on one line separated by tabs. Create a third student in main(). Use a series of prompts...
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Student Name and ID no. _________________________________________________________ Write the journal entries in the given table for below...
Student Name and ID no. _________________________________________________________ Write the journal entries in the given table for below transactions of Clean Corporation during June 2020. 1 Provided services to customers on account for $650. 2 Purchased a building using 10 year Note Payable for $200,000. 3 Paid salaries to employees, $2,600. 4 Received payment from customers to whom service was given in transaction 1 5 Paid $400 dividends to shareholders. Answer Accounts Debit Credit 1 2 3 4 5
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
List department name, employee id, and employee name for all employees in department name order. Repeat...
List department name, employee id, and employee name for all employees in department name order. Repeat for department #10 only. List the course ID, course name, section, instructor name, day, time, and room for all course sections. List the course ID, course name, section, student ID, and student name for CRN 1003. Display the list in ascending order of student last and first names. DROP TABLE registration; DROP TABLE sections; DROP TABLE courses; DROP TABLE students; DROP TABLE instructors; CREATE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT