#include <stdlib.
h>
#include <stdio.h>
Void createList(int n);
Void insertNodeAtBeginning(int data);
Void displayList();
Void insertNodeAtEnd(int data);
Void insertNodeAtSpecific(int data, int position);
Void deleteFirstNode();
Void deleteLastNode();
Void deleteNode(int position);
Struct node {
Int data;
Struct node *next;
Struct node *prev;
} *head;
Int main() {
Int choice, n, data, position, pos;
While (1) {
Printf(“\n MENU \n”);
Printf(“\n 1. Create”);
Printf(“\n 2. Display”);
Printf(“\n 3. Insert at the beginning”);
Printf(“\n 4. Insert at the end”);
Printf(“\n 5. Insert at specified position”);
Printf(“\n 6. Delete at beginning”);
Printf(“\n 7. Delete at end”);
Printf(“\n 8. Delete at position”);
Printf(“\n 9. Exit”);
Printf(“\n--------------------------------------\n”);
Printf(“\nEnter your choice: “);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
Printf(“\nEnter the total number of nodes: “);
Scanf(“%d”, &n);
createList(n);
break;
case 2:
displayList();
break;
case 3:
printf(“\nEnter data to be inserted at beginning of list: “);
scanf(“%d”, &data);
insertNodeAtBeginning(data);
break;
case 4:
printf(“\nEnter data to be inserted at end of list: “);
scanf(“%d”, &data);
insertNodeAtEnd(data);
break;
case 5:
printf(“\nEnter data to be inserted at specified position: “);
scanf(“%d”, &data);
printf(“\nEnter position: “);
scanf(“%d”, &position);
insertNodeAtSpecific(data, position);
break;
case 6:
deleteFirstNode();
break;
case 7:
deleteLastNode();
break;
case 8:
printf(“Enter the position to delete: “);
scanf(“%d”, &pos);
deleteNode(pos);
break;
case 9:
exit(0);
break;
default:
printf(“\n Wrong Choice:”);
break;
Return 0;
}
Void createList(int n) {
Struct node *newNode, *temp;
Int data, I;
Head = (struct node *)malloc(sizeof(struct node));
If (head == NULL) {
Printf(“Unable to allocate memory.”);
} else {
Printf(“Enter the data of node 1: “);
Scanf(“%d”, &data);
Head->data = data;
Head->next = NULL;
Head->prev = NULL;
Temp = head;
For (I = 2; I <= n; i++) {
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf(“Unable to allocate memory.”);
break;
} else {
Printf(“Enter the data of node %d: “, i);
Scanf(“%d”, &data);
newNode->data = data;
newNode->next = NULL;
newNode->prev = temp;
temp->next = newNode;
temp = temp->next;
Printf(“DOUBLY LINKED LIST CREATED SUCCESSFULLY\n”);
Void insertNodeAtBeginning(int data) {
Struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf(“Unable to allocate memory.”);
} else {
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
if (head != NULL)
head->prev = newNode;
head = newNode;
printf(“DATA INSERTED SUCCESSFULLY AT BEGINNING\n”);
Void insertNodeAtEnd(int data) {
Struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf(“Unable to allocate memory.”);
} else {
newNode->data = data;
newNode->next = NULL;
temp = head;
if (temp == NULL) {
newNode->prev = NULL;
head = newNode;
} else {
While (temp->next != NULL) {
Temp = temp->next;
Temp->next = newNode;
newNode->prev = temp;
Printf(“DATA INSERTED SUCCESSFULLY AT END\n”);
Void insertNodeAtSpecific(int data, int position) {
Int I;
Struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL) {
printf(“Unable to allocate memory.”);
} else {
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
temp = head;
for (I = 1; I < position – 1; i++) {
temp = temp->next;
if (temp == NULL)
break;
}
If (temp != NULL) {
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
printf(“DATA INSERTED SUCCESSFULLY\n”);
} else {
Printf(“UNABLE TO INSERT DATA AT THE GIVEN POSITION\n”);
Void deleteFirstNode() {
Struct node *toDelete;
If (head == NULL) {
Printf(“List is already empty.”);
} else {
toDelete = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
printf(“\nData deleted = %d\n”, toDelete->data);
free(toDelete);
printf(“SUCCESSFULLY DELETED FIRST NODE FROM LIST\n”);
Void deleteLastNode() {
Struct node *toDelete;
If (head == NULL) {
Printf(“List is already empty.”);
} else {
toDelete = head;
while (toDelete->next != NULL) {
toDelete = toDelete->next;
If (toDelete->prev != NULL)
toDelete->prev->next = NULL;
if (toDelete == head)
head = NULL;
free(toDelete);
printf(“SUCCESSFULLY DELETED LAST NODE OF LIST\n”);
}
Void deleteNode(int position) {
Struct node *temp;
Int I;
If (head == NULL) {
Printf(“List is empty\n”);
Return;
Temp = head;
For (I = 1; I < position; i++) {
Temp = temp->next;
If (temp == NULL)
Break;
If (temp == NULL) {
Printf(“Position not found\n”);
} else {
If (temp->prev != NULL)
Temp->prev->next = temp->next;
If (temp->next != NULL)
Temp->next->prev = temp->prev;
If (temp == head)
Head = temp->next;
Printf(“Node deleted at position %d\n”, position);
Free(temp);
Void displayList() {
Struct node *temp;
If (head == NULL) {
Printf(“List is empty.”);
} else {
Temp = head;
While (temp != NULL) {
Printf(“Data = %d\n”, temp->data);
Temp = temp->next;
Printf(“NULL\n”);
Double link list code full implementation
/* Initialize nodes */
Struct node *head;
Struct node *one = NULL;
Struct node *two = NULL;
Struct node *three = NULL;
/* Allocate memory */
One = malloc(sizeof(struct node));
Two = malloc(sizeof(struct node));
Three = malloc(sizeof(struct node));
/* Assign data values */
One->data = 1;
Two->data = 2;
Three->data = 3;
/* Connect nodes */
One->next = two;
One->prev = NULL;
Two->next = three;
Two->prev = one;
Three->next = NULL;
Three->prev = two;
/* Save address of first node in head */
Head = one;