Exp 4
Exp 4
Division. A
Batch. C
Experiment No. 4
Program 1.
• Insertion :
To insert a new node at the beginning of the doubly list, we can use the
following steps:
• Create a new node, say new_node with the given data and set its
previous pointer to null, new_node->prev = NULL.
• Set the next pointer of new_node to current head, new_node->next
= head.
• If the linked list is not empty, update the previous pointer of the
current head to new_node, head->prev = new_node.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
To insert a new node at the end of the doubly linked list, we can use the
following steps:
• Allocate memory for a new node and assign the provided value to its
data field.
• Initialize the next pointer of the new node to nullptr.
• If the list is empty:
o Set the previous pointer of the new node to nullptr.
o Update the head pointer to point to the new node.
• If the list is not empty:
o Traverse the list starting from the head to reach the last node.
o Set the next pointer of the last node to point to the new node.
o Set the previous pointer of the new node to point to the last
node.
• If position = 1, create a new node and make it the head of the linked
list and return it.
• Otherwise, traverse the list to reach the node at position – 1,
say curr.
• If the position is valid, create a new node with given data,
say new_node.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
• Update the next pointer of new node to the next of current node and
prev pointer of new node to current node, new_node->next = curr-
>next and new_node->prev = curr.
• Similarly, update next pointer of current node to the new
node, curr->next = new_node.
• If the new node is not the last node, update prev pointer of new
node’s next to the new node, new_node->next->prev = new_node
• Deletion :
o Deletion of a node at the beginning of Doubly Linked List
To delete a node at the end in doubly linked list, we can use the
following steps:
• Check if the doubly linked list is empty. If it is empty, then there is
nothing to delete.
• If the list is not empty, then move to the last node of the doubly
linked list, say curr.
• Update the second-to-last node’s next pointer to NULL, curr-
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
>prev->next = NULL.
• Free the memory allocated for the node that was deleted.
PROBLEM
SOLVING ON THE
CONCEPT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
CODE: #include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
struct Node* prev;
int position;
};
ptr->data = data;
ptr->prev = NULL;
ptr->next = head;
if (head != NULL) {
head->prev = ptr;
}
return ptr;
}
int getLength(struct Node* head) {
if (head == NULL)
return 0;
return 1 + getLength(head->next);
}
if (head != NULL) {
head->prev = NULL;
}
free(ptr);
return head;
}
free(temp);
return head;
}
void menu(){
printf("1,Insert at node\n");
printf("2.Find the Length\n");
printf("3.delete\n");
printf("4.Delete right node\n");
printf("5.display\n");
printf("6.Exit\n");
}
int main(){
struct Node* head=NULL;
int choice;
int data;
int position;
menu();
do{
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
switch(choice){
case 1:
printf("Enter Node:");
scanf("%d",&data);
head = insertAtFirst(head,data);
break;
case 2:
printf("Length of Node is:");
printf("%d\n",getLength(head));
break;
case 3:
printf("Deleted");
head=deleteAtStart(head);
break;
case 4:
printf("Enter position:");
scanf("%d",&position);
printf("Right Deleted");
head=deleteright(head,position);
break;
case 5:
printf("Linked List:");
doublyLinkedListTraversal(head);
break;
case 6:
printf("Exiting...");
break;
default:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
printf("Invalid choice\n");
}
}while(choice != 6);
return 0;
}
OUTPUT SCREENSHOT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering