Data Structure Practical-4-1
Data Structure Practical-4-1
Experiment No: 4
4.1 Write a program to implement following operations on the doubly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete a last node of the linked list.
(d) Delete a node before specified position.
Date: 9/10/23
Theory:
A doubly linked list is a data structure where each node contains data and two pointers - one to point
to the previous node (LPTR) and another to point to the next node (RPTR). The main advantage of
a doubly linked list is that we can traverse it in any direction, either forward or backward. Another
advantage is that we can delete a node with ease since we have pointers to both the previous and
next nodes. In contrast, a node on a singly linked list cannot be removed unless we have a pointer
to its predecessor. However, the drawback of a doubly linked list is that it requires more memory
than a singly linked list since we need an extra pointer to point to the previous node. In the image,
L and R denote the leftmost and rightmost nodes in the list, respectively. The left link of the L node
and the right link of the R node are both NULL, indicating the end of the list for each direction.
Page No
Data Structure (3130702) Enrollment No
Operations on doubly linked list
✓ Insert
- Insert at first position
- Insert at last position
- Insert into ordered list
✓ Delete
✓ Traverse list (Print list)
✓ Copy linked list
Page No
Data Structure (3130702) Enrollment No
4.1 Write a program to implement following operations on the doubly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete a last node of the linked list.
(d) Delete a node before specified position.
Program:
Page No
Data Structure (3130702) Enrollment No
Output:
Page No
Data Structure (3130702) Enrollment No
(b) Insert a node at the end of the linked list.
Program:
Page No
Data Structure (3130702) Enrollment No
Output:
Page No
Data Structure (3130702) Enrollment No
(c) Delete a last node of the linked list.
Program:
Page No
Data Structure (3130702) Enrollment No
Output:
Page No
Data Structure (3130702) Enrollment No
(d) Delete a node before specified position.
Program:
if (h == NULL)
{
printf("-->linked list is empty\n");
return;
}
else if (h->next == NULL)
{
printf("-->can not delete node only one node\n");
return;
}
printf("enter data:");
scanf("%d", &d);
if (h->data == d)
{
printf("-->can not delete node before first node\n");
free(p);
free(temp);
return;
}
else
{
p = h;
temp = h->next;
if (temp->data == d)
{
temp = h;
h = h->next;
free(p);
free(temp);
return;
}
else
{
while (temp->next != NULL && temp->next->data != d)
{
Page No
Data Structure (3130702) Enrollment No
p = temp;
temp = temp->next; //Traversal in link list until specified node or end
}
if(temp->next == NULL)
{
printf("-->%d not found\n", d); //Given node not found
return;
}
if (temp->next->data == d)
{
p->next = temp->next;
temp->next->prev = temp->prev;
free(temp); //Deletion of node before specified node
return;
}
}
}
}
Output:
Page No
Data Structure (3130702) Enrollment No
Observations:
A doubly linked list is a linear data structure where each node has a value and two pointers,
one pointing to the next node and another to the previous node. This bidirectional linkage enables
efficient traversal in both forward and backward directions.
Conclusion:
In a doubly linked list, each node not only points to the next node in the sequence (like in a
singly linked list) but also points to the previous node. This bidirectional linkage allows for more
flexible traversal. You can easily navigate both forward, from the head to the tail, and backward,
from the tail to the head. This enhanced capability makes doubly linked lists more versatile for
various operations, but they do consume slightly more memory compared to singly linked lists due
to the additional pointer for the previous node.
Quiz:
(1) Explain structure of a node of doubly link list
(2) Which is the main advantage of doubly link list?
(3)What is the drawback of doubly link list?
Ans:
(1) In doubly link list structure of a node contains one data field and two pointer field
in which one has address of previous node and other one has address of next node.
(2) Bidirectional traversal: the main advantage of doubly linked lists is the ability to traverse
the list in both forward and reverse directions. This feature is valuable for operations that
require accessing elements in a reverse order.
(3) Increased Memory Usage: Each node in a doubly linked list requires two pointers, one
for the next node and one for the previous node. This increased memory usage can be a
drawback when memory efficiency is a concern. Singly linked lists only use one pointer per
node.
Marks
Page No