In: Computer Science
Create a program that implements a singly linked list of Students.
Each node must contain the following variables:
In main():
Student_ID |
Student_Name |
00235 |
Mohammad |
00662 |
Ahmed |
00999 |
Ali |
00171 |
Fahad |
Student_ID |
Student_Name |
00236 |
Salman |
00663 |
Suliman |
00998 |
Abdulrahman |
in java
Here is the completed code for this problem. 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
// StudentLinkedList.java
public class StudentLinkedList {
//nodes to represent head and tail
private StudentNode head;
private StudentNode tail;
//current size of the list
private int size;
//constructor initializing empty list
public StudentLinkedList() {
head = null;
tail = null;
size = 0;
}
//method to add a student to the head
public void addHead(String name, String id) {
//creating a new node with given name and id
StudentNode node = new StudentNode(name, id);
//if this is first value, adding as both head and tail
if (size == 0) {
head = node;
tail = node;
} else {
//adding before head, and updating head
node.next = head;
head = node;
}
//updating size
size++;
}
//method to add a student to the tail
public void addTail(String name, String id) {
//creating a new node with given name and id
StudentNode node = new StudentNode(name, id);
//if this is first value, adding as both head and tail
if (size == 0) {
head = node;
tail = node;
} else {
//adding after tail, updating tail
tail.next = node;
tail = node;
}
size++;
}
//removes a node from the head, if not empty
public void deleteHead() {
//proceeding only if list is not empty
if (size > 0) {
//removing head node
head = head.next;
//setting tail to null if head become null
if (head == null) {
tail = null;
}
//updating size
size--;
}
}
//returns a String representation of the list
public String toString() {
String data = "";
//appending each node's toString() value to a String and returning it
for (StudentNode node = head; node != null; node = node.next) {
data += node.toString() + " ";
}
return data;
}
//an inner class representing a node in the linked list
class StudentNode {
//attributes
String name;
String id;
StudentNode next;
//constructor taking name and id of a student
public StudentNode(String name, String id) {
this.name = name;
this.id = id;
next = null;
}
//returns a String containing name and id details
public String toString() {
return "[name: " + name + ", id: " + id + "]";
}
}//end of StudentNode class
//main method for testing
public static void main(String[] args) {
//creating a list, adding some students using addHead() method
StudentLinkedList list1 = new StudentLinkedList();
list1.addHead("Mohammad", "00235");
list1.addHead("Ahmed", "00662");
list1.addHead("Ali", "00999");
list1.addHead("Fahad", "00171");
//printing the list
System.out.println(list1);
//creating a list, adding some students using addTail() method
StudentLinkedList list2 = new StudentLinkedList();
list2.addTail("Salman", "00236");
list2.addTail("Suliman", "00663");
list2.addTail("Abdulrahman", "00998");
//printing the list
System.out.println(list2);
}
} //end of StudentLinkedList class
/*OUTPUT*/
[name: Fahad, id: 00171] [name: Ali, id: 00999] [name: Ahmed, id: 00662] [name: Mohammad, id: 00235]
[name: Salman, id: 00236] [name: Suliman, id: 00663] [name: Abdulrahman, id: 00998]