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 double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
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.
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
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...
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...
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>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT