Linked List Data Structure
Kamaluddin“Behzad”
Bachelor in Computer Application
Linked Lists
A linked list is a linear data structure that stores data in terms of
nodes.
Linked lists are dynamic data structures in essence that they are
created and destroyed as and when needed.
They are a solution to the problems posed by arrays.
Arrays being static in nature suffer from the two main problems.
Linked Lists
• Shortage of memory:
Consider a situation where we store data of students in an array,
where each location stores a single student’s data. Initially if we
had 40 students in a class, we could work out by declaring an array
of 40 locations. Since array sized has to be defined whilst
programming we reserve 40 locations. Later if more students are
added to the class, there is no way we can increase the size of the
array to accommodate the additional student’s data. This leads to
shortage of memory.
Linked Lists
Wastage of memory
Consider a situation where we store data of students in an array, where
each location stores a single student’s data. Initially if we had 40 students
in a class, we could work out by declaring an array of 40 locations.
Anticipating that there might be additional students joining later, we
define the array with 10 additional locations thus we reserve 50 locations
in all. Later if no additional students join, the additional 10 locations
would remain unused, reserved but wasted as there is no way we can
decrease the size of the array and release the memory allocated to the
additional 10 locations. This leads to wastage of memory.
Linked Lists
Depending on the structure of the node and its traversing mechanism we
have two types of lists:
1. Singly Linked List
2. Doubly Linked List
Singly Linked List
A singly linked list is a linked list that has a singly list pointer reference and a node structure
as shown below:
Data next
The data part stores the information and the next is a pointer to the next node in order.
For the last node in the list the next pointer holds a null value, indicating the end of the list.
Singly Linked List
Operations on a singly linked list
In the linked list we have two operation INSERT and DELETE.
• Insert at head
This operation inserts a new node to the head end of the list.
Singly Linked List
• Insert at tail
This operation inserts a new node to the tail end of the list.
Singly Linked List
• Insert between
This operation inserts a new node in between the existing list.
Singly Linked List
Delete operation
• Delete at head
This operation deletes a node from the head end of the list.
Singly Linked List
• Delete at tail
This operation deletes a node from the tail end of the list.
Singly Linked List
• Delete between
This operation deletes a node from in between the existing list.
Doubly Linked List
A doubly linked list is a linked list that has a two list pointer references and a node structure
as shown below:
Previous Data next
The data part stores the information, the previous is the pointer to the predecessor node
and the next is a pointer to the successor node.
For the first node the previous pointer holds a null value and for the last node in the list the
next pointer holds a null value.
Doubly Linked List
Operations on a doubly linked list
In the double linked list also we have INSERT and DELETE.
• Insert at head
This operation inserts a new node to the head end of the list.
Doubly Linked List
• Insert at tail
This operation inserts a new node to the tail end of the list.
Doubly Linked List
• Insert between
This operation inserts a new node in between the existing list.
Doubly Linked List
• Delete at head
This operation deletes a node from the head end of the list.
Doubly Linked List
• Delete at tail
This operation deletes a node from the tail end of the list.
Doubly Linked List
• Delete between
This operation deletes a node from in between the existing list.
Add element to the linked list use add()
import java.util.LinkedList;
class Main {
public static void main(String[] args){
LinkedList<String> animals = new LinkedList<>();
animals.add("Dog");
animals.add("Cat");
animals.add("Cow");
System.out.println("LinkedList: " + animals);
animals.add(1, "Horse");
System.out.println("Updated LinkedList: " + animals); } }
Access the element of linked list use get()
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> languages = new LinkedList<>();
languages.add("Python");
languages.add("Java");
languages.add("JavaScript");
System.out.println("LinkedList: " + languages);
String str = languages.get(1);
System.out.print("Element at index 1: " + str); } }
Change the element of linked list use set()
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> languages = new LinkedList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
System.out.println("LinkedList: " + languages);
languages.set(2, "Kotlin");
System.out.println("Updated LinkedList: " + languages); } }
Remove elet from linked list use remove()
import java.util.LinkedList;
class Main { public static void main(String[] args) {
LinkedList<String> languages = new LinkedList<>();
languages.add("Java");
languages.add("Python");
languages.add("Kotlin");
System.out.println("LinkedList: " + languages);
String str = languages.remove(1);
System.out.println("Removed Element: " + str);
System.out.println("Updated LinkedList: " + languages); } }