Answer to Question #213358 in Java | JSP | JSF for Aniket Nayak

Question #213358

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.


1
Expert's answer
2021-07-04T17:12:25-0400
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;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS