Data Structure and Algorithm Unit 3
Data Structure and Algorithm Unit 3
Unit-3
Linked Lists
Data Structure
Data structure is a way of storing and organizing data efficiently such that the required
operations on them can be performed be efficient with respect to time as well as memory.
Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code.
Data structures can be two types: 1. Static Data Structure 2. Dynamic Data Structure
Arrays, Stacks, Queues, Trees Lists, Trees (with variable size), Hash
Examples
(with fixed size) tables
In this representation, each box represents a node, with an arrow indicating the link to the next
node. The last node points to NULL, indicating the end of the list.
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node in
the list, and a pointer to the previous node in the list. By linking these nodes together through
the next and prev pointers, we can traverse the list in both directions (forward and backward),
which is a key feature of a Doubly Linked List.
To insert a new node at the beginning of the doubly list, we can use the following steps:
Create a new node, say new_node with the given data and set its previous pointer to
null, new_node->prev = NULL.
Set the next pointer of new_node to current head, new_node->next = head.
If the linked list is not empty, update the previous pointer of the current head to
new_node, head->prev = new_node.
Return new_node as the head of the updated linked list.
To insert a new node at the end of the doubly linked list, we can use the following steps:
Allocate memory for a new node and assign the provided value to its data field.
Initialize the next pointer of the new node to nullptr.
If the list is empty:
o Set the previous pointer of the new node to nullptr.
o Update the head pointer to point to the new node.
If the list is not empty:
o Traverse the list starting from the head to reach the last node.
o Set the next pointer of the last node to point to the new node.
o Set the previous pointer of the new node to point to the last node.
To delete a node at the beginning in doubly linked list, we can use the following steps:
Check if the list is empty, there is nothing to delete. Return.
Store the head pointer in a variable, say temp.
Update the head of linked list to the node next to the current head, head = head->next.
If the new head is not NULL, update the previous pointer of new head to NULL, head-
>prev = NULL.
To delete a node at the end in doubly linked list, we can use the following steps:
Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
If the list is not empty, then move to the last node of the doubly linked list, say curr.
Update the second-to-last node's next pointer to NULL, curr->prev->next = NULL.
Free the memory allocated for the node that was deleted.
To delete a node at a specific position in doubly linked list, we can use the following steps:
Traverse to the node at the specified position, say curr.
If the position is valid, adjust the pointers to skip the node to be deleted.
o If curr is not the head of the linked list, update the next pointer of the node
before curr to point to the node after curr, curr->prev->next = curr-next.
o If curr is not the last node of the linked list, update the previous pointer of the
node after curr to the node before curr, curr->next->prev = curr->prev.
Free the memory allocated for the deleted node.
Easy insertion and deletion of nodes: The presence of pointers to both the previous and
next nodes makes it easy to insert or delete nodes from the list, without having to traverse
the entire list.
Can be used to implement a stack or queue: Doubly linked lists can be used to
implement both stacks and queues, which are common data structures used in
programming.
SLL nodes contains 2 field -data field DLL nodes contains 3 fields -data field, a
and next link field. previous link field and a next link field.
In SLL, the traversal can be done In DLL, the traversal can be done using the
using the next node link only. Thus previous node link or the next node link. Thus
traversal is possible in one direction traversal is possible in both directions (forward
only. and backward).
The SLL occupies less memory than The DLL occupies more memory than SLL as
DLL as it has only 2 fields. it has 3 fields.