Create a Student class with instance variables name, roll, mark and instance methods setStudentDetails(), displayStudent(). One object of the Student class represents a specific student. Create a Singly Linked List in such a way that every node of the Linked List contains 2 parts: stud and link, where, stud stores the reference to a Student object and link stores the reference of the next node. Hence, the Node class (user defined type) has the following structure.
import java.util.Scanner;
public class Student {
private String name;
private int roll;
private int mark;
public void setStudentDetails() {
Scanner in = new Scanner(System.in);
name = in.nextLine();
roll = Integer.parseInt(in.nextLine());
mark = Integer.parseInt(in.nextLine());
}
public void displayStudent() {
System.out.println(name + ", roll " + roll + ", mark " + mark);
}
}
public class SinglyLinkedList {
private Node head;
private Node tail;
public SinglyLinkedList() {
head = null;
tail = null;
}
public void add(Student student) {
if (head == null) {
head = new Node(student);
tail = head;
} else {
tail.next = new Node(student);
tail = tail.next;
}
}
private static class Node {
Student stud;
Node next;
Node(Student stud) {
this.stud = stud;
}
}
}
Comments
Leave a comment