Question

In: Computer Science

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 a Singly linked list (the element of search is given by the user)
9. Call all methods above in main method with the following data:

Test Data :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8

Solutions

Expert Solution

import java.util.*;

class Main {
        public static void main(String[] args) {
                
                Scanner input = new Scanner(System.in);
                // Creating object of the 
        // class linked list 
        LinkedList<Integer> ll = new LinkedList<Integer>(); 
  
        // Adding elements to the linked list 
        System.out.print("Input the number of nodes:");
        int n = input.nextInt();
        
        for(int i=1;i<=n;++i) {
                System.out.print("Input data for node " + i + ":");
            int temp = input.nextInt();
            ll.add(temp);
        }
        System.out.println(ll);
        
        System.out.println("Number of nodes :" + ll.size());
        
        System.out.print("Node to insert at the begining of the linked list: ");
        int front = input.nextInt();
        ll.addFirst(front);
        
        System.out.print("Node to insert at the end of the linked list: ");
        int back = input.nextInt();
        ll.addFirst(back);
        
        System.out.print("Node to insert after 5 of the linked list: ");
        int after = input.nextInt();
        ll.add(ll.indexOf(5),after);
        
        System.out.print("Enter the node to delete in the linked list: ");
        int del = input.nextInt();
        ll.removeFirstOccurrence(del);
        
        System.out.print("Enter the node to be searched in linked list: ");
        int ser = input.nextInt();
        System.out.println(ll.contains(ser));
        
        System.out.print(ll);   
                
        }
}

OUTPUT:


Related Solutions

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...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
**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...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT