Question

In: Computer Science

plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...

plz use doubly linked list. java

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.

Solutions

Expert Solution

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:


Related Solutions

TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template......
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template... public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last()...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT