0% found this document useful (0 votes)
135 views8 pages

Double Linked List Guide

A double linked list is a two-way list where each node contains pointers to both the preceding and following nodes. This allows traversal in both directions through the list. Each node contains data and pointers to the next and previous nodes. Basic operations on a double linked list include creation, insertion, and deletion of nodes. Nodes are dynamically allocated and linked together using their pointers to form the double linked list structure.
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)
135 views8 pages

Double Linked List Guide

A double linked list is a two-way list where each node contains pointers to both the preceding and following nodes. This allows traversal in both directions through the list. Each node contains data and pointers to the next and previous nodes. Basic operations on a double linked list include creation, insertion, and deletion of nodes. Nodes are dynamically allocated and linked together using their pointers to form the double linked list structure.
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

DOUBLE LINKED LIST

A double linked list is a two-way list in which all nodes will have two links. This helps in
accessing both successor node and predecessor node from the given node position. It
provides bi-directional traversing. Each node contains three fields:

Left link.
Data.
Right link.

The left link points to the predecessor node and the right link points to the successor
node. The data field stores the required data.

Many applications require searching forward and backward thru nodes of a list. For
example searching for a name in a telephone directory would need forward and
backward scanning thru a region of the whole list.

The basic operations in a double linked list are:

Creation.
Insertion.
Deletion.

Dept. of CSE, KSRMCE Page 1


A double linked list is shown in figure 3.3.1.
HEAP
Stores the previous
S node address.
T
A
C X 10 200 100 20 300 200 30 X
K
100 200 300
1
0
0
s
t
a
r
t

The
start
point
er
holds
the
addre
ss of
the
first
node
of the
list.

Dept. of CSE, KSRMCE Page 2


Stores the data. Stores the next The right field of the
node address. last node is NULL.
The beginning of the double linked list is
stored in a "start" pointer which points to
Figure 3.3.1.
the first node. The first nodes left link
Double and
Linked
last nodes right link is set to NULL.
List

The following code gives the structure


definition:

struct dlinklist
{ node: left data right
struct dlinklist *left;
int data;
struct dlinklist *right;

}; start

typedef struct dlinklist node; Empty list: NULL


node *start = NULL;
Figure 3.4.1. Structure definition, double link node and empty list

Creating a node for Double Linked List:

Creating a double linked list starts with


creating a node. Sufficient memory has to
be allocated for creating a node. The
information is stored in the memory,
allocated by using the malloc() function.
The function getnode(), is used for creating
a node, after allocating memory for the
structure of type node, the information for
the item (i.e., data) has to be read from
the user and set left field to NULL and right
field also set to NULL (see figure 3.2.2).

node* getnode()
{
node* newnode;
newnode = (node *) malloc(sizeof(node)); newnode
printf("\n Enter data: "); X 10 X
scanf("%d", &newnode -> data);
100
newnode -> left = NULL;
newnode -> right = NULL;
return newnode;
}
Figure 3.4.2. new node with a value of 10

Dept. of CSE, KSRMCE Page 3


Creating a Double Linked List with n number of nodes:

The following steps are to be followed to create n number of nodes:

Get the new node using getnode().

newnode =getnode();

If the list is empty then start = newnode.

If the list is not empty, follow the steps given below:

The left field of the new node is made to point the previous node.

The previous nodes right field must be assigned with address of the
new node.

Repeat the above steps n times.

The function createlist(), is used to create n number of nodes:

vo id createlist(int n)
{
int i;
no de * new no de;
no de *tem p;
for(i = 0; i < n; i+ +)
{
new no de = getno de();
if(start = = NULL)
{
start = new no de;
}
else
{
tem p = start;
w hile(tem p - > right)
tem p = tem p - > right;
tem p - > right = new no de; new
no de - > left = tem p;
}
}
}

Figure 3.4.3 shows 3 items in a double linked list stored at different locations.

start
100

X 10 200 100 20 300 200 30 X


