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.

Archived Comments

1. This design is steller! You certainly know how to keep
a reader amused. Between your wit and

View Tutorial          By: gold a good investment at 2017-04-27 05:21:21

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

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

4. This is very useful for beginners.
I want full information about Java & Data Structure

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

5. I like your linked list program. i've one doubt different between Array list and linked list.
View Tutorial          By: silambarasan at 2013-01-25 03:11:48

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

7. can anybody share those system that uses Linked List? or can you give example of a system that uses
View Tutorial          By: hazel at 2012-08-16 11:48:24

8. nice bt i wnt to implement link list n wanna to do insertion and deletion of the nodes as we do in C
View Tutorial          By: shlagha sharma at 2012-06-27 17:57:13

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

10. thanks
a good example, just what i wanted :)

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

11. It would he helpful to know difference between a list and linklist. Why use one in place of another.
View Tutorial          By: Tosh at 2012-04-28 18:00:20

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

13. i wont more of dis java

give more java

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

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

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

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

17. can you give me a good example of exception handling with detail explanation?..
View Tutorial          By: samik at 2011-08-07 06:51:20

18. Tnx. Very fast learning i think.
Just make more Topics and more Powerful.

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

19. sir can you write the sample proram of this:
Implement linked-list with 3 adding process and

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

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

21. good but i needed you to implenent linked list using Abstract Data Type operation!!
View Tutorial          By: emmanueli at 2011-06-08 10:08:16

22. Sir your example is god
but i am surprising that where is your "LinkedList" class

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

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

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

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial