LinkedList

  • Eclipse: Oxygen
  • Java: 1.8

This is one of the most useful data structures and highly used in real-world scenarios because of its high-speed capacity and fast addition and deletion. This basic collection structure is the data in and the address which points to the next node functions as the guide of the respective node (That is the reason for it being called the Linked List). Several methods on this include addition, deletion of a node anywhere in the list.

This following tutorial deals with the operations regarding the Linked Lists such as add, adding in the specific position, printing and removing the elements from the specific position in the linked list.

add() method is used to add data in a newly created linked list. But if the linked list is already created then add() will add data at the end of the linked list.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*****************************

In the following example, using add(int index, Object item) method, data can be added at the specific position of the linked list. Eg. Canada is added at the second position in the countries list.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*****************************

Element: India

Element: Canada

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

Element: Bragil

*****************************

In the following example, linked data can be modify using set () method.

Eg. Data is modified at the 7th index position.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*****************************

Element: India

Element: Canada

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

Element: Bragil

*****************************

Element: India

Element: Canada

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

Element: Brazil

*****************************

In the following example, remove() method is used to remove the data from specified location.

Eg, remove(4) data will be removed from the 4th index position.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*****************************

Element: India

Element: Canada

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

Element: Bragil

*****************************

Element: India

Element: Canada

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

Element: Brazil

*****************************

Element: India

Element: Canada

Element: USA

Element: Poland

Element: Thailand

Element: Russia

Element: Brazil

*****************************

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu