LinkedList Sample program in Java

By: Jagan  

The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked-list data structure. It has the two constructors, shown here:

LinkedList( )
LinkedList(Collection c)

The first constructor builds an empty linked list. The second constructor builds a linked list that is initialized with the elements of the collection c.

In addition to the methods that it inherits, the LinkedList class defines some useful methods of its own for manipulating and accessing lists. To add elements to the start of the list, use addFirst( ); to add elements to the end, use addLast( ). Their signatures are shown here:

void addFirst(Object obj)
void addLast(Object obj)

Here, obj is the item being added.

To obtain the first element, call getFirst( ). To retrieve the last element, call getLast( ). Their signatures are shown here:

Object getFirst( )
Object getLast( )

To remove the first element, use removeFirst( ); to remove the last element, call removeLast( ). They are shown here:

Object removeFirst( )
Object removeLast( )

The following program illustrates several of the methods supported by LinkedList:

// Demonstrate LinkedList.
import java.util.*;
class LinkedListDemo {
public static void main(String args[]) {
// create a linked list
LinkedList ll = new LinkedList();
// add elements to the linked list
ll.add(1, "A2");
System.out.println("Original contents of ll: " + ll);
// remove elements from the linked list
System.out.println("Contents of ll after deletion: "
+ ll);
// remove first and last elements
System.out.println("ll after deleting first and last: "
+ ll);
// get and set a value
Object val = ll.get(2);
ll.set(2, (String) val + " Changed");
System.out.println("ll after change: " + ll);

The output from this program is shown here:

Original contents of ll: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]

Because LinkedList implements the List interface, calls to add(Object) append items to the end of the list, as does addLast( ). To insert items at a specific location, use the add(int, Object) form of add( ), as illustrated by the call to add(1, "A2") in the example. Notice how the third element in ll is changed by employing calls to get( ) and set( ). To obtain the current value of an element, pass get( ) the index at which the element is stored. To assign a new value to that index, pass set( ) the index and its new value.

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. I want to know about all java application.
View Tutorial          By: Ahasan at 2009-04-10 09:42:48

2. Very good tutorial!
View Tutorial          By: Herve at 2011-04-04 08:06:21

3. Sir your example is god
but i am surprisin

View Tutorial          By: Ata Ul Nasar at 2011-06-05 16:23:09

4. good but i needed you to implenent linked list usi
View Tutorial          By: emmanueli at 2011-06-08 10:08:16

5. It is simply good and more informative
View Tutorial          By: ARUNA at 2011-06-19 06:32:29

6. sir can you write the sample proram of this:

View Tutorial          By: bryan labastida at 2011-06-29 02:53:14

7. Tnx. Very fast learning i think.
Just make

View Tutorial          By: mahdi at 2011-07-09 20:20:39

8. can you give me a good example of exception handli
View Tutorial          By: samik at 2011-08-07 06:51:20

9. sir i want to knw abt all simple java programs pls
View Tutorial          By: sakthi at 2011-08-29 14:06:10

10. sir help me to do insertion in hash table using JA
View Tutorial          By: Ananthi vasan at 2011-10-13 09:34:56

11. it is good to unstand and easy to remember forever
View Tutorial          By: Abdul at 2011-11-10 21:03:41

12. i wont more of dis java


View Tutorial          By: Rishwin at 2011-12-10 19:20:12

13. very simple :)
View Tutorial          By: cha at 2012-01-09 07:38:49

14. It would he helpful to know difference between a l
View Tutorial          By: Tosh at 2012-04-28 18:00:20

15. thanks
a good example, just what i wanted :

View Tutorial          By: vejdan at 2012-04-29 12:41:43

16. how to read one by one element from linkedlist.
View Tutorial          By: shruthi at 2012-05-10 12:58:33

17. nice bt i wnt to implement link list n wanna to do
View Tutorial          By: shlagha sharma at 2012-06-27 17:57:13

18. can anybody share those system that uses Linked Li
View Tutorial          By: hazel at 2012-08-16 11:48:24

19. can you have a user input tutorial for LinkedList]
View Tutorial          By: joedjoaquin at 2013-01-22 01:59:23

20. I like your linked list program. i've one doubt di
View Tutorial          By: silambarasan at 2013-01-25 03:11:48

21. This is very useful for beginners.
I want f

View Tutorial          By: shankar at 2013-02-04 13:28:16

22. Nice work,great Work,Thanks a lot for sharing this
View Tutorial          By: at 2013-02-12 05:34:15

23. I want full information about Java & Data Stru
View Tutorial          By: s.karthick at 2015-05-07 05:32:24

24. This design is steller! You certainly know how to
View Tutorial          By: gold a good investment at 2017-04-27 05:21:21