Module 4 Notes.docx
Module 4 Notes.docx
Linked List
Linked List
A linked list is a sequence of data structures, which are connected together via links. Linked List is a
sequence of links which contains items. Each link contains a connection to another link. Linked list is
the second most-used data structure after array. Following are the important terms to understand the
concept of Linked List.
● Link − Each link of a linked list can store a data called an element.
● Next − Each link of a linked list contains a link to the next link called Next.
● LinkedList − A Linked List contains the connection link to the first
link called First.
As per the above illustration, following are the important points to be considered.
● Linked List contains a link element called first.
● Each link carries a data field(s) and a link field called next.
● Each link is linked with its next link using its next link.
● Last link carries a link as null to mark the end of the list.
Data Storage Stores only values Stores values + pointers to next node
Implementation Complexity Simple and easy to use More complex due to pointers
One-dimensional,
Types Available Singly, Doubly, Circular Linked List
two-dimensional, etc.
Basic Operations
Following are the basic operations supported by a list.
● Insertion − Adds an element at the beginning of the list.
● Deletion − Deletes an element at the beginning of the list.
● Display − Displays the complete list.
● Search − Searches an element using the given key.
● Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in the linked list is a more than one step activity. We shall learn this
with diagrams here. First, create a node using the same structure and find the location
where it has to be inserted.
Imagine that we are inserting a node B (NewNode), between A (LeftNode)
and C (RightNode). Then point B.next to C −
Now, the next node at the left should point to the new node.
This will put the new node in the middle of the two. The new list should look like this −
Similar steps should be taken if the node is being inserted at the beginning of the list. While
inserting it at the end, the second last node of the list should point to the new node and the new
node will point to NULL.
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation.
First, locate the target node to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target
node −
This will remove the link that was pointing to the target node. Now, using the following
code, we will remove what the target node is pointing at.
We need to use the deleted node. We can keep that in memory otherwise we can simply
deallocate memory and wipe off the target node completely.
Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed by the head
node and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall make
it point to its previous node −
We have to make sure that the last node is not the lost node. So we'll have some temp node,
which looks like the head node pointing to the last node. Now, we shall make all left side
nodes point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes should point to their predecessor,
making them their new successor. The first node will point to NULL.
We'll make the head node point to the new first node by using the temp node.
Update Operation
The update operation in a singly linked list is used to modify the data of an existing node without changing
its structure or links.
Copying Operation
The copying operation creates a duplicate of an existing linked list, where each node in the new list has the
same data as the corresponding node in the original list.
A doubly linked list is a data structure that consists of a set of nodes, each of which contains a value and two
pointers, one pointing to the previous node in the list and one pointing to the next node in the list. This
allows for efficient traversal of the list in both directions, making it suitable for applications where
frequent insertions and deletions are required.
Insertion
A node can be added in four ways
1) At the front of the DLL
2) After a given node.
3) At the end of the DLL
4) Before a given node.
1) Add a node at the front: The new node is always added before the head of the given
Linked List. And newly added node becomes the new head of DLL. For example if the given Linked
List is A B C D and we add an item E at the front, then the Linked List becomes E A B C D. Let us
call the function that adds at the front of the list is push(). The push() must receive a pointer to the
head pointer, because push must change the head pointer to point to the new node
2)Add a node after a given node.:
Given a pointer to a node as prev_node, and the new node is inserted after the given node.
1. Check if the next_node is NULL or not. If it’s NULL, return from the function
because any new node can not be added before a NULL
2. Allocate memory for the new node, let it be called new_node
3. Set new_node->data = new_data
4. Set the previous pointer of this new_node as the previous node of the next_node,
new_node->prev = next_node->prev
5. Set the previous pointer of the next_node as the new_node, next_node->prev =
new_node
6. Set the next pointer of this new_node as the next_node, new_node->next =
next_node;
7. If the previous node of the new_node is not NULL, then set the next pointer of this
previous node as new_node, new_node->prev->next = new_node
Deletion
1) Deletion at the Beginning in Doubly Linked List
2) Deletion after a given node in Doubly Linked List
3) Deletion at a specific position in Doubly Linked List
4) Deletion at the End in Doubly Linked List
1) Deletion at the Beginning in Doubly Linked List
To update the head of a doubly linked list, follow these steps:
● Move the head to the next node in the list.
● If the new head is not empty (not NULL), set its previous pointer to NULL so that it no longer
points to the old head.
To delete a node after a specific node in a doubly linked list, follow these steps:
Find the Given Node:
● Look for the node that has the value you are given and store it in a variable called curr.
Check if There is a Next Node:
● If curr does not have a next node, there is nothing to delete, so just stop.
Find the Node to Delete:
● Store the next node (the one to be deleted) in a variable called nodeDelete.
Skip the Node to Be Deleted:
● Change curr’s next pointer to skip nodeDelete and point to the next node after it.
Fix the Backward Link (If Needed):
● If nodeDelete has another node after it, update its prev pointer to point back to curr.
Delete the Node:
● Free up the memory used by nodeDelete so it doesn’t take up space.
Uses less memory since it has only Uses more memory because each
3. Memory Usage
one pointer per node. node has two pointers.
Update Operation
The update operation modifies the data of an existing node without altering its structure or links.
Steps for Updating a Node in DLL
1. Traverse the list to find the node at a given position.
2. Change the data of that node.
3. If the position is invalid, return an error.
● Music and video playlists: Circular doubly linked lists are commonly used to implement music and video
playlists, allowing users to easily navigate between tracks and repeat the playlist once the last track is
reached.
● Browser history: Browser history can be stored using a circular doubly linked list, allowing users to
navigate back and forth between previously visited pages.
● Undo/redo functionality: Many applications use circular doubly linked lists to implement undo/redo
functionality, allowing users to undo or redo previous actions in the application.
To implement a stack using a singly linked list, we need to follow a simple rule: Last In, First Out (LIFO).
This means the last item added to the stack is the first one to be removed.
A top variable is used to keep track of the top element of the stack. With this concept, we can perform the
following operations:
1. push() – Adds a new element to the stack. This means inserting a new element at the beginning of the
linked list.
2. pop() – Removes the top element from the stack. This is done by deleting the first element of the linked
list.
3. peek() – Shows the top element without removing it.
4. display() – Prints all elements in the stack.
In a stack implementation using a singly linked list, we use a top pointer to keep track of the top element.
● The top is like the head of the list, where we add (push) and remove (pop) elements.
● The first node has null in its link field because there is no node before it.
● Each new node stores the address of the previous top node in its link field.
● The last added node is always at the top, and its address is stored in the top pointer.
This way, the stack follows the Last In, First Out (LIFO) rule.
Implementation of queue using Linked list:
Have you ever thought about how to manage data in the same order it arrives? A queue is a simple way to do this
using the First In, First Out (FIFO) method.
Using a linked list to create a queue makes it flexible and memory-efficient. It helps in handling tasks in order, like
managing resources or processing requests one by one.
A queue follows the FIFO rule, meaning the first item added is the first one to be removed. It has two main
operations:
o When the queue is empty, both should be NULL (this is done only once during initialization, not for every
enqueue).
o If FRONT is NULL, the queue is empty. Print a message like "Queue is empty! Nothing to dequeue."
2. If the queue is not empty:
At the end, prev will be the new head of the reversed linked list.