0% found this document useful (0 votes)
6 views18 pages

4-Array List and Linked List-21!12!2024

The document provides an overview of Linked Lists in Java, highlighting their characteristics such as maintaining insertion order, allowing duplicates and null values, and implementing Queue and Deque interfaces. It contrasts Linked Lists with Array Lists, explaining the structure of a doubly-linked list and various methods for adding and retrieving elements. Additionally, it includes code examples demonstrating the use of Linked List methods like addFirst, addLast, offer, peek, and toArray.

Uploaded by

Sekhar Aripaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

4-Array List and Linked List-21!12!2024

The document provides an overview of Linked Lists in Java, highlighting their characteristics such as maintaining insertion order, allowing duplicates and null values, and implementing Queue and Deque interfaces. It contrasts Linked Lists with Array Lists, explaining the structure of a doubly-linked list and various methods for adding and retrieving elements. Additionally, it includes code examples demonstrating the use of Linked List methods like addFirst, addLast, offer, peek, and toArray.

Uploaded by

Sekhar Aripaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

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.
friends.add("Rajeev");
friends.add("John");
friends.add("David”);
friends.add("Chris");
//Adding an element at the beginning of the LinkedList
friends.addFirst("Steve");
System.out.println("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.
friends.add("Rajeev");
friends.add("John");
friends.add("David”);
friends.add("Chris");
// Adding an element at the end of the LinkedList (This method is
equivalent to the add() method)
friends.addLast("Jennifer");
System.out.println("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
list.add(“RAJ”);
list.add(4);
list.add(“Hemanth”);
list.add(8);
// printing the list
System.out.println("The initial Linked list is : " + list);
// offering a new element
// adds element at tail.
list.offer(“Astha”);
// printing the new list
System.out.println("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
list.add("GeM");
list.add(4);
list.add("GREY");
list.add("8");
// printing the list
System.out.println("The initial list is :" + list);

// peek at the head of the list


// Prints 1st element, "GeM"
System.out.println("Head of the list : " +
list.peek());
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>();


al.add(10);
al.add(20);
al.add(30);
al.add(40);
Object[] objects = al.toArray();
// Printing array of objects
for (Object obj : objects)
System.out.print(obj + " ");
THANK YOU

You might also like