- Java Data Structures Tutorial
- Java Data Structures Resources
- Java Data Structures - Quick Guide
- Java Data Structures - Resources
- Java Data Structures - Discussion
Java Data Structures - Linked List Class
Introduction
The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
Class declaration
Following is the declaration for java.util.LinkedList class −
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable
Parameters
Following is the parameter for java.util.LinkedList class −
E − This is the type of elements held in this collection.
Field
Fields inherited from class java.util.AbstractList.
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 | LinkedList() This constructs constructs an empty list. |
2 | LinkedList(Collection<? extends E> c) This constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
Class methods
Sr.No. | Method & Description |
---|---|
1 | boolean add(E e) This method appends the specified element to the end of this list. |
2 | void add(int index, E element) This method inserts the specified element at the specified position in this list. |
3 | boolean addAll(Collection<? extends E> c) This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
4 | boolean addAll(int index, Collection<? extends E> c) This method inserts all of the elements in the specified collection into this list, starting at the specified position. |
5 | void addFirst(E e) This method returns inserts the specified element at the beginning of this list.. |
6 | void addLast(E e) This method returns appends the specified element to the end of this list. |
7 | void clear() This method removes all of the elements from this list. |
8 | Object clone() This method returns returns a shallow copy of this LinkedList. |
9 | boolean contains(Object o) This method returns true if this list contains the specified element. |
10 | Iterator<E> descendingIterator() This method returns an iterator over the elements in this deque in reverse sequential order. |
Example
package com.tutorialspoint; import java.util.*; public class LinkedListDemo { public static void main(String[] args) { // create a LinkedList LinkedList list = new LinkedList(); // add some elements list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); // print the list System.out.println("LinkedList:" + list); // add a new element at the end of the list list.add("Element"); // print the updated list System.out.println("LinkedList:" + list); } }
Output
LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Hello, 2, Chocolate, 10, Element]
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.
Linked List Representation
Linked list can be visualized as a chain of nodes, where every node points to the next node.
Each linked list contains head and a node.
Each node stores the data and the address of the next element.
Last element of the list is null marking the end of the list.
A linked list is of three types
Simple Linked List − Item navigation is forward only.
Doubly Linked List − Items can be navigated forward and backward.
Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.
To Continue Learning Please Login
Login with Google