DOUBLY LINKED LIST
Doubly Linked list
In doubly linked list two address files are used
one for storing the address of next node and
the other for storing the address of previous
node.
Prev stores the address of previous node
Info stores the actual data
Next stores the address of next instructions
Doubly Linked List
Thedata representation in doubly liked list
looks as follows
Operations on Doubly Linked list
Creation of the list
Insertion at the Front end
Insertion a the rear end
Insertion at a specified position
Deletion at the Front end
Deletion at the Rear end
Deletion of a specified node
Traversal ( Display )
Creation of list
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
Algorithm of insertion at the beginning
Create a new node
Assign its data value
Assign newly created node’s next ptr to current head
reference. So, it points to the previous start node of
the linked list address
Assign newly created node’s previous node to NULL
Assign the current head’s previous node to this new
node
Change the head reference to the new node’s
address.
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
Algorithm of insertion at the end
Create a new node
Assign its data value
Traverse till the end of the Linked List call this
node temp
Assign newly created node’s next node to NULL
Assign newly created node’s previous node to
temp
Assign Temp’s next node to this newly created
node.
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
Algorithm of insertion at the specified positon
Create a new node
Assign its data value
Traverse till nth(pos) node lets call this temp
Assign newly created node’s next node to
temp’s next node
Assign newly created node’s previous node to
temp
Assign Temp’s next node to this newly created
node.
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL) printf("Enter value");
{
scanf("%d",&item);
printf("\n OVERFLOW");
} ptr->data = item;
else ptr->next = temp->next;
{ ptr -> prev = temp;
temp=head;
printf("Enter the location");
temp->next->prev=ptr;
scanf("%d",&loc); temp->next = ptr;
for(i=0;i<loc;i++) printf("\nnode inserted\n");
{
temp = temp->next;
}
if(temp == NULL) }
{
printf("\n There are less than %d elements", loc);
return;
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deleteAtPosition(Node** head, int position) {
if (*head == nullptr || position <= 0) {
// List is empty or invalid position
return;
}
Node* temp = *head;
// Traverse to the node at the specified position
for (int i = 1; temp != nullptr && i < position; i++) { else {
temp = temp->next; // If the node to be deleted is
} the head node
// If the position is greater than the number of nodes *head = temp->next;
if (temp == nullptr) { }
return;
}
if (temp->next != nullptr) {
// Adjust the pointers
temp->next->prev =
if (temp->prev != nullptr) {
temp->prev->next = temp->next;
temp->prev;
} }
delete temp;
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}