LINKED LIST
IN
JAVA
LINKED –LIST
KEY–NOTE TO LINKED LIST
• Java Linked-List maintains the insertion order of the elements
• LinkedList can have duplicate and null values
• The LinkedList class implements Queue and Deque interfaces
• Therefore, It can also be used as a Queue, Deque or Stack
ARRAY-LIST VS LINKED -LIST
• Both ArrayList and LinkedList implement the List interface
• However, they differ completely in the way they store and link to the
elements
• An ArrayList stores the elements sequentially based on their index
• However, a LinkedList uses a doubly-linked list to store its elements
ARRAY–LIST VS LINKED –LIST
● A doubly-linked list consists of a collection of nodes, where each node
contains three fields -The data at that node
● A pointer/reference to the next node in the list
● A pointer/reference to the previous node in the list
addfirst
public void addFirst(E e)
Inserts the specified element at the beginning of this list
Parameters:
e - the element to add
LOGIC
// Creating a LinkedList
LinkedList<String> friends = new LinkedList<>();
// Adding new elements to the end of the LinkedList using add() method.
[Link]("Rajeev");
[Link]("John");
[Link]("David”);
[Link]("Chris");
//Adding an element at the beginning of the LinkedList
[Link]("Steve");
[Link]("After addFirst(\"Steve\") : " + friends);
addLast
public void addLast(E e)
Appends the specified element to the end of this list
Parameters:
e - the element to add
LOGIC
// Creating a LinkedList
LinkedList<String> friends = new LinkedList<>();
// Adding new elements to the end of the LinkedList using add() method.
[Link]("Rajeev");
[Link]("John");
[Link]("David”);
[Link]("Chris");
// Adding an element at the end of the LinkedList (This method is
equivalent to the add() method)
[Link]("Jennifer");
[Link]("After addLast(\"Jennifer\") : " + friends);
offer
public boolean offer(E e)
Adds the specified element as the tail (last element) of
this list
Parameters: e - the element to add
Returns: true
LOGIC
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
[Link](“RAJ”);
[Link](4);
[Link](“Hemanth”);
[Link](8);
// printing the list
[Link]("The initial Linked list is : " + list);
// offering a new element
// adds element at tail.
[Link](“Astha”);
// printing the new list
[Link]("LinkedList after insertion using offer() : " +
list);
peek
public E peek()
Retrieves, but does not remove, the last element of this list, or returns null
if this list is empty
Returns: the last element of this list, or null if this list is empty
LOGIC
// declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
[Link]("GeM");
[Link](4);
[Link]("GREY");
[Link]("8");
// printing the list
[Link]("The initial list is :" + list);
// peek at the head of the list
// Prints 1st element, "GeM"
[Link]("Head of the list : " +
[Link]());
toArray
public Object[] toArray()
Returns an array containing all of the elements in this list in proper
sequence (from first to last element)
Returns:
an array containing all of the elements in this list in proper sequence
LOGIC:
List<Integer> al = new ArrayList<Integer>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
Object[] objects = [Link]();
// Printing array of objects
for (Object obj : objects)
[Link](obj + " ");
THANK YOU