You are on page 1of 11

//Name: - YASH DATTATRAY WAGHMARE

//Roll_No-67

//Pgm- doubly Linked list

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct Node* head = NULL;

// Function to insert a node at the beginning

void insertAtBeginning(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->prev = NULL;

newNode->next = head;

if (head != NULL) {

head->prev = newNode;

head = newNode;

}
// Function to insert a node at the end

void insertAtLast(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

newNode->prev = NULL;

head = newNode;

return;

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

// Function to insert a node at a random location

void insertAtLocation(int value, int position) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;
if (position == 1) {

newNode->prev = NULL;

newNode->next = head;

head->prev = newNode;

head = newNode;

return;

struct Node* temp = head;

for (int i = 1; i < position - 1 && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Invalid position\n");

return;

newNode->prev = temp;

newNode->next = temp->next;

if (temp->next != NULL) {

temp->next->prev = newNode;

temp->next = newNode;

}
// Function to delete a node from the beginning

void deleteFromBeginning() {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

head = head->next;

if (head != NULL) {

head->prev = NULL;

free(temp);

// Function to delete a node from the end

void deleteFromLast() {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

while (temp->next != NULL) {


temp = temp->next;

if (temp->prev != NULL) {

temp->prev->next = NULL;

} else {

head = NULL;

free(temp);

// Function to delete a node after a specified location

void deleteAfterLocation(int position) {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

for (int i = 1; i < position && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL || temp->next == NULL) {

printf("Invalid position\n");

return;
}

struct Node* nodeToDelete = temp->next;

temp->next = nodeToDelete->next;

if (nodeToDelete->next != NULL) {

nodeToDelete->next->prev = temp;

free(nodeToDelete);

// Function to search for an element

void searchElement(int value) {

struct Node* temp = head;

int position = 1;

while (temp != NULL) {

if (temp->data == value) {

printf("Element found at position %d\n", position);

return;

temp = temp->next;

position++;

}
printf("Element not found\n");

// Function to display the doubly linked list

void displayList() {

struct Node* temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

int choice, value, position;

do {

printf("\n1.Insert in beginning\n2.Insert at last\n3.Insert at any random location\n");

printf("4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n");

printf("7.Search for an element\n8.Show\n9.Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter the value to be inserted: ");

scanf("%d", &value);

insertAtBeginning(value);

break;

case 2:

printf("Enter the value to be inserted: ");

scanf("%d", &value);

insertAtLast(value);

break;

case 3:

printf("Enter the value to be inserted: ");

scanf("%d", &value);

printf("Enter the position: ");

scanf("%d", &position);

insertAtLocation(value, position);

break;

case 4:

deleteFromBeginning();

break;

case 5:

deleteFromLast();

break;

case 6:

printf("Enter the position after which node has to be deleted: ");

scanf("%d", &position);

deleteAfterLocation(position);
break;

case 7:

printf("Enter the element to be searched: ");

scanf("%d", &value);

searchElement(value);

break;

case 8:

displayList();

break;

case 9:

printf("Exiting the program\n");

break;

default:

printf("Invalid choice\n");

} while (choice != 9);

return 0;

Output:-

1. Insert in beginning

2. Insert at last

3. at any random location

4. Delete from Beginning

5. Delete from last

6. Delete node after specified location


7.Search for an element

8.Show

9. Exit

Enter your choice: 1

Enter the value to be inserted: 16

1. Insert in beginning

2. Insert at last

3. Insert at any random location

4. Delete from Beginning

5. Delete from last

6. Delete node after specified location

7.Search for an element

8.Show

9. Exit

Enter your choice: 2

Enter the value to be inserted: 18

1. Insert in beginning

2. Insert at last

3. Insert at any random location

4. Delete from Beginning

5. Delete from last

6. Delete node after specified location

7.Search for an element

8.Show

9. Exit
Enter your choice: 3

Enter the value to be inserted: 67

Enter the position: 3

1. Insert in beginning

2. Insert at last

3. Insert at any random location

4. Delete from Beginning

5. Delete from last

6. Delete node after specified location

7.Search for an element

8.Show

9. Exit

Enter your choice: 7

Enter the element to be searched: 67 Element found at position 3

1. in beginning

2. Insert at last

3. Insert at any random location

4. Delete from Beginning

5. Delete from last

6. Delete node after specified location

7. Search for an element

8.Show

9. Exit

Enter your choice: 8

16 18 67 are the elements present in doubly link list

You might also like