You are on page 1of 11

LINKED LIST

What is a Linked List?

It is basically chains of nodes, each node contains information such


as data and a pointer to the next node in the chain. In the linked list there is
a head pointer, which points to the first element of the linked list, and if the list
is empty then it simply points to null or nothing.
Why linked list data structure needed?
Here are a few advantages of a linked list that is listed below, it will help
you understand why it is necessary to know.
 Dynamic Data structure: The size of memory can be allocated or de-
allocated at run time based on the operation insertion or deletion.
 Ease of Insertion/Deletion: The insertion and deletion of elements are
simpler than arrays since no elements need to be shifted after insertion and
deletion, Just the address needed to be updated.
 Efficient Memory Utilization: As we know Linked List is a dynamic data
structure the size increases or decreases as per the requirement so this avoids
the wastage of memory.
 Implementation: Various advanced data structures can be implemented
using a linked list like a stack, queue, graph, hash maps, etc.

Applications of Linked List:


Here are some of the applications of a linked list:
 Linear data structures such as stack, queue, and non-linear data structures such as
hash maps, and graphs can be implemented using linked lists.
 Dynamic memory allocation: We use a linked list of free blocks.
 Implementation of graphs: Adjacency list representation of graphs is the most
popular in that it uses linked lists to store adjacent vertices.
 In web browsers and editors, doubly linked lists can be used to build a forwards
and backward navigation button.
 A circular doubly linked list can also be used for implementing data structures like
Fibonacci heaps.

Applications of Linked Lists in real world:


 The list of songs in the music player is linked to the previous and next songs.
 In a web browser, previous and next web page URLs are linked through the
previous and next buttons.
 In the image viewer, the previous and next images are linked with the help of the
previous and next buttons.
 Switching between two applications is carried out by using “alt+tab” in windows
and “cmd+tab” in mac book. It requires the functionality of a circular linked list.
 In mobile phones, we save the contacts of people. The newly entered contact
details will be placed at the correct alphabetical order.
 This can be achieved by a linked list to set contact at the correct alphabetical
position.
 The modifications that we made in the documents are actually created as nodes in
doubly linked list. We can simply use the undo option by pressing Ctrl+Z to
modify the contents. It is done by the functionality of a linked list.

Types of linked lists:


There are mainly three types of linked lists:
1. Single-linked list
2. Double linked list
3. Circular linked list

1. Singly-linked list
Traversal of items can be done in the forward direction only due to the linking
of every node to its next node.
The following operation of the Single linked list:

 Inserting at head
 Inserting at tail
 Inserting after a node
 Inserting before a node
 Delete the head node
 Delete the tail node
 Search and Delete a node
 Traversing the Linked List

Here’s an example of a linked list with four nodes.

Example of a Singly Linked List

Insertion at the head of a Singly Linked List


This is a simple operation. Generally, it’s known as pushing a singly linked list.
You need to create a new node and place this at the head of the linked list.

To perform this operation, we need to follow two important conditions. They’re

1. If the list is empty, then the newly created node will be the head node, and
the next node of the head will be ”NULL”.
2. If the list is not empty, the new node will be the head node, and the next
will point to the previous head node.

Here’s the pseudo-code for inserting a node at the head of a linked list:

function insertAtHead( head, value ):


newNode = Node(value)
if head is NULL:
head = newNode
return head
else:
newNode.next = head
return newNode
Inserting at the head

Insertion at the end of a Singly Linked List


Inserting a node at the end of a linked list has some similarities with
inserting in the head. All you need to do is, traverse to the end node or the tail
node. Then point the “next” node of the end node to the newly created node. If
the head node is null, the new node will be the head node.

Here’re the steps:

Step 1) Traverse until the “next” node of the current node becomes null.

Step 2) Create a new node with the specified value.

Step 3) Assign the new node as the next node of the tail node.

The pseudo-code for inserting a node at the tail of a singly list:

function insertAtEnd( head, value ):


