0% found this document useful (0 votes)
15 views

Properties of Linked List

fghfghfg

Uploaded by

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

Properties of Linked List

fghfghfg

Uploaded by

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

VSRGDC

Linked Lists and Its Properties

Properties of Linked List


 It can be visualized as a chain of nodes where each node contains the location of
the next node. You can see this in the diagram given below:

 The structure of the node is

class Node{
int val // variable storing the data of each node
Node next // variable storing the address of the next blog
}

 The first node of the linked list is called the head of the linked list . Through
head, we can perform different operations on the linked list. In every linked list
question, we will be given the reference of the head node of the linked list.
 The last node of the linked list is pointing to NULL (None) which indicates that it
is the last node.
 Unlike arrays, linked list elements are not stored at contiguous memory locations.
 Linked Lists addresses some of the limitations of arrays of having a fixed size
because Linked Lists are dynamic in nature.

Advantages and Disadvantage of Linked list


PROS
VSRGDC
 They are dynamic in nature which allocates the memory when required.
 Insertion and deletion operations can be easily implemented.
CONS

 The memory is wasted as pointers require extra memory for storage.


 No element can be accessed randomly, it has to access each node sequentially i.e.
proper traversal must be done.
 Reverse Traversing is difficult in the linked list(though we can achieve this with
the help of Doubly Linked List).

Applications of Linked Lists


 Linked lists are used to implement stacks, queues, graphs, etc.
 Any application which has to deal with an unknown number of objects will need to
use a linked list.

Types of Linked List and Operation on Linked List

Types of Linked List


Following are the types of linked list

1. Singly Linked List.


2. Doubly Linked List.
3. Circular Linked List.

Singly Linked List


A Singly-linked list is a collection of nodes linked together in a sequential way where
each node of the singly linked list contains a data field and an address field that contains
the reference of the next node.

The structure of the node in the Singly Linked List is


VSRGDC
class Node {
int data // variable to store the data of the node
Node next // variable to store the address of the next node
}
The nodes are connected to each other in this form where the value of the next variable
of the last node is NULL i.e. next = NULL , which indicates the end of the linked list.

Doubly Linked List


A Doubly Linked List contains an extra memory to store the address of the previous
node, together with the address of the next node and data which are there in the singly
linked list. So, here we are storing the address of the next as well as the previous nodes.

The following is the structure of the node in the Doubly Linked List(DLL):

class DLLNode {
int val // variable to store the data of the node
DLLNode prev // variable to store the address of the previous node
DLLNode next // variable to store the address of the next node
}
The nodes are connected to each other in this form where the first node has prev =
NULL and the last node has next = NULL .
VSRGDC

Advantages over Singly Linked List-

 It can be traversed both forward and backward direction.


 The delete operation is more efficient if the node to be deleted is given. ( Think!
you will get the answer in the second half of this blog)
 The insert operation is more efficient if the node is given before which insertion
should take place. ( Think! )
Disadvantages over Singly Linked List-

 It will require more space as each node has an extra memory to store the address
of the previous node.
 The number of modification increase while doing various operations like
insertion, deletion, etc.

Circular Linked List


A circular linked list is either a singly or doubly linked list in which there are
no NULL values. Here, we can implement the Circular Linked List by making the use of
Singly or Doubly Linked List. In the case of a singly linked list, the next of the last node
contains the address of the first node and in case of a doubly-linked list, the next of last
node contains the address of the first node and prev of the first node contains the
address of the last node.

Advantages of a Circular linked list

 The list can be traversed from any node.


 Circular lists are the required data structure when we want a list to be accessed in
a circle or loop.
VSRGDC
 We can easily traverse to its previous node in a circular linked list, which is not
possible in a singly linked list. ( Think!)
Disadvantages of Circular linked list

 If not traversed carefully, then we could end up in an infinite loop because here we
don't have any NULL value to stop the traversal.
 Operations in a circular linked list are complex as compared to a singly linked list
and doubly linked list like reversing a circular linked list, etc.

Basic Operations on Linked List


 Traversal : To traverse all the nodes one after another.
 Insertion : To add a node at the given position.
 Deletion : To delete a node.
 Searching : To search an element(s) by value.
 Updating : To update a node.
 Sorting: To arrange nodes in a linked list in a specific order.
 Merging: To merge two linked lists into one.
We will see the various implementation of these operations on a singly linked list.

Following is the structure of the node in a linked list:

class Node{
int data // variable containing the data of the node
Node next // variable containing the address of next node
}

Linked List Traversal


The idea here is to step through the list from beginning to end. For example , we may
want to print the list or search for a specific node in the list.

The algorithm for traversing a list

 Start with the head of the list. Access the content of the head node if it is not null.
 Then go to the next node(if exists) and access the node information
 Continue until no more nodes (that is, you have reached the null node)
void traverseLL(Node head) {
VSRGDC
while(head != NULL)
{
print(head.data)
head = head.next
}
}

Linked List node Insertion


There can be three cases that will occur when we are inserting a node in a linked list.

 Insertion at the beginning


 Insertion at the end. (Append)
 Insertion after a given node
