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...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
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...
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
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()...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
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)
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...
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(); } /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT