0% found this document useful (0 votes)
3 views11 pages

Data Structure Practical-4-1

The document outlines an experiment on implementing operations for a doubly linked list, including node insertion at the front and end, deletion of the last node, and deletion of a node before a specified position. It provides theoretical background on doubly linked lists, their advantages and disadvantages, and includes sample code for each operation. The conclusion emphasizes the bidirectional traversal capability of doubly linked lists while noting their increased memory usage compared to singly linked lists.

Uploaded by

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

Data Structure Practical-4-1

The document outlines an experiment on implementing operations for a doubly linked list, including node insertion at the front and end, deletion of the last node, and deletion of a node before a specified position. It provides theoretical background on doubly linked lists, their advantages and disadvantages, and includes sample code for each operation. The conclusion emphasizes the bidirectional traversal capability of doubly linked lists while noting their increased memory usage compared to singly linked lists.

Uploaded by

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

Data Structure (3130702) Enrollment No

Experiment No: 4

AIM : Doubly linked list

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

Competency and Practical Skills: Logic building and programming

Relevant CO: CO2, CO5

Objectives: (a) To understand the concepts of doubly linked list


(b) To analyze different algorithms on doubly link list
(c) To implement various operations on doubly link list

Equipment/Instruments: Computer System with turbo C/C++

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

Theory:

Doubly linked list

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.

(a) Insert a node at the front of the linked list.

Program:

void insertbeg() // Insert a node at the front of the linked list


{
struct node *nn = malloc(sizeof(struct node));
if (nn == NULL)
{
printf("-->memory not allocated");
return;
}
printf("enter data: ");
scanf("%d", &nn->data);
nn->prev = NULL;
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
nn->next = h;
h->prev = nn;
h = nn;
}
}

Page No
Data Structure (3130702) Enrollment No
Output:

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:1
enter data: 47

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 47

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:1
enter data: 46

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 46 47

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:1
enter data: 44

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 44 46 47

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:1
enter data: 43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43 44 46 47

Page No
Data Structure (3130702) Enrollment No
(b) Insert a node at the end of the linked list.

Program:

void insertend() // Insert a node at the end of the linked list.


{
struct node *cur = h;
struct node *nn = (struct node *)malloc(sizeof(struct node));
if (nn == NULL)
{
printf("-->memory not allocated");
return;
}
printf("enter data: ");
scanf("%d", &nn->data);
nn->prev = NULL;
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
while (cur->next != NULL)
{
cur = cur->next; //Traversal in link list to the end
}
nn->prev = cur;
cur->next = nn;
}
}

Page No
Data Structure (3130702) Enrollment No
Output:

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:2
enter data: 43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:2
enter data: 44

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43 44

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:2
enter data: 46

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43 44 46

Page No
Data Structure (3130702) Enrollment No
(c) Delete a last node of the linked list.

Program:

void deleteend() // Delete a last node of the linked list


{
if (h == NULL)
{
printf("-->link list is empty\n");
return;
}
struct node *p = h;
struct node *temp = h->next;
if (temp == NULL)
{
h = NULL;
free(p);
return;
}
else
{
while (temp->next != NULL)
{
p = temp;
temp = temp->next; //Traversal in link list to the end
}
p->next = NULL;
free(temp); //Deletion of the last node
return;
}
}

Page No
Data Structure (3130702) Enrollment No
Output:

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43 44 46

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:3

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display

enter choice accordingly:5


link list: 43 44

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:3

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:3

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
-->link list is empty

Page No
Data Structure (3130702) Enrollment No
(d) Delete a node before specified position.

Program:

void deletebsn() // Delete a node before specified node


{
struct node *temp, *p;
int d;

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:

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 46 44 43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:4
enter data:43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 46 43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:4
enter data:43

0.stop 1.insert at front 2.insert at end


3.delete end 4.delete before 5.display
enter choice accordingly:5
link list: 43

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.

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

Page No

You might also like