Insertion at the beginning
Since there is no need to find the end of the list. If the list is empty, we make the new
node as the head of the list. Otherwise, we we have to connect the new node to the
current head of the list and make the new node, the head of the list.

// function is returning the head of the singly linked-list


Node insertAtBegin(Node head, int val)
{
newNode = new Node(val) // creating new node of linked list
VSRGDC
if(head == NULL) // check if linked list is empty
return newNode
else // inserting the node at the beginning
{
newNode.next = head
return newNode
}
}
Insertion at end

We will traverse the list until we find the last node. Then we insert the new node to the
end of the list. Note that we have to consider special cases such as list being empty.

In case of a list being empty, we will return the updated head of the linked list because in
this case, the inserted node is the first as well as the last node of the linked list.

// the function is returning the head of the singly linked list


Node insertAtEnd(Node head, int val)
{
if( head == NULL ) // handing the special case
{
newNode = new Node(val)
head = newNode
return head
}
Node temp = head
VSRGDC
// traversing the list to get the last node
while( temp.next != NULL )
{
temp = temp.next
}
newNode = new Node(val)
temp.next = newNode
return head
}
Insertion after a given node

We are given the reference to a node, and the new node is inserted after the given node.

void insertAfter(Node prevNode, int val)


{
newNode = new Node(val)

newNode.next = prevNode.next
prevNode.next = newNode
}
NOTE: If the address of the prevNode is not given, then you can traverse to that node
by finding the data value.

Linked List node Deletion


To delete a node from a linked list, we need to do these steps
VSRGDC
 Find the previous node of the node to be deleted.
 Change the next pointer of the previous node
 Free the memory of the deleted node.
In the deletion, there is a special case in which the first node is deleted. In this, we need
to update the head of the linked list.

// this function will return the head of the linked list


Node deleteLL(Node head, Node del)
{
if(head == del) // if the node to be deleted is the head node
{
return head.next // special case for the first Node
}
Node temp = head

while( temp.next != NULL )


{
if(temp.next == del) // finding the node to be deleted
{
temp.next = temp.next.next
delete del // free the memory of that Node
return head
}
temp = temp.next
}
return head // if no node matches in the Linked List
}
VSRGDC

Linked List node Searching


To search any value in the linked list, we can traverse the linked list and compares the
value present in the node.

bool searchLL(Node head, int val)


{
Node temp = head // creating a temp variable pointing to the head of the
linked list
while( temp != NULL) // traversing the list
{
if( temp.data == val )
return true
temp = temp.next
}
return false
}

Linked List node Updation


To update the value of the node, we just need to set the data part to the new value.

Below is the implementation in which we had to update the value of the first node in
which data is equal to val and we have to set it to newVal .

void updateLL(Node head, int val, int newVal)


{
Node temp = head
while(temp != NULL)
{
if( temp.data == val)
{
temp.data = newVal
return
}
temp = temp.next
}
}
VSRGDC
Array vs Linked List

Array and Linked List are the two most used data structures. It's really important to
understand and compare the advantages and disadvantages of both array and linked
list. In this blog, we will compare these two linear data structures. So let’s get starte d.

To start of the things, we will take some parameters through which we can compare both
the data structures.

Structure

Array and Linked list are used to store linear data of similar type but the major
difference between them is related to their structure. Arrays are an index-based data
structure where each element is associated with an index. On the other hand, Linked list
relies on references of where the next element in the list is stored, the last element of
the linked list refers to NULL denoting that its the end of the list.

Arrays

Linked List

Size

Arrays have fixed size and it is required to know the size of the array at the time of
declaration whereas Linked List is not restricted to size and can be expanded during the
execution. So, Linked lists are dynamic, flexible.

Memory Required
VSRGDC
The memory required to store data in the linked list is more than that of an array
because of additional memory used to store the address/references of the next node.

Storage Allocation

In an array, memory is assigned during compile time while in a Linked list it is allocated
during execution or runtime.

The elements in the array are stored at contiguous positions in the memory whereas the
elements in the linked list are stored at random positions.

Accessing Time

Elements in the array can be accessed using it’s index i.e. if you want to get into the
fourth element you have to write the array variable name with its index or location
within the square bracket. But, in a linked list, you have to start from the head and
iterate until you get to the fourth element.

The elements in an array can be directly and sequentially accessed but in a linked list
only sequential access is possible. To conclude, accessing an element in an array is fast
and is a constant time operation whereas, in a linked list, it takes linear time.

Memory utilization

Memory utilization is inefficient in the array as we need to declare the size of the array
during the time of declaration. Conversely, memory utilization is efficient in the linked
list.

Insertion/ Deletion

Operations like insertion and deletion in arrays consume a lot of time as shifting of
elements are required but these operations are easy, fast and efficient in linked lists.

Conclusion
From the above points, we can conclude that Array and Linked lists are the types of data
structures that differ in their structure, accessing and manipulation methods, memory
requirement, and utilization and have particular advantages and disadvantages over its
implementation.

Linked List: Dynamic Size, Fast insertion, and deletion once the element is reached

Arrays: Fast Random element access and requires less memory per element
VSRGDC

You might also like