In: Computer Science
package learning;
import java.util.*;
class Student{
public String name;
public int marks;
Student(String name,int marks){
this.name = name;
this.marks = marks;
}
}
class node{
public Student S;
node next;
node prev;
node(Student S){
this.S = S;
next=null;
prev=null;
}
}
class DLL{
node head=null;
node tail=null;
void insert(String name,int marks) {
if(head == null) {
head = new node(new Student(name,marks));
tail = head;
}else {
tail.next = new node(new Student(name,marks));
tail.next.prev = tail;
tail = tail.next;
}
}
void largest() {
node temp = head;
Student Max = new Student("dummy",-1000);
while(temp!=null) {
if(temp.S.marks > Max.marks) {
Max = temp.S;
}
temp = temp.next;
}
System.out.println("Student with Largest Marks: ");
System.out.println(Max.name);
System.out.println(Max.marks);
System.out.println();
}
void printList() {
node temp = head;
while(temp!=null) {
System.out.println(temp.S.name);
System.out.println(temp.S.marks);
temp = temp.next;
}
}
void printMarks(int m) {
node temp = head;
while(temp!=null) {
if(temp.S.marks == m) {
System.out.println(temp.S.name);
System.out.println(temp.S.marks);
System.out.println();
}
temp = temp.next;
}
}
void delete(char c) {
node temp = head;
while(temp!=null) {
if(temp.S.name.charAt(0) == c) {
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
}
temp = temp.next;
}
}
}
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DLL D = new DLL();
System.out.print("Enter the number of students to insert:");
int n = Integer.parseInt(input.nextLine());
for(int i=0;i<n;++i) {
System.out.print("Enter the name of the student:");
String name = input.nextLine();
System.out.print("Enter the marks of the student:");
int marks = Integer.parseInt(input.nextLine());
D.insert(name, marks);
}
System.out.println();
D.printList();
System.out.println();
System.out.print("Enter the marks to search for:");
int marks = Integer.parseInt(input.nextLine());
D.printMarks(marks);
System.out.println();
D.largest();
System.out.print("Enter the first Character of student name to delete:");
char c =input.nextLine().charAt(0);
D.delete(c);
System.out.print("List after deleting students with first character "+ c + ":\n");
D.printList();
}
}
OUTPUT: