Linked List: JAVA

Screen Shot 2017-05-25 at 1.35.50 AM
What is a LinkedList?

  1. Linked List (LL) is a data structure where each element links to the next element
  2. Element in Linked List are not indexed so to access an element you have to start from the head and work your way through to the required element
  3. LL is quite slow for retrieving data (Point 2) but insertions and deletions to the beginning of the LL are very fast
  4. It can contain any type of data type be it Strings, Int, Char etc.
  5. Elements in Linked List can be all duplicates or unique

Declaring a LinkedList:


LinkedList<String> fruitNames = new LinkedList<String>();

Here we are initializing a LinkedList called fruitNames containing data of type String.

Note: Don’t forget to import java.util.* to use the LinkedList class

A few methods you can use on LinkedList:

  1. Use add() method to add elements to the LinkedList

fruitNames.add("banana");

fruitNames.add("apple");

fruitNames.add("cherry");

Add element at a specified index as following:


fruitNames.add(1, "watermelon");

Add elements to the beginning or end of LinkedList as following:


fruitNames.addFirst("strawberry");

fruitNames.addLast("blueberry");

 

2. Fetch elements from the LinkedList using a get() method and passing it the index parameter as following:


System.out.println("Index 1 element in LinkedList is: " + fruitName.get(1));

Fetch elements from the beginning or end of LinkedList as following:


System.out.println("The first element in LinkedList is: " +fruitName.getFirst());

System.out.println("The last element in the LinkedList is: " + fruitNames.getLast());

 

3. Remove elements from the beginning or end of LinkedList as following:


fruitNames.remove();

//or use the removeFirst() method

fruitNames.removeFirst();

fruitNames.removeLast();

Remove elements from a specific index using the remove() method and passing it the index parameter as following:


fruitNames.remove(2);

 

4. Get size of the LinkedList using the size() method as following:


fruitNames.size();

 

5. Use clear() method to remove all the elements from a LinkedList as following:


fruitNames.clear();

Source code:


import java.util.*;

public class LinkedListBlog {

public static void main(String[] args){

// Declaring LinkedList
LinkedList<String> fruitNames = new LinkedList<String>();

// Adding 3 elements to LinkedList
fruitNames.add("banana");
fruitNames.add("apple");
fruitNames.add("cherry");

//Printing LinkedList after adding elements
System.out.println("LinkedList fruitNames: " + fruitNames);

//Adding watermelon to index 1
fruitNames.add(1, "watermelon");
System.out.println("LL after adding watermelon to index 1: " + fruitNames);

//Adding strawberry to the beginning of the LinkedList
fruitNames.addFirst("strawberry");
System.out.println("LL after adding strawberry at the beginning: " + fruitNames);

//Adding blueberry to the end of the LinkedList
fruitNames.addLast("blueberry");
System.out.println("LL after adding blueberry to the end: " + fruitNames);

//Printing the element at index 1
System.out.println("Index 1 element in the LinkedList is: " + fruitNames.get(1));

//Printing first and last element in the LinkedList
System.out.println("The first element in LinkedList is: " + fruitNames.getFirst());
System.out.println("The last element in the LinkedList is: " + fruitNames.getLast());

//Removing First and Last element from the LinkedList
fruitNames.remove();
System.out.println("LinkedList after removing first element: " + fruitNames);
//can also use the removeFirst() method
fruitNames.removeLast();
System.out.println("LinkedList after removing last element: " + fruitNames);

//Removing element from index 2
fruitNames.remove(2);
System.out.println("LinkedList after removing index 2 element: " + fruitNames);

//Printing size of fruitNames
System.out.println("Size of LinkedList is: " + fruitNames.size());

//Clearing entire LinkedList
fruitNames.clear();
System.out.println("LinkedList after using clear method: " + fruitNames);

}
}

Output:

LinkedList fruitNames: [banana, apple, cherry]

LL after adding watermelon to index 1: [banana, watermelon, apple, cherry]

LL after adding strawberry at the beginning: [strawberry, banana, watermelon, apple, cherry]

LL after adding blueberry to the end: [strawberry, banana, watermelon, apple, cherry, blueberry]

Index 1 element in the LinkedList is: banana

The first element in LinkedList is: strawberry

The last element in the LinkedList is: blueberry

LinkedList after removing first element: [banana, watermelon, apple, cherry, blueberry]

LinkedList after removing last element: [banana, watermelon, apple, cherry]

LinkedList after removing index 2 element: [banana, watermelon, cherry]

Size of LinkedList is: 3

LinkedList after using clear method: []
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: