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

DSA NOTES 3

Double linked list cpp

Uploaded by

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

DSA NOTES 3

Double linked list cpp

Uploaded by

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

Data Structures and Algorithms

Lab 8

Lab Instructor: Riaz Ahmad


Course Code: CC2042L

Session: Fall 2023


School of Systems and Technology
UMT Lahore Pakistan
Link List
What is Linked List:
A linked list is a linear data structure, in which the elements are not stored at con-
tiguous memory locations. The elements in a linked list are linked using pointers as
shown in the below image:

Linked-List-Data-Structure

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.

Doubly Link List


Inserting a new node in a doubly linked list is very similar to inserting
new node in linked list. There is a little extra work required to main-
tain the link of the previous node. A node can be inserted in a Doubly
Linked List in four ways:
 At the front of the DLL.
 In between two nodes
 After a given node.
 Before a given node.
 At the end of the DLL.
Add a node at the front in a Doubly Linked List:
The new node is always added before the head of the given Linked
List. The task can be performed by using the following 5 steps:
1. Firstly, allocate a new node (say new_node).
2. Now put the required data in the new node.
3. Make the next of new_node point to the current head of the
doubly linked list.
4. Make the previous of the current head point to new_node.
5. Lastly, point head to new_node.

Add a node in between two nodes:


It is further classified into the following two parts:
Add a node after a given node in a Doubly Linked List:
We are given a pointer to a node as prev_node, and the new node is
inserted after the given node. This can be done using the following 6
steps:
1. Firstly create a new node (say new_node).
2. Now insert the data in the new node.
3. Point the next of new_node to the next of prev_node.
4. Point the next of prev_node to new_node.
5. Point the previous of new_node to prev_node.
6. Change the pointer of the new node’s previous pointer
to new_node.
Add a node before a given node in a Doubly Linked List:
Let the pointer to this given node be next_node. This can be done us-
ing the following 6 steps.
1. Allocate memory for the new node, let it be called new_node.
2. Put the data in new_node.
3. Set the previous pointer of this new_node as the previous
node of the next_node.
4. Set the previous pointer of the next_node as the new_node.
5. Set the next pointer of this new_node as the next_node.
6. Now set the previous pointer of new_node.
 If the previous node of the new_node is not NULL,
then set the next pointer of this previous node
as new_node.
 Else, if the prev of new_node is NULL, it will be the
new head node.

Add a node at the end in a Doubly Linked List:


The new node is always added after the last node of the given Linked
List. This can be done using the following 7 steps:
1. Create a new node (say new_node).
2. Put the value in the new node.
3. Make the next pointer of new_node as null.
4. If the list is empty, make new_node as the head.
5. Otherwise, travel to the end of the linked list.
6. Now make the next pointer of last node point to new_node.
7. Change the previous pointer of new_node to the last node of
the list.

Tasks:
You need to implement the concept of Doubly Link List:

[1] Insertion Functions Includes:


 Add At Head
 Add At Tail
 Add At Specific Position

[2] Deletion Function Includes:


 Delete from Head
 Delete from Tail

[3] Display the Doubly Link List


[4] Sort
[5] Search

Happy Learning!!!

You might also like