In: Computer Science
Q1) Create a program that do the following:
1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list.
2. Write a method to find the largest mark and print the name of the student having that mark
3. Write a method to print the content of the list (name, mark)
4. Write a method to search the list for a given mark and prints the result
6. Insert 2 new students to the list (print the list before and after the insertion)
7. Delete any students with the first letter "D" in his name, (print the list before and after the deletion)
Submit .java files only.
Create program that do the following:
1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list.
Explanation:
LinkedList.java
class Node{
protected int marks;
protected String name;
protected Node next, prev;
public Node(){
next = null;
prev = null;
marks = 0;
name = null;
}
public Node(String name, int marks, Node next, Node prev){
this.next = next;
this.prev = prev;
this.name = name;
this.marks = marks;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
public Node getPrev(){
return this.prev;
}
public void setPrev(Node prev){
this.prev = prev;
}
public void setMarks(int marks){
this.marks = marks;
}
public int getMarks(){
return this.marks;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
public class LinkedList{
protected Node start;
protected Node end;
public int size;
public LinkedList(){
this.start = null;
this.end = null;
this.size = 0;
}
public Node getStart(){
return this.start;
}
public Node getEnd(){
return this.end;
}
public void insertAtStart(String name, int marks){
Node ptr = new Node(name, marks, null, null);
if(start == null){
start = ptr;
end = start;
}
else
{
start.setPrev(ptr);
ptr.setNext(start);
start = ptr;
}
size++;
}
public void insertAtEnd(String name, int marks){
Node ptr = new Node(name, marks, null, null);
if(start == null){
start = ptr;
end = start;
}
else{
end.setNext(ptr);
ptr.setPrev(end);
end = ptr;
}
size++;
}
public void deleteWithName(String name){
Node ptr = start;
while(!ptr.getName().equals(name))
ptr = ptr.getNext();
if(ptr.equals(start)){
start = ptr.getNext();
}
else if(ptr.equals(end)){
end = ptr.getPrev();
end.setNext(null);
}
else{
ptr.getPrev().setNext(ptr.getNext());
}
}
public void printHigestMark(){
int highest = 0;
String name = null;
Node ptr = start;
for( int i = 0; i< size; i++){
if(highest < ptr.getMarks()){
highest = ptr.getMarks();
name = ptr.getName();
ptr = ptr.getNext();
}
}
System.out.println(name + " has highest marks : " + highest);
}
public void search(int marks){
Node ptr = start;
for( int i=0; i<size; i++){
if( marks == ptr.getMarks())
System.out.println("Student : " + ptr.getName() + " has marks : " + ptr.getMarks());
ptr = ptr.getNext();
}
}
public void deleteWithFirstLetterD(){
Node ptr = start;
String name;
for( int i=0; i<size; i++){
name = ptr.getName();
if(name.startsWith("D") || name.startsWith("d")){
this.deleteWithName(name);
}
ptr = ptr.getNext();
}
}
public void show(){
Node ptr = start;
if(start == null )
System.out.println("LINKED LIST IS EMPTY ");
else{
if(start.equals(end)){
System.out.println("Name: " + ptr.getName() + "\tMarks : " + ptr.getMarks());
}
else{
while(!ptr.equals(end)){
System.out.println("Name: " + ptr.getName() + "\tMarks : " + ptr.getMarks());
ptr = ptr.getNext();
}
System.out.println("Name: " + ptr.getName() + "\tMarks : " + ptr.getMarks());
}
}
}
}
Main.java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
LinkedList studentList = new LinkedList();
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of students : ");
int n = Integer.parseInt(input.nextLine());
System.out.println("Enter the name and marks of the students ");
int tempMarks = 0;
String tempName = null;
for(int i = 1;i<=n;i++){
System.out.println("Name : ");
tempName = input.nextLine();
System.out.println("Marks : ");
tempMarks = Integer.parseInt(input.nextLine());
studentList.insertAtEnd(tempName, tempMarks);
}
studentList.printHigestMark();
studentList.show();
studentList.search(84);
System.out.println("List before insertion ");
studentList.show();
studentList.insertAtEnd("Hagrid", 10);
studentList.insertAtEnd("Peter", 40);
System.out.println("List after insertion ");
studentList.show();
System.out.println("DELETING ");
System.out.println("List before deletion");
studentList.show();
System.out.println("List after deletion ");
studentList.deleteWithFirstLetterD();
studentList.show();
}
}
Output: