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
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse...
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse list method in the subclass to reverse the order of the linked list. Print the original linked list and the reverse ordered linked list at the end of program. You can use the gamescore.txt to test the reverse method. _____________________________________________________________________________________________________________________________________________________ /** Source code example for "A Practical Introduction to Data     Structures and Algorithm Analysis, 3rd Edition (Java)"     by Clifford A. Shaffer     Copyright 2008-2011 by...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT