Write a C program that uses functions to perform the
following operations on doubly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
/* Node Structure definition */
struct node
{
int data;
struct node *next;
struct node *prev;
};
/* Global variables */
struct node *head = NULL, *tail = NULL;
/* Function to count nodes */
int count()
{
int cnt = 0;
struct node *temp = head;
while (temp != NULL)
{
cnt++;
temp = temp->next;
}
return cnt;
}
/* Function to create doubly linked list */
void createDLL()
{
struct node *newnode;
int x;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data you want to insert: ");
scanf("%d", &x);
newnode->data = x;
newnode->prev = NULL;
newnode->next = NULL;
if (head == NULL)
{
head = tail = newnode;
}
else
{
tail->next = newnode;
newnode->prev = tail;
tail = newnode;
}
}
/* Function to display doubly linked list */
void display()
{
struct node *temp = head;
if (head == NULL)
{
printf("List is Empty\n");
return;
}
printf("Doubly Linked List: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
/* Insertion at beginning */
void insertatbeginning()
{
struct node *newnode;
int x;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data you want to insert: ");
scanf("%d", &x);
newnode->data = x;
newnode->prev = NULL;
newnode->next = head;
if (head != NULL)
head->prev = newnode;
else
tail = newnode;
head = newnode;
}
/* Insertion at end */
void insertatend()
{
struct node *newnode;
int x;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data you want to insert: ");
scanf("%d", &x);
newnode->data = x;
newnode->next = NULL;
newnode->prev = tail;
if (tail != NULL)
tail->next = newnode;
else
head = newnode;
tail = newnode;
}
/* Insertion at particular position */
void insertatposition()
{
int pos, i = 1, x;
struct node *newnode, *temp;
printf("Enter the position: ");
scanf("%d", &pos);
if (pos < 1 || pos > count() + 1)
{
printf("Invalid position\n");
return;
}
printf("Enter the element you want to insert: ");
scanf("%d", &x);
if (pos == 1)
{
insertatbeginning();
}
else if (pos == count() + 1)
{
insertatend();
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = x;
temp = head;
while (i < pos - 1)
{
temp = temp->next;
i++;
}
newnode->prev = temp;
newnode->next = temp->next;
temp->next = newnode;
newnode ->next->prev = newnode;
}
}
/* Deletion from beginning */
void deletefrombeg()
{
struct node *temp;
if (head == NULL)
{
printf("List is Empty\n");
return;
}
temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
else
tail = NULL;
free(temp);
}
/* Deletion from end */
void deletefromend()
{
struct node *temp;
if (tail == NULL)
{
printf("List is Empty\n");
return;
}
temp = tail;
tail = tail->prev;
if (tail != NULL)
tail->next = NULL;
else
head = NULL;
free(temp);
}
/* Deletion from particular position */
void deletefrompos()
{
int pos, i = 1;
struct node *temp;
if (head == NULL)
{
printf("List is Empty\n");
return;
}
printf("Enter the position: ");
scanf("%d", &pos);
if (pos < 1 || pos > count())
{
printf("Invalid position\n");
return;
}
if (pos == 1)
{
deletefrombeg();
return;
}
if (pos == count())
{
deletefromend();
return;
}
temp = head;
while (i < pos)
{
temp = temp->next;
i++;
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
}
/* Main function */
int main()
{
int choice;
while (1)
{
printf("\n---- DOUBLY LINKED LIST MENU ----\n");
printf("1. Create\n");
printf("2. Insert at Beginning\n");
printf("3. Insert at End\n");
printf("4. Insert at Position\n");
printf("5. Delete from Beginning\n");
printf("6. Delete from End\n");
printf("7. Delete from Position\n");
printf("8. Display\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: createDLL(); break;
case 2: insertatbeginning(); break;
case 3: insertatend(); break;
case 4: insertatposition(); break;
case 5: deletefrombeg(); break;
case 6: deletefromend(); break;
case 7: deletefrompos(); break;
case 8: display(); break;
case 9: exit(0);
default: printf("Invalid choice\n");
}
}
}