Department: MCA
Semester : II
Semester : II
Subject : Data Structures through C++
Paper : CCMCA 203
Faculty : Avinash Kumar
Syllabus covered in this blog
Linked Lists (Singly, Doubly & Circular)
Linked List
A linked list is a linear data
structure, in which the elements are not stored at contiguous memory locations.
The elements in a linked list are linked using pointers as shown in the below
image:
In
simple words, a linked list consists of nodes where each node contains a data
field and a reference (link) to the next node in the list.
Linked List and Array
Both Arrays and Linked List can be used to
store linear data of similar types, but they both have some advantages and
disadvantages over each other.
Key Differences between Array and Linked List
- An array is a data structure that contains a collection of similar type data elements whereas the Linked list is considered as non-primitive data structure contains a collection of unordered linked elements known as nodes.
- In array the elements belong to indexes, i.e., if you want to get into the fourth element you have to write the variable name with its index or location.
- In a linked list though, you have to start from the head and work your way through until you get to the desired element.
- Accessing an element in an array is fast, while Linked list takes linear time, so it is quite slower.
- Operations like insertion and deletion in arrays consume a lot of time. On the other hand, the performance of these operations in Linked lists is fast.
- Arrays are of fixed size. In contrast, Linked lists are dynamic and flexible and can expand and contract its size.
- In an array, memory is assigned during compile time while in a Linked list it is allocated during execution or runtime.
- Elements are stored consecutively in arrays whereas it is stored randomly in Linked lists.
- In addition memory utilization is inefficient in the array. Conversely, memory utilization is efficient in the linked list.
- Linked list provides the following two advantages over arrays
- Dynamic size
- Ease of insertion/deletion
- Linked lists have following drawbacks:
- Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do a binary search with linked lists.
- Extra memory space for a pointer is required with each element of the list.
Types of Linked List
- Singly Linked List.
- Doubly Linked List.
- 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:
class Node
{
int Data;
Node * Next;
};
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 Node
{
int Data;
Node * Next;
Node * Prev;
};
The nodes are connected to each other in this form where the first
node has
prev = NULL and
the last node has next = NULL
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.
- The insert operation is more efficient if the node is given before which insertion should take place.
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. 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.
- We can easily traverse to its previous node in a circular linked list, which is not possible in a singly linked list.
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.
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)
{
while(head != NULL)
{
cout<<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
If
the list is empty, we make the new node as the head of the list. Otherwise, we
have to connect the new node to the current head of the list and make the new
node, the head of the list.
Node insertAtBegin(Node
*head, int val)
{
newNode = new Node(val);
if(head == NULL)
return newNode;
else
{
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.
Node insertAtEnd(Node *head,
int val)
{
if( head == NULL )
{
newNode = new Node(val);
head = newNode;
return head;
}
Node
*temp = head;
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;
}
Linked List node Deletion
To
delete a node from a linked list, we need to do these steps:
- 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.
Node deleteLL(Node *head,
Node *del)
{
if(head == del)
{
return head->next;
}
Node *temp = head;
while( temp->next != NULL )
{
if(temp->next == del)
{
temp->next =
temp->next->next;
delete del;
}
temp = temp->next;
}
return head;
}
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;
while( temp != NULL)
{
if( temp->data == val )
return true;
temp = temp->next;
}
return false;
}
No comments:
Post a Comment