Question

In: Computer Science

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.

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

class SingleLinkedList{
    //Node of the linked list
    class Node{
        Node next;
        String s;
        //Argumented constructor
        Node(String s){
            this.s = s;
        }

        @Override
        public String toString(){
            return this.s;
        }
    }

    //Head of the linked list
    Node head;

    SingleLinkedList(){
        head = null;
    }

    //Add a String to the linked list
    public void add(String s){
        //If head is null, make the new node head
        if(head==null){
            head = new Node(s);
            return;
        }
        //Move to the end of the linked list
        Node temp  = head;
        while (temp.next!=null){
            temp = temp.next;
        }
        //Add new node at the end
        temp.next = new Node(s);
    }

    public void add(int index, String s){
        Node newNode = new Node(s);
        //If index is 0, add at the head
        if(index==0){
            newNode.next = head;
            head = newNode;
            return;
        }
        //Move till the index
        Node temp = head;
        for(int ind=0;ind<index-1;ind++){
            temp = temp.next;
        }
        newNode.next = temp.next;
        temp.next = newNode;
    }

    public String toString(){
        Node temp = head;
        String ans = "";
        while (temp!=null){
            ans = ans+temp.toString()+"->";
            temp = temp.next;
        }
        ans = ans+"NULL";
        return ans;
    }

}
class Main{
    public static void main(String[] args) {
        SingleLinkedList sl = new SingleLinkedList();
        sl.add("Brian");
        sl.add("Larry");
        System.out.println(sl);
        sl.add(1,"Kathy");
        System.out.println(sl);
        sl.add(0,"Chris");
        sl.add("Briana");
        System.out.println(sl);
    }
}

Related Solutions

write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
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()...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. 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() { if (isEmpty()) return null; return tail.getElement(); } /*...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
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 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...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT