0% found this document useful (0 votes)
6 views

Lab Program No. 7

Uploaded by

ayushngowda838
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Program No. 7

Uploaded by

ayushngowda838
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

/* Create a menu-driven program in C that facilitates various operations on doubly linked list.

Create doubly linked list of N nodes with integer data by adding each node at the front.
Delete the node of a given data if it is found, otherwise display appropriate message.
Insert a node to the left of the node whose key value is read as input.
Display the contents of the list.
*/

#include <stdio.h>
#include <stdlib.h>

// Define the structure of a node in a doubly linked list


struct Node
{
int data;
struct Node *prev;
struct Node *next;
};

// Function to create a new node


struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("Memory allocation failed!\n");
return NULL;
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to add node at the front of the doubly linked list


void addNodeAtFront(struct Node** head, int data)
{
struct Node* newNode = createNode(data);
if (*head == NULL)
{
*head = newNode;
}
else
{
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}

// Function to display the contents of the doubly linked list


void displayList(struct Node* head)
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("List: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Function to delete a node with a given value


void deleteNode(struct Node** head, int data)
{
if (*head == NULL)
{
printf("List is empty, nothing to delete.\n");
return;
}

struct Node* temp = *head;


while (temp != NULL)
{
if (temp->data == data)
{
if (temp->prev != NULL)
{
temp->prev->next = temp->next;
} else {
*head = temp->next; // This means we are deleting the head node
}
if (temp->next != NULL)
{
temp->next->prev = temp->prev;
}
free(temp);
printf("Node with data %d deleted.\n", data);
return;
}
temp = temp->next;
}
printf("Node with data %d not found.\n", data);
}

// Function to insert a node to the left of the node with a given key value
void insertNodeToLeft(struct Node** head, int key, int newData) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = *head;


while (temp != NULL)
{
if (temp->data == key)
{
struct Node* newNode = createNode(newData);

// Insert the new node before the current node


newNode->prev = temp->prev;
newNode->next = temp;

if (temp->prev != NULL)
{
temp->prev->next = newNode;
}
else
{
*head = newNode; // New node is the new head
}
temp->prev = newNode;
printf("Node with data %d inserted before node with data %d.\n", newData, key);
return;
}
temp = temp->next;
}
printf("Node with key %d not found.\n", key);
}

// Main menu-driven program


int main()
{
struct Node* head = NULL;
int choice, data, key;
while (1) {
printf("\nDoubly Linked List Operations Menu\n");
printf("1. Create list by adding node at the front\n");
printf("2. Delete node with a given data\n");
printf("3. Insert node to the left of a node with given key\n");
printf("4. Display the contents of the list\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to add at the front: ");
scanf("%d", &data);
addNodeAtFront(&head, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 3:
printf("Enter key value to insert before: ");
scanf("%d", &key);
printf("Enter data for the new node: ");
scanf("%d", &data);
insertNodeToLeft(&head, key, data);
break;
case 4:
displayList(head);
break;
case 5:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

You might also like