In: Computer Science
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());
}
}
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