100 200 300

Figure 3.4.3. Double Linked List with 3 nodes


Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the list:

Get the new node using getnode().

newnode=getnode();

If the list is empty then start = newnode.

If the list is not empty, follow the steps given below:

newnode -> right = start;


start -> left = newnode;
start = newnode;

The function dbl_insert_beg(), is used for inserting a node at the beginning. Figure
3.4.4 shows inserting a node into the double linked list at the beginning.

start
400

400 10 200 100 20 300 200 30 X


100 200 300

X 40 100
400

Figure 3.4.4. Inserting a node at the beginning

Inserting a node at the end:

The following steps are followed to insert a new node at the end of the list:

Get the new node using

getnode() newnode=getnode();

If the list is empty then start = newnode.

If the list is not empty follow the steps given below:

temp = start;
while(temp -> right != NULL)
temp = temp -> right;
temp -> right = newnode;
newnode -> left = temp;

The function dbl_insert_end(), is used for inserting a node at the end. Figure 3.4.5
shows inserting a node into the double linked list at the end.
start
100

X 10 200 100 20 300 200 30 400


100 200 300

300 40 X
400

Figure 3.4.5. Inserting a node at the end

Inserting a node at an intermediate position:

The following steps are followed, to insert a new node in an intermediate position in the
list:

Get the new node using getnode().

newnode=getnode();

Ensure that the specified position is in between first node and last node. If
not, specified position is invalid. This is done by countnode() function.

Store the starting address (which is in start pointer) in temp and prev
pointers. Then traverse the temp pointer upto the specified position followed
by prev pointer.

After reaching the specified position, follow the steps given below:

newnode -> left = temp; newnode


-> right = temp -> right; temp ->
right -> left = newnode; temp ->
right = newnode;

The function dbl_insert_mid(), is used for inserting a node in the intermediate position.
Figure 3.4.6 shows inserting a node into the double linked list at a specified
intermediate position other than beginning and end.

start
100 40 200
100
400
400 20 300
X 10 400
200
100

200 30 X
300
Figure 3.4.6. Inserting a node at an intermediate position
Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:

If list is empty then display Empty List message.

If the list is not empty, follow the steps given below:

temp = start;
start = start -> right;
start -> left = NULL;
free(temp);

The function dbl_delete_beg(), is used for deleting the first node in the list. Figure 3.4.6
shows deleting a node at the beginning of a double linked list.

start
200

X 10 200 X 20 300 200 30 X


100 200 300

Figure 3.4.6. Deleting a node at beginning

Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:
If list is empty then display Empty List message

If the list is not empty, follow the steps given below:

temp = start;
while(temp -> right != NULL)
{
temp = temp -> right;
}
temp -> left -> right = NULL;
free(temp);

The function dbl_delete_last(), is used for deleting the last node in the list. Figure 3.4.7
shows deleting a node at the end of a double linked list.

start
100

X 10 200 100 20 X 20030 X


100 200 300

Figure 3.4.7. Deleting a node at the end


Deleting a node at Intermediate position:

The following steps are followed, to delete a node from an intermediate position in the list
(List must contain more than two nodes).

If list is empty then display Empty List message.

If the list is not empty, follow the steps given below:

Get the position of the node to delete.

Ensure that the specified position is in between first node and last node. If
not, specified position is invalid.

Then perform the following steps:


if(pos > 1 && pos < nodectr)
{
temp = start; i =
1;
while(i < pos)
{
temp = temp -> right; i+
+;
}
temp -> right -> left = temp -> left; temp
-> left -> right = temp -> right;
free(temp);
printf("\n node deleted..");
}

The function delete_at_mid(), is used for deleting the intermediate node in the list. Figure
3.4.8 shows deleting a node at a specified intermediate position other than beginning and
end from a double linked list.

start
100

X 10 300 100 20 300 100 30 X


100 200 300

Figure 3.4.8 Deleting a node at an intermediate position

You might also like