-1

In a exercise that I came across, I'm requested to use a linked list. I know the use of arrays and array list, but how does it differ from linked list (array list I mean, I know the array has a specific number of elements).

What I'm requested to do is to store some elements in linked list. What I thought of doing is storing some values in an array and using those reference to store inside the linked list. The user can enter any amount of strings inside the array. The array it self has 24 names so the user can either chose 1 or all 24.

Is it possible to use the linked list without using the nodes to connect it? As in, if I am printing the values off the linked list, can I not just use a for loop?

What is the actual reason to use nodes? I know it is to connect, but if I connect using them, can I use linked list in any order or do I have to go through that specific order? If anyone can explain using few examples, that would be of great help.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • It looks like you're not clear on how a [linked list](https://en.wikipedia.org/wiki/Linked_list) operates. – Kayaman Apr 13 '18 at 07:42
  • Possible duplicate of [When to use LinkedList over ArrayList?](https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) – HBo Apr 13 '18 at 07:43
  • *array list i mean i know the array has a specific number of elements* no no NO. arrays have restricted bound but arraylist does not. – Aniket Sahrawat Apr 13 '18 at 07:47

1 Answers1

0

Yes,you can do it by using LinkedList class in Java which implements the list interface.

// Java code for Linked List implementation

import java.util.*;

public class Test
{
    public static void main(String args[])
    {
        // Creating object of class linked list
        LinkedList<String> object = new LinkedList<>();

        // Adding elements to the linked list
        object.add("A");
        object.add("B");
        object.addLast("C");
        object.addFirst("D");
        object.add(2, "E");
        object.add("F");
        object.add("G");
        System.out.println("Linked list : " + object);

        // Removing elements from the linked list
        object.remove("B");
        object.remove(3);
        object.removeFirst();
        object.removeLast();
        System.out.println("Linked list after deletion: " + object);

        // Finding elements in the linked list
        boolean status = object.contains("E");

        if(status)
            System.out.println("List contains the element 'E' ");
        else
            System.out.println("List doesn't contain the element 'E'");

        // Number of elements in the linked list
        int size = object.size();
        System.out.println("Size of linked list = " + size);

        // Get and set elements from linked list
        Object element = object.get(2);
        System.out.println("Element returned by get() : " + element);
        object.set(2, "Y");
        System.out.println("Linked list after change : " + object);
    }
}

Output:

Linked list : [D, A, E, B, C, F, G]
Linked list after deletion: [A, E, F]
List contains the element 'E' 
Size of linked list = 3
Element returned by get() : F
Linked list after change : [A, E, Y]
Logan
  • 926
  • 1
  • 10
  • 18