newNode = Node(value)
if head is NULL:
head = newNode
return head
while head.next is not NULL:
then head = head.next
head.next = newNode
newNode.next = NULL
Inserting at the tail

Insertion after a node in a Singly Linked List


Inserting after a node has two parts: Search for the node and attach after the
searched node. We need to traverse all the nodes. For each node, we need to match
with the search element. If there’s a match, then we will add the new node after
the searched node.

Here’re the steps:

Step 1) Traverse the next node until the value of the current node equals the
search item.

Step 2) New node’s next pointer will be the current node’s next pointer.

Step 3) Current node’s next node will be the new node.

Here’s the pseudo-code for inserting a node after a node:

function insertAfter( head, value, searchItem ):


newNode = Node(value)
while head.value equals searchItem:
then head = head.next
newNode.next = head.next.next
head.next = newNode
Inserting a node after a node in Singly Linked List

Insertion before a node in a Singly Linked List


This function is much similar to the insertion after a node. We must create a new
node and traverse the linked list until the current node matches the search node.
After that, we will add the new node as the previous node of the current node.

Here’re the steps:

Step 1) Traverse until the next node’s value equals the search item.

Step 2) Create a new node and assign the node’s “next” with the next to the next
node of the current node.

Step 3) Assign the new node as the next node of the current node.

Here’s the pseudo-code:

function insertBefore( head, value, searchItem ):


newNode = Node(value)
while head.next.value equals searchItem:
then head = head.next
newNode.next = head.next.next
head.next = newNode

Inserting a node before a node in Singly Linked List

Delete the head of the singly linked list


In every function of the linked list, the head pointer is provided as the
parameter. You need to delete the head node and make the next node of the head
node as the new head of the linked list. We also need to free the memory of the
deleted node. Otherwise, the memory will be marked as occupied when another
program tries to access it.

Here’re the steps for deleting the head of the singly linked list:

Step 1) Assign the next node of the head node as the new head.

Step 2) Free the allocated memory by the previous head node.

Step 3) Return the new head node.


The pseudo-code for deleting the head node:

function deleteHead( head ):


temp = head
head = head.next
free( temp )
return head

Deleting the head of a linked list

Delete the tail of the singly linked list


Deleting the tail node is more familiar with deleting the head node. The difference
is that we need to traverse to the end of the linked list and delete it. In the singly
linked list, the node with the next node as “NULL” is the tail node.

Here’re the steps for deleting the tail node of the linked list:

Step 1) Traverse before the tail node. Save the current node.

Step 2) Free the memory of the next node of the current node.

Step 3) Set the next node of the current node as NULL.

Here’s the pseudo-code for deleting the tail node:

function deleteTail( head ):


while head.next.next is not NULL:
head = head.next
free( head.next )
head.next = NULL
Deleting the tail of Singly Linked List

Search and delete a node from a singly linked list


This function has two tasks, search and delete. We need to search until the end of
the singly linked lists. If we find any similar node, we will delete that one. After
that, we need to merge or link the left and right nodes of the deleted node.

Here’re the steps for doing this:

Step 1) Traverse until the end of the linked list. Check if the current node is equal
to the search node or not.

Step 2) If any node matches, store the node pointer to the current node.

Step 3) The “next” of the previous node will be the next node of the current node.

Step 4) Delete and free the memory of the current node.

Pseudo-code for search and delete a node from a singly linked list:

function searchAndDelete( head, searchItem ):


while head.next.next is not NULL and head.next.value is not equals
searchItem :
head = head.next
head.next = head.next.next
delete(head.next)
Search and delete a node from Singly Linked List

Display

The components of a single linked list can be displayed using the methods below:

Step 1: Determine whether or not the list is empty (head == NULL).


Step 2: If the list is empty, show “List is Empty!!!” and exit the method.
Step 3: If it is not empty, create a ‘temp’ Node pointer and initialize it with head.
Step 4: Continue to display temp →data with an arrow (—>) until the temp
reaches the last node.
Step 5: Finally, show temp →data with an arrow pointing to NULL
(temp →data —> NULL).

You might also like